<L, In, E, A, R>(
channel: Channel.Channel<
never,
E,
End<A, L>,
NonEmptyReadonlyArray<In>,
never,
void,
R
>
): Sink<A, In, L, E, R>Creates a sink from a Channel.
When to use
Use to create a Sink from a Channel that processes non-empty arrays of
input values.
export const const fromChannel: <L, In, E, A, R>(
channel: Channel.Channel<
never,
E,
End<A, L>,
NonEmptyReadonlyArray<In>,
never,
void,
R
>
) => Sink<A, In, L, E, R>
Creates a sink from a Channel.
When to use
Use to create a Sink from a Channel that processes non-empty arrays of
input values.
fromChannel = <function (type parameter) L in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>L, function (type parameter) In in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>In, function (type parameter) E in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>E, function (type parameter) A in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>A, function (type parameter) R in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>R>(
channel: Channel.Channel<
never,
E,
End<A, L>,
NonEmptyReadonlyArray<In>,
never,
void,
R
>
(parameter) 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: import ChannelChannel.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<
never,
function (type parameter) E in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>E,
type End<A, L = never> = readonly [
value: A,
leftover?: readonly [L, ...L[]] | undefined
]
Tuple returned when a Sink finishes.
Details
The first element is the sink result. The optional second element contains a
non-empty array of leftover input that was pulled but not consumed.
End<function (type parameter) A in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>A, function (type parameter) L in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>L>,
type NonEmptyReadonlyArray<A> = readonly [A, ...A[]]A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<function (type parameter) In in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>In>,
never,
void,
function (type parameter) R in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>R
>
): interface Sink<out A, in In = unknown, out L = never, out E = never, out R = never>A Sink<A, In, L, E, R> is used to consume elements produced by a Stream.
You can think of a sink as a function that will consume a variable amount of
In elements (could be 0, 1, or many), might fail with an error of type E,
and will eventually yield a value of type A together with a remainder of
type L (i.e. any leftovers).
Example (Running a sink with a stream)
import { Effect, Sink, Stream } from "effect"
// Create a simple sink that always succeeds with a value
const sink: Sink.Sink<number> = Sink.succeed(42)
// Use the sink to consume a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program).then(console.log)
// Output: 42
Namespace containing types and interfaces for Sink variance and type relationships.
Sink<function (type parameter) A in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>A, function (type parameter) In in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>In, function (type parameter) L in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>L, function (type parameter) E in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>E, function (type parameter) R in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>R> =>
const fromTransform: <
In,
A,
E,
R,
L = never
>(
transform: (
upstream: Pull.Pull<
NonEmptyReadonlyArray<In>,
never,
void
>,
scope: Scope.Scope
) => Effect.Effect<End<A, L>, E, R>
) => Sink<A, In, L, E, R>
Creates a Sink from a low-level transform function.
Details
The transform receives the upstream pull of non-empty input arrays and the
active scope, and returns an effect that completes with the sink's End
value.
fromTransform((upstream: Pull.Pull<
readonly [In, ...In[]],
never,
void,
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) =>
import ChannelChannel.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(channel: Channel.Channel<
never,
E,
End<A, L>,
NonEmptyReadonlyArray<In>,
never,
void,
R
>
(parameter) 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<
readonly [In, ...In[]],
never,
void,
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).Pipeable.pipe<Effect.Effect<Pull.Pull<never, E, End<A, L>, never>, never, R>, Effect.Effect<never, E | Cause.Done<End<A, L>>, R>, Effect.Effect<End<A, L> | Cause.Done<A = void>.Extract<E>, Exclude<E, Cause.Done<any>>, R>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<Pull.Pull<never, E, End<A, L>, never>, never, R>) => Effect.Effect<never, E | Cause.Done<End<A, L>>, R>, bc: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)pipe(
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(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 PullPull.const catchDone: {
<E, A2, E2, R2>(
f: (
leftover: Cause.Done.Extract<E>
) => Effect<A2, E2, R2>
): <A, R>(
self: Effect<A, E, R>
) => Effect<A | A2, ExcludeDone<E> | E2, R | R2>
<A, R, E, A2, E2, R2>(
self: Effect<A, E, R>,
f: (
leftover: Cause.Done.Extract<E>
) => Effect<A2, E2, R2>
): Effect<A | A2, ExcludeDone<E> | E2, R | R2>
}
catchDone(import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed)
) as 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<type End<A, L = never> = readonly [
value: A,
leftover?: readonly [L, ...L[]] | undefined
]
Tuple returned when a Sink finishes.
Details
The first element is the sink result. The optional second element contains a
non-empty array of leftover input that was pulled but not consumed.
End<function (type parameter) A in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>A, function (type parameter) L in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>L>, function (type parameter) E in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>E, function (type parameter) R in <L, In, E, A, R>(channel: Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>): Sink<A, In, L, E, R>R>
)