(options: {
readonly concurrency: number | "unbounded"
readonly bufferSize?: number | undefined
readonly switch?: boolean | undefined
}): <
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>(
channels: Channel<
Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
) => Channel<
OutElem,
OutErr1 | OutErr,
OutDone,
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
Env1 | Env
>
<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>(
channels: Channel<
Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>,
options: {
readonly concurrency: number | "unbounded"
readonly bufferSize?: number | undefined
readonly switch?: boolean | undefined
}
): Channel<
OutElem,
OutErr1 | OutErr,
OutDone,
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
Env1 | Env
>Merges multiple channels with specified concurrency and buffering options.
When to use
Use when channel outputs are themselves channels and multiple inner channels should run with configured concurrency and buffering.
Example (Merging nested channels)
import { Channel, Data } from "effect"
class MergeAllError extends Data.TaggedError("MergeAllError")<{
readonly reason: string
}> {}
// Create channels that output other channels
const nestedChannels = Channel.fromIterable([
Channel.fromIterable([1, 2]),
Channel.fromIterable([3, 4]),
Channel.fromIterable([5, 6])
])
// Merge all channels with bounded concurrency
const mergedChannel = Channel.mergeAll({
concurrency: 2,
bufferSize: 16
})(nestedChannels)
// Outputs: 1, 2, 3, 4, 5, 6 (order may vary due to concurrency)export const const mergeAll: {
(options: {
readonly concurrency: number | "unbounded"
readonly bufferSize?: number | undefined
readonly switch?: boolean | undefined
}): <
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>(
channels: Channel<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
) => Channel<
OutElem,
OutErr1 | OutErr,
OutDone,
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
Env1 | Env
>
<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>(
channels: Channel<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>,
options: {
readonly concurrency: number | "unbounded"
readonly bufferSize?: number | undefined
readonly switch?: boolean | undefined
}
): Channel<
OutElem,
OutErr1 | OutErr,
OutDone,
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
Env1 | Env
>
}
Merges multiple channels with specified concurrency and buffering options.
When to use
Use when channel outputs are themselves channels and multiple inner channels
should run with configured concurrency and buffering.
Example (Merging nested channels)
import { Channel, Data } from "effect"
class MergeAllError extends Data.TaggedError("MergeAllError")<{
readonly reason: string
}> {}
// Create channels that output other channels
const nestedChannels = Channel.fromIterable([
Channel.fromIterable([1, 2]),
Channel.fromIterable([3, 4]),
Channel.fromIterable([5, 6])
])
// Merge all channels with bounded concurrency
const mergedChannel = Channel.mergeAll({
concurrency: 2,
bufferSize: 16
})(nestedChannels)
// Outputs: 1, 2, 3, 4, 5, 6 (order may vary due to concurrency)
mergeAll: {
(options: {
readonly concurrency: number | "unbounded"
readonly bufferSize?: number | undefined
readonly switch?: boolean | undefined
}
options: {
readonly concurrency: number | "unbounded"concurrency: number | "unbounded"
readonly bufferSize?: number | undefinedbufferSize?: number | undefined
readonly switch?: boolean | undefinedswitch?: boolean | undefined
}): <function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutElem, function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutErr1, function (type parameter) OutDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutDone1, function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InElem1, function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InErr1, function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InDone1, function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>Env1, function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutErr, function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutDone, function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InElem, function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InErr, function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InDone, function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>Env>(
channels: Channel<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
(parameter) channels: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
channels: 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<
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<function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutElem, function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutErr1, function (type parameter) OutDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutDone1, function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InElem1, function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InErr1, function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InDone1, function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>Env1>,
function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutErr,
function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutDone,
function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InElem,
function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InErr,
function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InDone,
function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>Env
>
) => 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<
function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutElem,
function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutErr1 | function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutErr,
function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>OutDone,
function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InElem & function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InElem1,
function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InErr & function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InErr1,
function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InDone & function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>InDone1,
function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>Env1 | function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>Env
>
<function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem, function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1, function (type parameter) OutDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone1, function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem1, function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr1, function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone1, function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env1, function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr, function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone, function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem, function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr, function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone, function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env>(
channels: Channel<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
(parameter) channels: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
channels: 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<
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<function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem, function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1, function (type parameter) OutDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone1, function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem1, function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr1, function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone1, function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env1>,
function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr,
function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone,
function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem,
function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr,
function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone,
function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env
>,
options: {
readonly concurrency: number | "unbounded"
readonly bufferSize?: number | undefined
readonly switch?: boolean | undefined
}
options: {
readonly concurrency: number | "unbounded"concurrency: number | "unbounded"
readonly bufferSize?: number | undefinedbufferSize?: number | undefined
readonly switch?: boolean | undefinedswitch?: boolean | undefined
}
): 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<
function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem,
function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1 | function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr,
function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone,
function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem & function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem1,
function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr & function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr1,
function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone & function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone1,
function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env1 | function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, options: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env
>
} = dual<(...args: Array<any>) => any, <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}) => Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>>(arity: 2, body: <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}) => Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>): ((...args: Array<any>) => any) & (<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}) => Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>) (+1 overload)
Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(
2,
<function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem, function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1, function (type parameter) OutDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone1, function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem1, function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr1, function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone1, function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env1, function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr, function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone, function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem, function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr, function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone, function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env>(
channels: Channel<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
(parameter) channels: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
channels: 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<
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<function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem, function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1, function (type parameter) OutDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone1, function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem1, function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr1, function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone1, function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env1>,
function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr,
function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone,
function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem,
function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr,
function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone,
function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env
>,
{ bufferSize: numberbufferSize = 16, concurrency: number | "unbounded"concurrency, switch?: boolean | undefinedswitch: switch_: booleanswitch_ = false }: {
readonly concurrency: number | "unbounded"concurrency: number | "unbounded"
readonly bufferSize?: number | undefinedbufferSize?: number | undefined
readonly switch?: boolean | undefinedswitch?: boolean | undefined
}
): 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<
function (type parameter) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem,
function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1 | function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr,
function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone,
function (type parameter) InElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem & function (type parameter) InElem1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InElem1,
function (type parameter) InErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr & function (type parameter) InErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InErr1,
function (type parameter) InDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone & function (type parameter) InDone1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
InDone1,
function (type parameter) Env1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env1 | function (type parameter) Env in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
Env
> =>
const fromTransformBracket: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
EX,
EnvX,
Env
>(
f: (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope,
forkedScope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
InElem,
InErr,
InDone,
Env | EnvX
>
Creates a Channel from a transformation function that operates on upstream
pulls, but also provides a forked scope that closes when the resulting
Channel completes.
When to use
Use when building channels that require scoped resource lifecycle management,
providing both the channel scope and a forked scope that automatically closes
when the channel completes.
fromTransformBracket(
import EffectEffect.const fnUntraced: <Effect.Effect<void, never, never> | Effect.Effect<Pull.Pull<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, never>, never, Env> | Effect.Effect<Fiber.Fiber<boolean, never>, never, Env1>, Effect.Effect<OutElem, any, never>, [upstream: Pull.Pull<InElem & InElem1, InErr & InErr1, InDone & InDone1, never>, scope: Scope.Scope, forkedScope: Scope.Scope]>(body: (this: Types.unassigned, upstream: Pull.Pull<...>, scope: Scope.Scope, forkedScope: Scope.Scope) => Generator<...>) => (upstream: Pull.Pull<...>, scope: Scope.Scope, forkedScope: Scope.Scope) => Effect.Effect<...> (+41 overloads)fnUntraced(function*(upstream: Pull.Pull<
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, forkedScope: Scope.Scope(parameter) forkedScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
forkedScope) {
const const concurrencyN: numberconcurrencyN = concurrency: number | "unbounded"concurrency === "unbounded"
? var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.MAX_SAFE_INTEGER: numberThe value of the largest integer n such that n and n + 1 are both exactly representable as
a Number value.
The value of Number.MAX_SAFE_INTEGER is 9007199254740991 2^53 − 1.
MAX_SAFE_INTEGER
: var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(1, concurrency: numberconcurrency)
const const semaphore:
| Semaphore.Semaphore
| undefined
semaphore = switch_: booleanswitch_ ? var undefinedundefined : import SemaphoreSemaphore.const makeUnsafe: (
permits: number
) => Semaphore
Creates a Semaphore synchronously with the specified total
number of permits.
When to use
Use to construct a semaphore synchronously when an immediate value is
required outside an Effect workflow.
Example (Creating an unsafe semaphore)
import { Effect, Semaphore } from "effect"
const semaphore = Semaphore.makeUnsafe(3)
const task = (id: number) =>
semaphore.withPermits(1)(
Effect.gen(function*() {
yield* Effect.log(`Task ${id} started`)
yield* Effect.sleep("1 second")
yield* Effect.log(`Task ${id} completed`)
})
)
// Only 3 tasks can run concurrently
const program = Effect.all([
task(1),
task(2),
task(3),
task(4),
task(5)
], { concurrency: "unbounded" })
makeUnsafe(const concurrencyN: numberconcurrencyN)
const const doneLatch: Latch.Latchconst doneLatch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
doneLatch = yield* import LatchLatch.const make: (
open?: boolean | undefined
) => Effect.Effect<Latch>
Creates a Latch inside Effect.
When to use
Use to create a latch for coordinating fibers inside Effect code.
Details
The latch starts closed by default; pass true to create it open.
Example (Creating a latch)
import { Effect, Latch } from "effect"
const program = Effect.gen(function*() {
const latch = yield* Latch.make(false)
const waiter = Effect.gen(function*() {
yield* Effect.log("Waiting for latch to open...")
yield* latch.await
yield* Effect.log("Latch opened! Continuing...")
})
const opener = Effect.gen(function*() {
yield* Effect.sleep("2 seconds")
yield* Effect.log("Opening latch...")
yield* latch.open
})
yield* Effect.all([waiter, opener])
})
make(true)
const const fibers: Set<Fiber.Fiber<any, any>>fibers = new var Set: SetConstructor
new <Fiber.Fiber<any, any>>(iterable?: Iterable<Fiber.Fiber<any, any>> | null | undefined) => Set<Fiber.Fiber<any, any>> (+1 overload)
Set<import FiberFiber.interface Fiber<out A, out E = never>A runtime fiber is a lightweight thread that executes Effects. Fibers are
the unit of concurrency in Effect. They provide a way to run multiple
Effects concurrently while maintaining structured concurrency and
cancellation safety.
When to use
Use to observe, join, interrupt, or coordinate work that has already been
forked.
Details
A fiber exposes both safe Effect-based operations, such as
await
,
join
, and
interrupt
, and low-level runtime fields used by
the scheduler and runtime internals.
Gotchas
Prefer the exported functions in this module over calling interruptUnsafe
or pollUnsafe directly. The unsafe methods are immediate runtime hooks and
do not provide the same Effect-based sequencing guarantees.
Example (Awaiting a forked fiber)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Fork an effect to run in a new fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Wait for the fiber to complete and get its result
const result = yield* Fiber.await(fiber)
console.log(result) // Exit.succeed(42)
return result
})
The Fiber namespace contains utility types and functions for working with fibers.
It provides type-level utilities for fiber operations and variance encoding.
When to use
Use to reference type-level helpers associated with Fiber.
Details
The namespace currently exposes type-level support used by the Fiber
interface. Runtime operations are exported as module-level functions.
Example (Working with fiber types)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Create a fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Use namespace types for variance
const typedFiber: Fiber.Fiber<number, never> = fiber
// Access fiber properties
console.log(`Fiber ID: ${fiber.id}`)
// Join the fiber
const result = yield* Fiber.join(fiber)
return result // 42
})
Fiber<any, any>>()
const const queue: Queue.Queue<
OutElem,
OutErr1 | OutErr | Cause.Done<OutDone>
>
const 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 = yield* import QueueQueue.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) OutElem in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutElem, function (type parameter) OutErr in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr | function (type parameter) OutErr1 in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutErr1 | import CauseCause.type Cause.Done = /*unresolved*/ anyDone<function (type parameter) OutDone in <OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1, OutErr, OutDone, InElem, InErr, InDone, Env>(channels: Channel<Channel<OutElem, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>, OutErr, OutDone, InElem, InErr, InDone, Env>, { bufferSize, concurrency, switch: switch_ }: {
readonly concurrency: number | "unbounded";
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
}): Channel<OutElem, OutErr1 | OutErr, OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>
OutDone>>(
bufferSize: numberbufferSize
)
yield* import ScopeScope.const addFinalizer: (
scope: Scope,
finalizer: Effect<unknown>
) => Effect<void>
Registers a finalizer effect on a scope.
Details
If the scope is open, the finalizer runs when the scope closes, regardless of
whether the scope closes successfully or with an error. If the scope is
already closed, the finalizer runs immediately.
Example (Adding finalizers)
import { Console, Effect, Exit, Scope } from "effect"
const program = Effect.gen(function*() {
const scope = yield* Scope.make()
// Add simple finalizers
yield* Scope.addFinalizer(scope, Console.log("Cleanup task 1"))
yield* Scope.addFinalizer(scope, Console.log("Cleanup task 2"))
yield* Scope.addFinalizer(scope, Effect.log("Cleanup task 3"))
// Do some work
yield* Console.log("Doing work...")
// Close the scope
yield* Scope.close(scope, Exit.void)
})
addFinalizer(forkedScope: Scope.Scope(parameter) forkedScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
forkedScope, import QueueQueue.const shutdown: <A, E>(
self: Enqueue<A, E>
) => Effect<boolean>
Shuts down the queue immediately, discarding buffered messages and resuming
pending operations.
Details
The operation is idempotent and returns true, including when the queue has
already been shut down or completed.
Example (Shutting down queues)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(2)
// Add messages
yield* Queue.offer(queue, 1)
yield* Queue.offer(queue, 2)
// Shutdown clears buffered messages and prevents further offers
const wasShutdown = yield* Queue.shutdown(queue)
console.log(wasShutdown) // true
// Queue is now done and cleared
const size = yield* Queue.size(queue)
console.log(size) // 0
})
shutdown(const queue: Queue.Queue<
OutElem,
OutErr1 | OutErr | Cause.Done<OutDone>
>
const 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))
const const pull: Pull.Pull<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
never
>
const pull: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
pull = yield* const toTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>(
channel: Channel<
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
) => (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone>,
never,
Env
>
Converts a Channel back to its underlying transformation function.
Example (Extracting channel transforms)
import { Channel } from "effect"
const channel = Channel.succeed(42)
const transform = Channel.toTransform(channel)
// transform can now be used directly
toTransform(channels: Channel<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
(parameter) channels: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
channels)(upstream: Pull.Pull<
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope)
yield* import EffectEffect.const gen: {
<Eff extends Effect<any, any, any>, AEff>(
f: () => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
<Self, Eff extends Effect<any, any, any>, AEff>(
options: { readonly self: Self },
f: (this: Self) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
}
gen(function*() {
while (true) {
if (const semaphore:
| Semaphore.Semaphore
| undefined
semaphore) yield* const semaphore: Semaphore.Semaphoreconst semaphore: {
resize: (this: Semaphore, permits: number) => Effect.Effect<void>;
withPermits: (this: Semaphore, permits: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
withPermit: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
withPermitsIfAvailable: (this: Semaphore, permits: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>;
take: (this: Semaphore, permits: number) => Effect.Effect<number>;
release: (this: Semaphore, permits: number) => Effect.Effect<number>;
releaseAll: Effect.Effect<number>;
}
semaphore.Semaphore.take(this: Semaphore, permits: number): Effect.Effect<number>Acquires the specified number of permits and returns the resulting
available permits, suspending the task if they are not yet available.
Concurrent pending take calls are processed in a first-in, first-out manner.
When to use
Use to manually acquire permits for lower-level coordination protocols.
take(1)
const const channel: Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>
const channel: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
channel = yield* const pull: Pull.Pull<
Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>,
OutErr,
OutDone,
never
>
const pull: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
pull
const const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope = import ScopeScope.const forkUnsafe: (
scope: Scope,
finalizerStrategy?: "sequential" | "parallel"
) => Closeable
Creates a closeable child scope synchronously and registers it with a parent scope.
When to use
Use when a child scope must be created synchronously and the caller controls
both parent and child scope lifetimes.
Details
Closing the parent closes the child with the same exit value, and closing the
child detaches it from the parent. The optional finalizer strategy configures
the child scope and defaults to "sequential" when omitted.
Example (Creating a child scope synchronously)
import { Console, Effect, Exit, Scope } from "effect"
const program = Effect.gen(function*() {
const parentScope = Scope.makeUnsafe("sequential")
const childScope = Scope.forkUnsafe(parentScope, "parallel")
// Add finalizers to both scopes
yield* Scope.addFinalizer(parentScope, Console.log("Parent cleanup"))
yield* Scope.addFinalizer(childScope, Console.log("Child cleanup"))
// Close child first, then parent
yield* Scope.close(childScope, Exit.void)
yield* Scope.close(parentScope, Exit.void)
})
forkUnsafe(forkedScope: Scope.Scope(parameter) forkedScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
forkedScope)
const const childPull: Pull.Pull<
OutElem,
OutErr1,
OutDone1,
never
>
const childPull: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
childPull = yield* const toTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>(
channel: Channel<
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
Env
>
) => (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone>,
never,
Env
>
Converts a Channel back to its underlying transformation function.
Example (Extracting channel transforms)
import { Channel } from "effect"
const channel = Channel.succeed(42)
const transform = Channel.toTransform(channel)
// transform can now be used directly
toTransform(const channel: Channel<
OutElem,
OutErr1,
OutDone1,
InElem1,
InErr1,
InDone1,
Env1
>
const channel: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
channel)(upstream: Pull.Pull<
InElem & InElem1,
InErr & InErr1,
InDone & InDone1,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope)
while (const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<T>.size: numbersize >= const concurrencyN: numberconcurrencyN) {
const const fiber: Fiber.Fiber<any, any>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber = import IterableIterable.const headUnsafe: <Fiber.Fiber<any, any>>(self: Iterable<Fiber.Fiber<any, any>>) => Fiber.Fiber<any, any>Gets the first element of an Iterable without returning an Option.
When to use
Use when the Iterable is known to be non-empty and direct access to the
first element is preferred over handling Option.none.
Gotchas
Throws if the Iterable is empty.
Example (Getting the first element unsafely)
import { Iterable } from "effect"
const numbers = [1, 2, 3]
console.log(Iterable.headUnsafe(numbers)) // 1
const letters = "hello"
console.log(Iterable.headUnsafe(letters)) // "h"
// Iterable.headUnsafe(Iterable.empty<number>())
// throws Error: "headUnsafe: empty iterable"
// Use only when you're certain the iterable is non-empty
const nonEmpty = Iterable.range(1, 10)
console.log(Iterable.headUnsafe(nonEmpty)) // 1
headUnsafe(const fibers: Set<Fiber.Fiber<any, any>>fibers)
const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<Fiber<any, any>>.delete(value: Fiber.Fiber<any, any>): booleanRemoves a specified value from the Set.
delete(const fiber: Fiber.Fiber<any, any>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber)
if (const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<T>.size: numbersize === 0) yield* const doneLatch: Latch.Latchconst doneLatch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
doneLatch.Latch.open: Effect.Effect<boolean>(property) Latch.open: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Opens the latch, releasing all fibers waiting on it.
When to use
Use to let current and future waiters continue.
open
yield* import FiberFiber.const interrupt: <A, E>(
self: Fiber<A, E>
) => Effect<void>
Interrupts a fiber, causing it to stop executing and clean up any
acquired resources.
When to use
Use when you need to cancel a forked fiber and wait for its cleanup to
complete.
Details
The returned Effect completes only after the interrupted fiber has completed.
Gotchas
Interruption is cooperative. A fiber can continue running while it is inside
uninterruptible work or finalizers.
Example (Interrupting a fiber)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
const fiber = yield* Effect.forkChild(
Effect.delay("1 second")(Effect.succeed(42))
)
yield* Fiber.interrupt(fiber)
console.log("Fiber interrupted")
})
interrupt(const fiber: Fiber.Fiber<any, any>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber)
}
const const fiber: Fiber.Fiber<
never,
OutErr1 | Cause.Done<OutDone1>
>
const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber = yield* const childPull: Pull.Pull<
OutElem,
OutErr1,
OutDone1,
never
>
const childPull: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
childPull.Pipeable.pipe<Pull.Pull<OutElem, OutErr1, OutDone1, never>, Effect.Effect<OutElem, any, never>, Effect.Effect<boolean, any, never>, Effect.Effect<never, any, never>, Effect.Effect<never, any, never>, Effect.Effect<Fiber.Fiber<never, any>, never, never>>(this: Pull.Pull<...>, ab: (_: Pull.Pull<OutElem, OutErr1, OutDone1, never>) => Effect.Effect<OutElem, any, never>, bc: (_: Effect.Effect<OutElem, any, never>) => Effect.Effect<...>, cd: (_: Effect.Effect<...>) => Effect.Effect<...>, de: (_: Effect.Effect<...>) => Effect.Effect<...>, ef: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)pipe(
import EffectEffect.const tap: {
<A, B, E2, R2>(
f: (a: NoInfer<A>) => Effect<B, E2, R2>
): <E, R>(
self: Effect<A, E, R>
) => Effect<A, E | E2, R | R2>
<B, E2, R2>(f: Effect<B, E2, R2>): <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
f: (a: NoInfer<A>) => Effect<B, E2, R2>
): Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
f: Effect<B, E2, R2>
): Effect<A, E | E2, R | R2>
}
tap(() => import EffectEffect.const yieldNow: Effect<void>const yieldNow: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Yields control back to the Effect runtime, allowing other fibers to execute.
Example (Yielding to other fibers)
import { Effect } from "effect"
const program = Effect.gen(function*() {
console.log("Before yield")
yield* Effect.yieldNow
console.log("After yield")
})
Effect.runPromise(program)
yieldNow),
import EffectEffect.const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
}
flatMap((value: OutElemvalue) => import QueueQueue.const offer: <A, E>(
self: Enqueue<A, E>,
message: Types.NoInfer<A>
) => Effect<boolean>
Adds a message to the queue. Returns false if the queue is done.
Details
For bounded queues, this operation may suspend if the queue is at capacity,
depending on the backpressure strategy. For dropping/sliding queues, it may
return false or succeed immediately by dropping/sliding existing messages.
Example (Offering a value)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(3)
// Successfully add messages to queue
const success1 = yield* Queue.offer(queue, 1)
const success2 = yield* Queue.offer(queue, 2)
console.log(success1, success2) // true, true
// Queue state
const size = yield* Queue.size(queue)
console.log(size) // 2
})
offer(const queue: Queue.Queue<
OutElem,
OutErr1 | OutErr | Cause.Done<OutDone>
>
const 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, value: OutElemvalue)),
import EffectEffect.const forever: <
Arg extends
| Effect<any, any, any>
| {
readonly disableYield?:
| boolean
| undefined
}
| undefined = {
readonly disableYield?: boolean | undefined
}
>(
effectOrOptions?: Arg,
options?:
| {
readonly disableYield?:
| boolean
| undefined
}
| undefined
) => [Arg] extends [
Effect<infer _A, infer _E, infer _R>
]
? Effect<never, _E, _R>
: <A, E, R>(
self: Effect<A, E, R>
) => Effect<never, E, R>
Repeats this effect forever (until the first error).
Example (Repeating forever)
import { Console, Effect, Fiber } from "effect"
const task = Effect.gen(function*() {
yield* Console.log("Task running...")
yield* Effect.sleep("1 second")
})
// This will run forever, printing every second
const program = task.pipe(Effect.forever)
// This will run forever, without yielding every iteration
const programNoYield = task.pipe(Effect.forever({ disableYield: true }))
// Run for 5 seconds then interrupt
const timedProgram = Effect.gen(function*() {
const fiber = yield* Effect.forkChild(program)
yield* Effect.sleep("5 seconds")
yield* Fiber.interrupt(fiber)
})
forever({ disableYield: truedisableYield: true }),
import EffectEffect.const onError: {
<E, X, R2>(
cleanup: (
cause: Cause.Cause<E>
) => Effect<X, never, R2>
): <A, R>(
self: Effect<A, E, R>
) => Effect<A, E, R2 | R>
<A, E, R, X, R2>(
self: Effect<A, E, R>,
cleanup: (
cause: Cause.Cause<E>
) => Effect<X, never, R2>
): Effect<A, E, R2 | R>
}
onError(import EffectEffect.const fnUntraced: <Effect.Effect<boolean, never, never> | Effect.Effect<number, never, never> | Effect.Effect<Exit.Exit<void, never>, never, never>, boolean | undefined, [cause: Cause.Cause<E>]>(body: (this: Types.unassigned, cause: Cause.Cause<E>) => Generator<Effect.Effect<boolean, never, never> | Effect.Effect<number, never, never> | Effect.Effect<Exit.Exit<void, never>, never, never>, boolean | undefined, never>) => (cause: Cause.Cause<E>) => Effect.Effect<boolean | undefined, never, never> (+41 overloads)fnUntraced(function*(cause: Cause.Cause<
OutErr1 | Cause.Done<OutDone1>
>
(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause) {
const const halt: Result.Result<
Cause.Done<OutDone1> | Cause.Done.Only<OutErr1>,
Cause.Cause<Exclude<OutErr1, Cause.Done<any>>>
>
halt = import PullPull.const filterDone: <E>(
input: Cause.Cause<E>
) => Result.Result<
Cause.Done.Only<E>,
Cause.Cause<ExcludeDone<E>>
>
Finds a Cause.Done failure in a Cause.
When to use
Use to separate Cause.Done completion from ordinary causes while preserving
the typed done value.
Details
Returns a successful Result with the Cause.Done value when one is
present, otherwise returns a failed Result containing the non-done cause.
filterDone(cause: Cause.Cause<
OutErr1 | Cause.Done<OutDone1>
>
(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause)
yield* import EffectEffect.const exit: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Exit.Exit<A, E>, never, R>
Transforms an effect to encapsulate both failure and success using the Exit
data type.
When to use
Use when you need to inspect the full outcome, including typed failures, defects,
and interruptions.
Details
exit wraps an effect's success or failure inside an Exit type, allowing
you to handle both cases explicitly.
The resulting effect cannot fail because the failure is encapsulated within
the Exit.Failure type. The error type is set to never, indicating that
the effect is structured to never fail directly.
Example (Capturing completion as Exit)
import { Effect } from "effect"
const success = Effect.succeed(42)
const failure = Effect.fail("Something went wrong")
const program1 = Effect.exit(success)
const program2 = Effect.exit(failure)
Effect.runPromise(program1).then(console.log)
// { _id: 'Exit', _tag: 'Success', value: 42 }
Effect.runPromise(program2).then(console.log)
// { _id: 'Exit', _tag: 'Failure', cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong' } }
exit(import ScopeScope.const close: <A, E>(
self: Scope,
exit: Exit<A, E>
) => Effect<void>
Closes a scope and runs its registered finalizers.
When to use
Use to close a scope manually with a specific exit value.
Details
Finalizers run in the scope's configured order and receive the supplied
Exit.
Example (Running scope finalizers)
import { Console, Effect, Exit, Scope } from "effect"
const resourceManagement = Effect.gen(function*() {
const scope = yield* Scope.make("sequential")
// Add multiple finalizers
yield* Scope.addFinalizer(scope, Console.log("Close database connection"))
yield* Scope.addFinalizer(scope, Console.log("Close file handle"))
yield* Scope.addFinalizer(scope, Console.log("Release memory"))
// Do some work...
yield* Console.log("Performing operations...")
// Close scope - finalizers run in reverse order of registration
yield* Scope.close(scope, Exit.succeed("Success!"))
// Output: "Release memory", "Close file handle", "Close database connection"
})
close(
const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope,
!import ResultResult.const isFailure: <A, E>(
self: Result<A, E>
) => self is Failure<A, E>
Checks whether a Result is a Failure.
When to use
Use to narrow a known Result to the Failure variant.
Details
- Acts as a TypeScript type guard, narrowing to
Failure<A, E>
- After narrowing, you can access
.failure to read the error value
Example (Narrowing to failure)
import { Result } from "effect"
const result = Result.fail("oops")
if (Result.isFailure(result)) {
console.log(result.failure)
// Output: "oops"
}
isFailure(const halt: Result.Result<
Cause.Done<OutDone1> | Cause.Done.Only<OutErr1>,
Cause.Cause<Exclude<OutErr1, Cause.Done<any>>>
>
halt) ? import ExitExit.const succeed: <A>(a: A) => Exit<A>Creates a successful Exit containing the given value.
When to use
Use when you need an Exit that contains a known success value.
Details
Returns a Success<A> with the provided value. Does not perform any
computation.
Example (Creating a successful Exit)
import { Exit } from "effect"
const exit = Exit.succeed(42)
console.log(Exit.isSuccess(exit)) // true
succeed(const halt: Result.Success<
Cause.Done<OutDone1> | Cause.Done.Only<OutErr1>,
Cause.Cause<Exclude<OutErr1, Cause.Done<any>>>
>
const halt: {
_tag: "Success";
_op: "Success";
success: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
halt.Success<Cause.Done.Only<E>, Cause.Cause<Exclude<E, Cause.Done<any>>>>.success: Cause.Done.Only<E>success.value) : import ExitExit.const failCause: <E>(
cause: Cause.Cause<E>
) => Exit<never, E>
Creates a failed Exit from a Cause.
When to use
Use when you already have a Cause<E> and want to wrap it in an Exit
for advanced error handling where you need full control over the Cause
structure.
Details
Returns a Failure<never, E>. If you only have an error value, use
fail
instead.
Example (Creating a failed Exit from a Cause)
import { Cause, Exit } from "effect"
const cause = Cause.fail("Something went wrong")
const exit = Exit.failCause(cause)
console.log(Exit.isFailure(exit)) // true
failCause(const halt: Result.Failure<
Cause.Done<OutDone1> | Cause.Done.Only<OutErr1>,
Cause.Cause<Exclude<OutErr1, Cause.Done<any>>>
>
const halt: {
_tag: "Failure";
_op: "Failure";
failure: E;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
halt.Failure<Cause.Done.Only<E>, Cause.Cause<Exclude<E, Cause.Done<any>>>>.failure: Cause.Cause<Exclude<E, Cause.Done<any>>>(property) Failure<Cause.Done.Only<E>, Cause.Cause<Exclude<E, Cause.Done<any>>>>.failure: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
failure)
))
if (!const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<Fiber<any, any>>.has(value: Fiber.Fiber<any, any>): booleanhas(const fiber: Fiber.Fiber<
never,
OutErr1 | Cause.Done<OutDone1>
>
const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber)) return
const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<Fiber<any, any>>.delete(value: Fiber.Fiber<any, any>): booleanRemoves a specified value from the Set.
delete(const fiber: Fiber.Fiber<
never,
OutErr1 | Cause.Done<OutDone1>
>
const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber)
if (const semaphore:
| Semaphore.Semaphore
| undefined
semaphore) yield* const semaphore: Semaphore.Semaphoreconst semaphore: {
resize: (this: Semaphore, permits: number) => Effect.Effect<void>;
withPermits: (this: Semaphore, permits: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
withPermit: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
withPermitsIfAvailable: (this: Semaphore, permits: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>;
take: (this: Semaphore, permits: number) => Effect.Effect<number>;
release: (this: Semaphore, permits: number) => Effect.Effect<number>;
releaseAll: Effect.Effect<number>;
}
semaphore.Semaphore.release(this: Semaphore, permits: number): Effect.Effect<number>Releases the specified number of permits and returns the resulting
available permits.
When to use
Use to manually return permits acquired by a lower-level coordination
protocol.
release(1)
if (const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<T>.size: numbersize === 0) yield* const doneLatch: Latch.Latchconst doneLatch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
doneLatch.Latch.open: Effect.Effect<boolean>(property) Latch.open: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Opens the latch, releasing all fibers waiting on it.
When to use
Use to let current and future waiters continue.
open
if (import ResultResult.const isSuccess: <A, E>(
self: Result<A, E>
) => self is Success<A, E>
Checks whether a Result is a Success.
When to use
Use to narrow a known Result to the Success variant.
Details
- Acts as a TypeScript type guard, narrowing to
Success<A, E>
- After narrowing, you can access
.success to read the value
Example (Narrowing to success)
import { Result } from "effect"
const result = Result.succeed(42)
if (Result.isSuccess(result)) {
console.log(result.success)
// Output: 42
}
isSuccess(const halt: Result.Result<
Cause.Done<OutDone1> | Cause.Done.Only<OutErr1>,
Cause.Cause<Exclude<OutErr1, Cause.Done<any>>>
>
halt)) return
return yield* import QueueQueue.const failCause: {
<E>(cause: Cause<E>): <A>(
self: Enqueue<A, E>
) => Effect<boolean>
<A, E>(
self: Enqueue<A, E>,
cause: Cause<E>
): Effect<boolean>
}
failCause(const queue: Queue.Queue<
OutElem,
OutErr1 | OutErr | Cause.Done<OutDone>
>
const 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, cause: Cause.Cause<
OutErr1 | Cause.Done<OutDone1>
>
(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause as any)
})),
import EffectEffect.const forkChild: <
Arg extends
| Effect<any, any, any>
| {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
| undefined = {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
>(
effectOrOptions?: Arg,
options?:
| {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
| undefined
) => [Arg] extends [
Effect<infer _A, infer _E, infer _R>
]
? Effect<Fiber<_A, _E>, never, _R>
: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Fiber<A, E>, never, R>
Returns an effect that forks this effect into its own separate fiber,
returning the fiber immediately, without waiting for it to begin executing
the effect.
Details
You can use the forkChild method whenever you want to execute an effect in a
new fiber, concurrently and without "blocking" the fiber executing other
effects. Using fibers can be tricky, so instead of using this method
directly, consider other higher-level methods, such as raceWith,
zipPar, and so forth.
The fiber returned by this method has methods to interrupt the fiber and to
wait for it to finish executing the effect. See Fiber for more
information.
Whenever you use this method to launch a new fiber, the new fiber is
attached to the parent fiber's scope. This means when the parent fiber
terminates, the child fiber will be terminated as well, ensuring that no
fibers leak. This behavior is called "auto supervision", and if this
behavior is not desired, you may use the forkDetach or forkIn methods.
Example (Forking a child fiber)
import { Effect, Fiber } from "effect"
const longRunningTask = Effect.gen(function*() {
yield* Effect.sleep("2 seconds")
yield* Effect.log("Task completed")
return "result"
})
const program = Effect.gen(function*() {
const fiber = yield* longRunningTask.pipe(Effect.forkChild)
// or fork a fiber that starts immediately:
yield* longRunningTask.pipe(Effect.forkChild({ startImmediately: true }))
yield* Effect.log("Task forked, continuing...")
const result = yield* Fiber.join(fiber)
return result
})
forkChild
)
const doneLatch: Latch.Latchconst doneLatch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
doneLatch.Latch.closeUnsafe(this: Latch): booleanCloses the latch synchronously so future waiters suspend again.
When to use
Use when synchronous code must close the latch immediately.
closeUnsafe()
const fibers: Set<Fiber.Fiber<any, any>>fibers.Set<Fiber<any, any>>.add(value: Fiber.Fiber<any, any>): Set<Fiber.Fiber<any, any>>Appends a new element with a specified value to the end of the Set.
add(const fiber: Fiber.Fiber<
never,
OutErr1 | Cause.Done<OutDone1>
>
const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
fiber)
}
}).Pipeable.pipe<Effect.Effect<never, any, Env1>, Effect.Effect<boolean, never, Env1>, Effect.Effect<Fiber.Fiber<boolean, never>, never, Env1>>(this: Effect.Effect<never, any, Env1>, ab: (_: Effect.Effect<never, any, Env1>) => Effect.Effect<boolean, never, Env1>, bc: (_: Effect.Effect<boolean, never, Env1>) => Effect.Effect<Fiber.Fiber<boolean, never>, never, Env1>): Effect.Effect<Fiber.Fiber<boolean, never>, never, Env1> (+21 overloads)pipe(
import EffectEffect.const catchCause: {
<E, A2, E2, R2>(
f: (
cause: Cause.Cause<E>
) => Effect<A2, E2, R2>
): <A, R>(
self: Effect<A, E, R>
) => Effect<A2 | A, E2, R2 | R>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
f: (
cause: Cause.Cause<E>
) => Effect<A2, E2, R2>
): Effect<A | A2, E2, R | R2>
}
catchCause((cause: Cause.Cause<
OutErr | Cause.Done<OutDone>
>
(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause) => const doneLatch: Latch.Latchconst doneLatch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
doneLatch.Latch.whenOpen<A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>Runs the given effect only after the latch allows waiting fibers to
continue.
When to use
Use to gate an effect behind the latch signal.
whenOpen(import QueueQueue.const failCause: {
<E>(cause: Cause<E>): <A>(
self: Enqueue<A, E>
) => Effect<boolean>
<A, E>(
self: Enqueue<A, E>,
cause: Cause<E>
): Effect<boolean>
}
failCause(const queue: Queue.Queue<
OutElem,
OutErr1 | OutErr | Cause.Done<OutDone>
>
const 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, cause: Cause.Cause<
OutErr | Cause.Done<OutDone>
>
(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause))),
import EffectEffect.const forkIn: {
(
scope: Scope,
options?: {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
): <A, E, R>(
self: Effect<A, E, R>
) => Effect<Fiber<A, E>, never, R>
<A, E, R>(
self: Effect<A, E, R>,
scope: Scope,
options?: {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
): Effect<Fiber<A, E>, never, R>
}
forkIn(forkedScope: Scope.Scope(parameter) forkedScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
forkedScope)
)
return import QueueQueue.const take: <A, E>(
self: Dequeue<A, E>
) => Effect<A, E>
Takes a single message from the queue, or wait for a message to be
available.
Details
If the queue is done, it will fail with Done. If the
queue fails, the Effect will fail with the error.
Example (Taking one value)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<string, Cause.Done>(3)
// Add some messages
yield* Queue.offer(queue, "first")
yield* Queue.offer(queue, "second")
// Take messages one by one
const msg1 = yield* Queue.take(queue)
const msg2 = yield* Queue.take(queue)
console.log(msg1, msg2) // "first", "second"
// End the queue
yield* Queue.end(queue)
// Taking from an ended queue fails with Done
const result = yield* Effect.match(Queue.take(queue), {
onFailure: (error: Cause.Done) => true,
onSuccess: (value: string) => false
})
console.log("Queue ended:", result) // true
})
take(const queue: Queue.Queue<
OutElem,
OutErr1 | OutErr | Cause.Done<OutDone>
>
const 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)
})
)
)