<A, E = never>(capacity: number): Effect<Queue<A, E>>Creates a bounded queue with sliding strategy. When the queue reaches capacity, new elements are added and the oldest elements are dropped.
When to use
Use when you need producer offers not to block and can accept dropping the oldest messages, such as when maintaining a rolling window of recent values.
Example (Creating sliding queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.sliding<number>(3)
// Fill the queue to capacity
yield* Queue.offer(queue, 1)
yield* Queue.offer(queue, 2)
yield* Queue.offer(queue, 3)
// This will succeed, dropping the oldest element (1)
yield* Queue.offer(queue, 4)
const all = yield* Queue.takeAll(queue)
console.log(all) // [2, 3, 4] - oldest element (1) was dropped
})export const const sliding: <A, E = never>(
capacity: number
) => Effect<Queue<A, E>>
Creates a bounded queue with sliding strategy. When the queue reaches capacity,
new elements are added and the oldest elements are dropped.
When to use
Use when you need producer offers not to block and can accept dropping the
oldest messages, such as when maintaining a rolling window of recent values.
Example (Creating sliding queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.sliding<number>(3)
// Fill the queue to capacity
yield* Queue.offer(queue, 1)
yield* Queue.offer(queue, 2)
yield* Queue.offer(queue, 3)
// This will succeed, dropping the oldest element (1)
yield* Queue.offer(queue, 4)
const all = yield* Queue.takeAll(queue)
console.log(all) // [2, 3, 4] - oldest element (1) was dropped
})
sliding = <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: "sliding" })