<A, E = never>(capacity: number): Effect<Queue<A, E>>Creates a bounded queue with dropping strategy. When the queue reaches capacity, new elements are dropped and the offer operation returns false.
When to use
Use when you need producer offers not to block while preserving existing queued messages, even if new messages may be dropped when the queue is full.
Example (Creating dropping queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.dropping<number>(2)
// Fill the queue to capacity
const success1 = yield* Queue.offer(queue, 1)
const success2 = yield* Queue.offer(queue, 2)
console.log(success1, success2) // true, true
// This will be dropped
const success3 = yield* Queue.offer(queue, 3)
console.log(success3) // false
const all = yield* Queue.takeAll(queue)
console.log(all) // [1, 2] - element 3 was dropped
})export const const dropping: <A, E = never>(
capacity: number
) => Effect<Queue<A, E>>
Creates a bounded queue with dropping strategy. When the queue reaches capacity,
new elements are dropped and the offer operation returns false.
When to use
Use when you need producer offers not to block while preserving existing
queued messages, even if new messages may be dropped when the queue is full.
Example (Creating dropping queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.dropping<number>(2)
// Fill the queue to capacity
const success1 = yield* Queue.offer(queue, 1)
const success2 = yield* Queue.offer(queue, 2)
console.log(success1, success2) // true, true
// This will be dropped
const success3 = yield* Queue.offer(queue, 3)
console.log(success3) // false
const all = yield* Queue.takeAll(queue)
console.log(all) // [1, 2] - element 3 was dropped
})
dropping = <function (type parameter) A in <A, E = never>(capacity: number): Effect<Queue<A, E>>A, function (type parameter) E in <A, E = never>(capacity: number): Effect<Queue<A, E>>E = never>(capacity: numbercapacity: number): import EffectEffect<interface Queue<in out A, in out E = never>A Queue is an asynchronous queue that can be offered to and taken from.
Details
It also supports signaling that it is done or failed.
Example (Offering and taking queue values)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
// Create a bounded queue
const queue = yield* Queue.bounded<string>(10)
// Producer: offer items to the queue
yield* Queue.offer(queue, "hello")
yield* Queue.offerAll(queue, ["world", "!"])
// Consumer: take items from the queue
const item1 = yield* Queue.take(queue)
const item2 = yield* Queue.take(queue)
const item3 = yield* Queue.take(queue)
console.log([item1, item2, item3]) // ["hello", "world", "!"]
})
Companion namespace containing type-level metadata and low-level state types
for Queue.
Queue<function (type parameter) A in <A, E = never>(capacity: number): Effect<Queue<A, E>>A, function (type parameter) E in <A, E = never>(capacity: number): Effect<Queue<A, E>>E>> =>
const make: <A, E = never>(
options?:
| {
readonly capacity?: number | undefined
readonly strategy?:
| "suspend"
| "dropping"
| "sliding"
| undefined
}
| undefined
) => Effect<Queue<A, E>>
Creates a Queue with optional capacity and overflow strategy.
Details
By default the queue is unbounded and uses the "suspend" strategy. Provide
capacity for a bounded queue and choose "suspend", "dropping", or
"sliding" to control what happens when the queue is full. The returned
queue can be offered to, taken from, failed, ended, interrupted, or shut down.
Example (Creating queues)
import { Cause, Effect, Queue } from "effect"
Effect.gen(function*() {
const queue = yield* Queue.make<number, string | Cause.Done>()
// add messages to the queue
yield* Queue.offer(queue, 1)
yield* Queue.offer(queue, 2)
yield* Queue.offerAll(queue, [3, 4, 5])
// take messages from the queue
const messages = yield* Queue.takeAll(queue)
console.log(messages) // [1, 2, 3, 4, 5]
// signal that the queue is done
yield* Queue.end(queue)
const done = yield* Effect.flip(Queue.take(queue))
console.log(Cause.isDone(done)) // true
// signal that another queue has failed
const failedQueue = yield* Queue.make<number, string>()
const failed = yield* Queue.fail(failedQueue, "boom")
console.log(failed) // true
})
make({ capacity?: number | undefinedcapacity, strategy?: | "suspend"
| "dropping"
| "sliding"
| undefined
strategy: "dropping" })