<A, E>(queue: Queue.Dequeue<A, E>): Channel<
Arr.NonEmptyReadonlyArray<A>,
Exclude<E, Cause.Done>
>Creates a channel from a queue that emits arrays of elements.
Example (Creating batched channels from queues)
import { Channel, Data, Effect, Queue } from "effect"
class ProcessingError extends Data.TaggedError("ProcessingError")<{
readonly stage: string
}> {}
const program = Effect.gen(function*() {
// Create a queue for batch processing
const queue = yield* Queue.bounded<number, ProcessingError>(100)
// Fill queue with data
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
// Create a channel that reads arrays from the queue
const arrayChannel = Channel.fromQueueArray(queue)
// This will emit non-empty arrays of elements instead of individual items
// Useful for batch processing scenarios
return arrayChannel
})
// High-throughput processing example
const batchProcessor = Effect.gen(function*() {
const dataQueue = yield* Queue.dropping<string, ProcessingError>(1000)
const batchChannel = Channel.fromQueueArray(dataQueue)
// Process data in batches for better performance
return Channel.map(
batchChannel,
(batch) => batch.map((item) => item.toUpperCase())
)
})export const const fromQueueArray: <A, E>(
queue: Queue.Dequeue<A, E>
) => Channel<
Arr.NonEmptyReadonlyArray<A>,
Exclude<E, Cause.Done>
>
Creates a channel from a queue that emits arrays of elements.
Example (Creating batched channels from queues)
import { Channel, Data, Effect, Queue } from "effect"
class ProcessingError extends Data.TaggedError("ProcessingError")<{
readonly stage: string
}> {}
const program = Effect.gen(function*() {
// Create a queue for batch processing
const queue = yield* Queue.bounded<number, ProcessingError>(100)
// Fill queue with data
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
// Create a channel that reads arrays from the queue
const arrayChannel = Channel.fromQueueArray(queue)
// This will emit non-empty arrays of elements instead of individual items
// Useful for batch processing scenarios
return arrayChannel
})
// High-throughput processing example
const batchProcessor = Effect.gen(function*() {
const dataQueue = yield* Queue.dropping<string, ProcessingError>(1000)
const batchChannel = Channel.fromQueueArray(dataQueue)
// Process data in batches for better performance
return Channel.map(
batchChannel,
(batch) => batch.map((item) => item.toUpperCase())
)
})
fromQueueArray = <function (type parameter) A in <A, E>(queue: Queue.Dequeue<A, E>): Channel<Arr.NonEmptyReadonlyArray<A>, Exclude<E, Cause.Done>>A, function (type parameter) E in <A, E>(queue: Queue.Dequeue<A, E>): Channel<Arr.NonEmptyReadonlyArray<A>, Exclude<E, Cause.Done>>E>(
queue: Queue.Dequeue<A, E>(parameter) queue: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
queue: import QueueQueue.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>(queue: Queue.Dequeue<A, E>): Channel<Arr.NonEmptyReadonlyArray<A>, Exclude<E, Cause.Done>>A, function (type parameter) E in <A, E>(queue: Queue.Dequeue<A, E>): Channel<Arr.NonEmptyReadonlyArray<A>, Exclude<E, Cause.Done>>E>
): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<import ArrArr.type NonEmptyReadonlyArray<A> = readonly [
A,
...A[]
]
A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<function (type parameter) A in <A, E>(queue: Queue.Dequeue<A, E>): Channel<Arr.NonEmptyReadonlyArray<A>, Exclude<E, Cause.Done>>A>, type Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) E in <A, E>(queue: Queue.Dequeue<A, E>): Channel<Arr.NonEmptyReadonlyArray<A>, Exclude<E, Cause.Done>>E, import CauseCause.interface Done<A = void>A graceful completion signal for queues and streams.
When to use
Use to model normal producer completion through a stream or queue error
channel.
Details
Done indicates that a producer has finished normally — no more elements
will arrive. It is distinct from an error or interruption; it represents
successful completion. The optional value field can carry a final
leftover payload.
Example (Signaling queue completion)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number, Cause.Done>(10)
yield* Queue.offer(queue, 1)
yield* Queue.end(queue)
const result = yield* Effect.flip(Queue.take(queue))
console.log(Cause.isDone(result)) // true
})
Companion namespace for the Done interface.
Creates a Done signal with an optional value.
When to use
Use when you need to construct a low-level pull completion signal directly.
Done>> => const fromPull: <
OutElem,
OutErr,
OutDone,
EX,
EnvX,
Env
>(
effect: Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
unknown,
unknown,
unknown,
Env | EnvX
>
Creates a Channel from an Effect that produces a Pull.
Example (Creating channels from pulls)
import { Channel, Effect } from "effect"
const channel = Channel.fromPull(
Effect.succeed(Effect.succeed(42))
)
fromPull(import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(import QueueQueue.const takeAll: <A, E>(
self: Dequeue<A, E>
) => Effect<Arr.NonEmptyArray<A>, E>
Takes all currently available messages, waiting until at least one message
is available when the queue is empty.
When to use
Use when consumers should process the next non-empty batch of buffered
messages instead of repeatedly taking one message at a time.
Details
Returns a non-empty array. If the queue completes or fails before a message
can be taken, the effect fails with the queue's terminal error.
Example (Taking all available values)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number, Cause.Done>(5)
// Add several messages
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5])
// Take all available messages
const messages1 = yield* Queue.takeAll(queue)
console.log(messages1) // [1, 2, 3, 4, 5]
})
takeAll(queue: Queue.Dequeue<A, E>(parameter) queue: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
queue)))