<A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>Takes up to n messages from the queue.
Details
The operation may wait until enough messages are available to satisfy the
queue's batching rules. If n is less than or equal to zero, it succeeds
with an empty array. If the queue completes or fails before messages can be
taken, the effect fails with the queue's terminal error.
Example (Taking a fixed number of values)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number, Cause.Done>(10)
// Add several messages
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5, 6, 7])
// Take exactly 3 messages
const first3 = yield* Queue.takeN(queue, 3)
console.log(first3) // [1, 2, 3]
// Take exactly 2 more messages
const next2 = yield* Queue.takeN(queue, 2)
console.log(next2) // [4, 5]
// Take remaining messages
const remaining = yield* Queue.takeN(queue, 2)
console.log(remaining) // [6, 7]
})export const const takeN: <A, E>(
self: Dequeue<A, E>,
n: number
) => Effect<Array<A>, E>
Takes up to n messages from the queue.
Details
The operation may wait until enough messages are available to satisfy the
queue's batching rules. If n is less than or equal to zero, it succeeds
with an empty array. If the queue completes or fails before messages can be
taken, the effect fails with the queue's terminal error.
Example (Taking a fixed number of values)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number, Cause.Done>(10)
// Add several messages
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5, 6, 7])
// Take exactly 3 messages
const first3 = yield* Queue.takeN(queue, 3)
console.log(first3) // [1, 2, 3]
// Take exactly 2 more messages
const next2 = yield* Queue.takeN(queue, 2)
console.log(next2) // [4, 5]
// Take remaining messages
const remaining = yield* Queue.takeN(queue, 2)
console.log(remaining) // [6, 7]
})
takeN = <function (type parameter) A in <A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>A, function (type parameter) E in <A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>E>(
self: Dequeue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self: interface Dequeue<out A, out E = never>A Dequeue is a queue that can be taken from.
Details
This interface represents the read-only part of a Queue, allowing you to take
elements from the queue but not offer elements to it.
Example (Taking through dequeue handles)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<string, never>(10)
// A Dequeue can only take elements
const dequeue: Queue.Dequeue<string> = queue
// Pre-populate the queue
yield* Queue.offerAll(queue, ["a", "b", "c"])
// Take elements using dequeue interface
const item = yield* Queue.take(dequeue)
console.log(item) // "a"
})
Companion namespace containing type-level metadata for the Dequeue
read-only queue interface.
Dequeue<function (type parameter) A in <A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>A, function (type parameter) E in <A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>E>,
n: numbern: 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 Array<T>Array<function (type parameter) A in <A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>A>, function (type parameter) E in <A, E>(self: Dequeue<A, E>, n: number): Effect<Array<A>, E>E> => const takeBetween: <A, E>(
self: Dequeue<A, E>,
min: number,
max: number
) => Effect<Array<A>, E>
Takes between min and max messages from the queue.
Details
The operation waits when fewer than the required minimum messages are
available. It returns at most max messages. If the queue completes or fails
before the minimum can be satisfied, the effect fails with the queue's
terminal error.
Example (Taking a bounded batch of values)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(10)
// Add several messages
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5, 6, 7, 8])
// Take between 2 and 5 messages
const batch1 = yield* Queue.takeBetween(queue, 2, 5)
console.log(batch1) // [1, 2, 3, 4, 5] - took 5 (up to max)
// Take between 1 and 10 messages (but only 3 remain)
const batch2 = yield* Queue.takeBetween(queue, 1, 10)
console.log(batch2) // [6, 7, 8] - took 3 (all remaining)
// No more messages available, will wait or return done
// const batch3 = yield* Queue.takeBetween(queue, 1, 3)
})
takeBetween(self: Dequeue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self, n: numbern, n: numbern)