<OutElem>(
pubsub: PubSub.PubSub<OutElem>,
options?: { readonly shutdownOnEnd?: boolean | undefined } | undefined
): <OutErr, OutDone, Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
) => Effect.Effect<OutDone, OutErr, Env>
<OutElem, OutErr, OutDone, Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>,
pubsub: PubSub.PubSub<OutElem>,
options?: { readonly shutdownOnEnd?: boolean | undefined } | undefined
): Effect.Effect<OutDone, OutErr, Env>Runs an array-emitting channel and publishes each array element to a
PubSub.
Details
Each element inside emitted non-empty arrays is published as an individual
PubSub message. Use options.shutdownOnEnd to shut down the PubSub when
channel execution ends.
export const const runIntoPubSubArray: {
<OutElem>(
pubsub: PubSub.PubSub<OutElem>,
options?:
| {
readonly shutdownOnEnd?:
| boolean
| undefined
}
| undefined
): <OutErr, OutDone, Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
) => Effect.Effect<OutDone, OutErr, Env>
<OutElem, OutErr, OutDone, Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>,
pubsub: PubSub.PubSub<OutElem>,
options?:
| {
readonly shutdownOnEnd?:
| boolean
| undefined
}
| undefined
): Effect.Effect<OutDone, OutErr, Env>
}
Runs an array-emitting channel and publishes each array element to a
PubSub.
Details
Each element inside emitted non-empty arrays is published as an individual
PubSub message. Use options.shutdownOnEnd to shut down the PubSub when
channel execution ends.
runIntoPubSubArray: {
<function (type parameter) OutElem in <OutElem>(pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>) => Effect.Effect<OutDone, OutErr, Env>
OutElem>(
pubsub: PubSub.PubSub<OutElem>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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; <…;
}
pubsub: import PubSubPubSub.interface PubSub<in out A>A PubSub<A> is an asynchronous message hub into which publishers can publish
messages of type A and subscribers can subscribe to take messages of type
A.
Example (Publishing and subscribing to messages)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
// Create a bounded PubSub with capacity 10
const pubsub = yield* PubSub.bounded<string>(10)
// Subscribe and consume messages
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
// Publish messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
const message1 = yield* PubSub.take(subscription)
const message2 = yield* PubSub.take(subscription)
console.log(message1, message2) // "Hello", "World"
}))
})
Companion namespace containing the low-level building blocks used by
PubSub, including atomic implementations, backing subscriptions, replay
windows, and delivery strategies.
PubSub<function (type parameter) OutElem in <OutElem>(pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>) => Effect.Effect<OutDone, OutErr, Env>
OutElem>,
options: | {
readonly shutdownOnEnd?: boolean | undefined
}
| undefined
options?: {
readonly shutdownOnEnd?: boolean | undefinedshutdownOnEnd?: boolean | undefined
} | undefined
): <function (type parameter) OutErr in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>OutErr, function (type parameter) OutDone in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>OutDone, function (type parameter) Env in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
(parameter) self: {
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; <…;
}
self: interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<function (type parameter) OutElem in <OutElem>(pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>) => Effect.Effect<OutDone, OutErr, Env>
OutElem>, function (type parameter) OutErr in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>OutErr, function (type parameter) OutDone in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>OutDone, unknown, unknown, unknown, function (type parameter) Env in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>Env>
) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) OutDone in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>OutDone, function (type parameter) OutErr in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>OutErr, function (type parameter) Env in <OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>): Effect.Effect<OutDone, OutErr, Env>Env>
<function (type parameter) OutElem in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutElem, function (type parameter) OutErr in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutErr, function (type parameter) OutDone in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutDone, function (type parameter) Env in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
(parameter) self: {
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; <…;
}
self: interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<function (type parameter) OutElem in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutElem>, function (type parameter) OutErr in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutErr, function (type parameter) OutDone in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutDone, unknown, unknown, unknown, function (type parameter) Env in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
Env>,
pubsub: PubSub.PubSub<OutElem>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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; <…;
}
pubsub: import PubSubPubSub.interface PubSub<in out A>A PubSub<A> is an asynchronous message hub into which publishers can publish
messages of type A and subscribers can subscribe to take messages of type
A.
Example (Publishing and subscribing to messages)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
// Create a bounded PubSub with capacity 10
const pubsub = yield* PubSub.bounded<string>(10)
// Subscribe and consume messages
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
// Publish messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
const message1 = yield* PubSub.take(subscription)
const message2 = yield* PubSub.take(subscription)
console.log(message1, message2) // "Hello", "World"
}))
})
Companion namespace containing the low-level building blocks used by
PubSub, including atomic implementations, backing subscriptions, replay
windows, and delivery strategies.
PubSub<function (type parameter) OutElem in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutElem>,
options: | {
readonly shutdownOnEnd?: boolean | undefined
}
| undefined
options?: {
readonly shutdownOnEnd?: boolean | undefinedshutdownOnEnd?: boolean | undefined
} | undefined
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) OutDone in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutDone, function (type parameter) OutErr in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
OutErr, function (type parameter) Env in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<OutDone, OutErr, Env>
Env>
} = dual<(...args: Array<any>) => any, <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined) => Effect.Effect<OutDone, OutErr, Env>>(isDataFirst: (args: IArguments) => boolean, body: <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined) => Effect.Effect<OutDone, OutErr, Env>): ((...args: Array<any>) => any) & (<OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined) => Effect.Effect<OutDone, OutErr, 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(
(args: IArgumentsargs) => const isChannel: (
u: unknown
) => u is Channel<
unknown,
unknown,
unknown,
unknown,
unknown,
unknown,
unknown
>
Checks whether a value is a Channel.
Example (Checking for channels)
import { Channel } from "effect"
const channel = Channel.succeed(42)
console.log(Channel.isChannel(channel)) // true
console.log(Channel.isChannel("not a channel")) // false
isChannel(args: IArgumentsargs[0]),
<function (type parameter) OutElem in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutElem, function (type parameter) OutErr in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutErr, function (type parameter) OutDone in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutDone, function (type parameter) Env in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
Env>(
self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
(parameter) self: {
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; <…;
}
self: interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<function (type parameter) OutElem in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutElem>, function (type parameter) OutErr in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutErr, function (type parameter) OutDone in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutDone, unknown, unknown, unknown, function (type parameter) Env in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
Env>,
pubsub: PubSub.PubSub<OutElem>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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; <…;
}
pubsub: import PubSubPubSub.interface PubSub<in out A>A PubSub<A> is an asynchronous message hub into which publishers can publish
messages of type A and subscribers can subscribe to take messages of type
A.
Example (Publishing and subscribing to messages)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
// Create a bounded PubSub with capacity 10
const pubsub = yield* PubSub.bounded<string>(10)
// Subscribe and consume messages
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
// Publish messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
const message1 = yield* PubSub.take(subscription)
const message2 = yield* PubSub.take(subscription)
console.log(message1, message2) // "Hello", "World"
}))
})
Companion namespace containing the low-level building blocks used by
PubSub, including atomic implementations, backing subscriptions, replay
windows, and delivery strategies.
PubSub<function (type parameter) OutElem in <OutElem, OutErr, OutDone, Env>(self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, unknown, unknown, unknown, Env>, pubsub: PubSub.PubSub<OutElem>, options?: {
readonly shutdownOnEnd?: boolean | undefined;
} | undefined): Effect.Effect<...>
OutElem>,
options: | {
readonly shutdownOnEnd?: boolean | undefined
}
| undefined
options?: {
readonly shutdownOnEnd?: boolean | undefinedshutdownOnEnd?: boolean | undefined
} | undefined
) =>
const runForEach: {
<OutElem, EX, RX>(
f: (o: OutElem) => Effect.Effect<void, EX, RX>
): <OutErr, OutDone, Env>(
self: Channel<
OutElem,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
) => Effect.Effect<
OutDone,
OutErr | EX,
Env | RX
>
<OutElem, OutErr, OutDone, Env, EX, RX>(
self: Channel<
OutElem,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>,
f: (o: OutElem) => Effect.Effect<void, EX, RX>
): Effect.Effect<OutDone, OutErr | EX, Env | RX>
}
runForEach(self: Channel<
Arr.NonEmptyReadonlyArray<OutElem>,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
(parameter) self: {
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; <…;
}
self, (value: Arr.NonEmptyReadonlyArray<OutElem>(parameter) value: {
0: OutElem;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<OutElem>>): Array<OutElem>; (...items: Array<OutElem | ConcatArray<OutElem>>): Array<OutElem> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<OutElem>;
indexOf: (searchElement: OutElem, fromIndex?: number) => number;
lastIndexOf: (searchElement: OutElem, fromIndex?: number) => number;
every: { (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any): boolea…;
some: (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => void, thisArg?: any) => void;
map: (callbackfn: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => value is S, thisArg?: any): Array<S>; (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any): Array<OutElem> };
reduce: { (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArray<OutElem>) => OutElem): OutElem; (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArr…;
reduceRight: { (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArray<OutElem>) => OutElem): OutElem; (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArr…;
find: { (predicate: (value: OutElem, index: number, obj: ReadonlyArray<OutElem>) => value is S, thisArg?: any): S | undefined; (predicate: (value: OutElem, index: number, obj: ReadonlyArray<OutElem>) => unknown, thisArg?: any): OutElem | undefin…;
findIndex: (predicate: (value: OutElem, index: number, obj: ReadonlyArray<OutElem>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, OutElem]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<OutElem>;
includes: (searchElement: OutElem, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: OutElem, index: number, array: Array<OutElem>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => OutElem | undefined;
findLast: { (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => value is S, thisArg?: any): S | undefined; (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any): OutElem | und…;
findLastIndex: (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any) => number;
toReversed: () => Array<OutElem>;
toSorted: (compareFn?: ((a: OutElem, b: OutElem) => number) | undefined) => Array<OutElem>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<OutElem>): Array<OutElem>; (start: number, deleteCount?: number): Array<OutElem> };
with: (index: number, value: OutElem) => Array<OutElem>;
}
value) => import PubSubPubSub.const publishAll: {
<A>(elements: Iterable<A>): (
self: PubSub<A>
) => Effect.Effect<boolean>
<A>(
self: PubSub<A>,
elements: Iterable<A>
): Effect.Effect<boolean>
}
publishAll(pubsub: PubSub.PubSub<OutElem>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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; <…;
}
pubsub, value: Arr.NonEmptyReadonlyArray<OutElem>(parameter) value: {
0: OutElem;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<OutElem>>): Array<OutElem>; (...items: Array<OutElem | ConcatArray<OutElem>>): Array<OutElem> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<OutElem>;
indexOf: (searchElement: OutElem, fromIndex?: number) => number;
lastIndexOf: (searchElement: OutElem, fromIndex?: number) => number;
every: { (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any): boolea…;
some: (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => void, thisArg?: any) => void;
map: (callbackfn: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => value is S, thisArg?: any): Array<S>; (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any): Array<OutElem> };
reduce: { (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArray<OutElem>) => OutElem): OutElem; (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArr…;
reduceRight: { (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArray<OutElem>) => OutElem): OutElem; (callbackfn: (previousValue: OutElem, currentValue: OutElem, currentIndex: number, array: ReadonlyArr…;
find: { (predicate: (value: OutElem, index: number, obj: ReadonlyArray<OutElem>) => value is S, thisArg?: any): S | undefined; (predicate: (value: OutElem, index: number, obj: ReadonlyArray<OutElem>) => unknown, thisArg?: any): OutElem | undefin…;
findIndex: (predicate: (value: OutElem, index: number, obj: ReadonlyArray<OutElem>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, OutElem]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<OutElem>;
includes: (searchElement: OutElem, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: OutElem, index: number, array: Array<OutElem>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => OutElem | undefined;
findLast: { (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => value is S, thisArg?: any): S | undefined; (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any): OutElem | und…;
findLastIndex: (predicate: (value: OutElem, index: number, array: ReadonlyArray<OutElem>) => unknown, thisArg?: any) => number;
toReversed: () => Array<OutElem>;
toSorted: (compareFn?: ((a: OutElem, b: OutElem) => number) | undefined) => Array<OutElem>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<OutElem>): Array<OutElem>; (start: number, deleteCount?: number): Array<OutElem> };
with: (index: number, value: OutElem) => Array<OutElem>;
}
value)).Pipeable.pipe<Effect.Effect<OutDone, OutErr, Env>, Effect.Effect<OutDone, OutErr, Env>>(this: Effect.Effect<OutDone, OutErr, Env>, ab: (_: Effect.Effect<OutDone, OutErr, Env>) => Effect.Effect<OutDone, OutErr, Env>): Effect.Effect<OutDone, OutErr, Env> (+21 overloads)pipe(
options: | {
readonly shutdownOnEnd?: boolean | undefined
}
| undefined
options?.shutdownOnEnd?: boolean | undefinedshutdownOnEnd === true ? import EffectEffect.const ensuring: {
<X, R1>(finalizer: Effect<X, never, R1>): <
A,
E,
R
>(
self: Effect<A, E, R>
) => Effect<A, E, R1 | R>
<A, E, R, X, R1>(
self: Effect<A, E, R>,
finalizer: Effect<X, never, R1>
): Effect<A, E, R1 | R>
}
ensuring(import PubSubPubSub.const shutdown: <A>(
self: PubSub<A>
) => Effect.Effect<void>
Shuts down the PubSub, interrupting suspended publishers and subscribers
and finalizing active subscriptions.
Details
After shutdown, publish and publishAll succeed with false,
publishUnsafe returns false, and subscription operations such as take
interrupt.
Example (Shutting down a PubSub)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
const pubsub = yield* PubSub.bounded<string>(1)
// Shutdown the PubSub
yield* PubSub.shutdown(pubsub)
const isShutdown = yield* PubSub.isShutdown(pubsub)
console.log("Is shutdown:", isShutdown) // true
// Publishing after shutdown returns false
const published = yield* PubSub.publish(pubsub, "msg1")
console.log("Published after shutdown:", published) // false
})
shutdown(pubsub: PubSub.PubSub<OutElem>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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; <…;
}
pubsub)) : const identity_: <A>(a: A) => AReturns its input argument unchanged.
When to use
Use to return a value unchanged where a function is required.
Example (Returning the same value)
import { identity } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(identity(5), 5)
identity_
)
)