<A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<
Arr.NonEmptyReadonlyArray<A>,
never,
L
>Creates a Channel that emits arrays of elements from an iterable.
Example (Batching iterable output)
import { Channel } from "effect"
const numbers = [1, 2, 3, 4, 5]
const channel = Channel.fromIterableArray(numbers)
// Emits arrays like: [1, 2, 3, 4], [5] (based on chunk size)export const const fromIterableArray: <A, L>(
iterable: Iterable<A, L>,
chunkSize?: number
) => Channel<
Arr.NonEmptyReadonlyArray<A>,
never,
L
>
Creates a Channel that emits arrays of elements from an iterable.
Example (Batching iterable output)
import { Channel } from "effect"
const numbers = [1, 2, 3, 4, 5]
const channel = Channel.fromIterableArray(numbers)
// Emits arrays like: [1, 2, 3, 4], [5] (based on chunk size)
fromIterableArray = <function (type parameter) A in <A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<Arr.NonEmptyReadonlyArray<A>, never, L>A, function (type parameter) L in <A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<Arr.NonEmptyReadonlyArray<A>, never, L>L>(
iterable: Iterable<A, L>iterable: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<Arr.NonEmptyReadonlyArray<A>, never, L>A, function (type parameter) L in <A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<Arr.NonEmptyReadonlyArray<A>, never, L>L>,
chunkSize: numberchunkSize = const DefaultChunkSize: numberThe default chunk size used by channels for batching operations.
Example (Reading the default chunk size)
import { Channel } from "effect"
console.log(Channel.DefaultChunkSize) // 4096
DefaultChunkSize
): 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 Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<function (type parameter) A in <A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<Arr.NonEmptyReadonlyArray<A>, never, L>A>, never, function (type parameter) L in <A, L>(iterable: Iterable<A, L>, chunkSize?: number): Channel<Arr.NonEmptyReadonlyArray<A>, never, L>L> => const fromIteratorArray: <A, L>(
iterator: LazyArg<Iterator<A, L>>,
chunkSize?: number
) => Channel<
Arr.NonEmptyReadonlyArray<A>,
never,
L
>
Creates a Channel from an iterator that emits arrays of elements.
Example (Batching iterator output)
import { Channel } from "effect"
// Create a channel from a simple iterator
const numberIterator = (): Iterator<number, string> => {
let count = 0
return {
next: () => {
if (count < 3) {
return { value: count++, done: false }
}
return { value: "finished", done: true }
}
}
}
const channel = Channel.fromIteratorArray(() => numberIterator(), 2)
// This will emit arrays: [0, 1], [2], then complete with "finished"
Example (Batching generator output)
import { Channel } from "effect"
// Create channel from a generator function
function* fibonacci(): Generator<number, void, unknown> {
let a = 0, b = 1
for (let i = 0; i < 5; i++) {
yield a
;[a, b] = [b, a + b]
}
}
const fibChannel = Channel.fromIteratorArray(() => fibonacci(), 3)
// Emits: [0, 1, 1], [2, 3], then completes
fromIteratorArray(() => iterable: Iterable<A, L>iterable[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator](), chunkSize: numberchunkSize)