<A, E = never>(capacity: number): Effect<Queue<A, E>>Creates a bounded queue with the specified capacity that uses backpressure strategy.
Details
When the queue reaches capacity, producers will be suspended until space becomes available. This ensures all messages are processed but may slow down producers.
Example (Creating bounded queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<string>(5)
// This will succeed as queue has capacity
yield* Queue.offer(queue, "first")
yield* Queue.offer(queue, "second")
const size = yield* Queue.size(queue)
console.log(size) // 2
})export const const bounded: <A, E = never>(
capacity: number
) => Effect<Queue<A, E>>
Creates a bounded queue with the specified capacity that uses backpressure strategy.
Details
When the queue reaches capacity, producers will be suspended until space becomes available.
This ensures all messages are processed but may slow down producers.
Example (Creating bounded queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<string>(5)
// This will succeed as queue has capacity
yield* Queue.offer(queue, "first")
yield* Queue.offer(queue, "second")
const size = yield* Queue.size(queue)
console.log(size) // 2
})
bounded = <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): interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<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 })