Skip to content

Channel

Provides low-level building blocks for streaming data through Effect.

A Channel can read input elements, write output elements, fail with a typed error, and finish with a typed result while managing resources safely. Streams and sinks are built on channels, so most application code uses those higher-level modules instead. This module is useful when implementing stream operators or specialized streaming workflows.

157 exports Added in v2.0.0 Source

Accessors

contextWith

Added in v2.0.0 Source

Creates a channel from the specified services.

Signature

declare function contextWith<Env, OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2>(
  f: (context: Context<Env>) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2>,
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env | Env2>;

Buffering

buffer

Added in v2.0.0 Source

Buffers individual output elements in a queue with the configured capacity so a faster producer can progress independently of a slower consumer.

When to use

Use when output elements can be decoupled from downstream demand and the configured backpressure or loss strategy is acceptable.

Details

Finite queues use the strategy option. The default "suspend" strategy applies backpressure, while "dropping" and "sliding" can discard output elements when the queue is full. "unbounded" capacity does not use a finite capacity strategy.

Gotchas

Dropping and sliding strategies can lose output elements under backpressure.

See

Signature

declare const buffer: {
  (
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>;
};

bufferArray

Added in v4.0.0 Source

Buffers array output elements in a queue with the configured capacity so a faster producer can progress independently of a slower consumer.

When to use

Use when emitted arrays are batches of elements and it is acceptable for buffering to flatten and rebuild those batches.

Details

Finite queues use the strategy option. The default "suspend" strategy applies backpressure, while "dropping" and "sliding" can discard output elements when the queue is full. "unbounded" capacity does not use a finite capacity strategy.

Gotchas

Input arrays are offered to the queue element-by-element and outputs are rebuilt from the currently available queued elements, so upstream array boundaries are not preserved.

See

  • buffer for buffering output elements without flattening arrays

Signature

declare const bufferArray: {
  (
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>;
};

Combining

merge

Added in v4.0.0 Source

Returns a new channel, which is the merge of this channel and the specified channel.

Signature

declare const merge: {
  <OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    right: Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    options?: {
      readonly haltStrategy?: HaltStrategy;
    },
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    left: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr1 | OutErr,
    OutDone1 | OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    left: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    right: Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    options?: {
      readonly haltStrategy?: HaltStrategy;
    },
  ): Channel<
    OutElem | OutElem1,
    OutErr | OutErr1,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

mergeAll

Added in v2.0.0 Source

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.

Signature

declare const mergeAll: {
  (options: {
    readonly bufferSize?: number;
    readonly concurrency: number | "unbounded";
    readonly switch?: boolean;
  }): <
    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 bufferSize?: number;
      readonly concurrency: number | "unbounded";
      readonly switch?: boolean;
    },
  ): Channel<
    OutElem,
    OutErr1 | OutErr,
    OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
};

mergeEffect

Added in v4.0.0 Source

Runs an effect concurrently with a channel while emitting only the channel's output elements.

When to use

Use when a side effect should run for the lifetime of a channel and only the channel's output elements should be emitted.

Details

The effect's successful value is ignored. If the effect fails while the channel is running, the returned channel fails with that error.

Signature

declare const mergeEffect: {
  <X, E, R>(
    effect: Effect<X, E, R>,
  ): <OutElem, OutDone, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, X, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    effect: Effect<X, E, R>,
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

Constants

The default chunk size used by channels for batching operations.

Signature

declare const DefaultChunkSize: number;

Constructors

Acquires a resource, emits the acquired value as a single channel element, and registers release in the channel scope.

Details

The release action runs when the channel scope closes and receives the scope exit. If acquisition fails, no element is emitted and release is not registered.

Signature

declare const acquireRelease: {
  <Z>(
    release: (z: Z, e: Exit<unknown, unknown>) => Effect<unknown>,
  ): <E, R>(self: Effect<Z, E, R>) => Channel<Z, E, void, unknown, unknown, unknown, R>;
  <Z, E, R>(
    self: Effect<Z, E, R>,
    release: (z: Z, e: Exit<unknown, unknown>) => Effect<unknown>,
  ): Channel<Z, E, void, unknown, unknown, unknown, R>;
};

Acquires a resource, uses it to build a Channel, and guarantees that release runs with the channel's Exit when the channel completes, fails, or is interrupted.

Details

Acquisition is uninterruptible. If acquisition fails, use is not run and release is not registered.

Signature

declare function acquireUseRelease<A, E, R, OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  acquire: Effect<A, E, R>,
  use: (a: A) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  release: (a: A, exit: Exit<OutDone, OutErr>) => Effect<unknown>,
): Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;

callback

Added in v4.0.0 Source

Creates a Channel that interacts with a callback function using a queue.

Signature

declare function callback<A, E = never, R = never>(
  f: (queue: Queue<A, E | Done<void>>) => Effect<unknown, E, Scope | R>,
  options?: {
    readonly bufferSize?: number;
    readonly strategy?: "sliding" | "dropping" | "suspend";
  },
): Channel<A, E, void, unknown, unknown, unknown, Exclude<R, Scope>>;

Creates a Channel that interacts with a callback function using a queue, emitting arrays.

Signature

declare function callbackArray<A, E = never, R = never>(
  f: (queue: Queue<A, Done<void> | E>) => Effect<unknown, E, Scope | R>,
  options?: {
    readonly bufferSize?: number;
    readonly strategy?: "sliding" | "dropping" | "suspend";
  },
): Channel<readonly [A, A], E, void, unknown, unknown, unknown, Exclude<R, Scope>>;

die

Added in v4.0.0 Source

Constructs a channel that fails immediately with the specified defect.

Signature

declare function die(defect: unknown): Channel<never, never, never>;

Do

Added in v4.0.0 Source

The starting channel for Do notation, emitting an empty object.

Signature

declare const Do: Channel<{}>;

drain

Added in v2.0.0 Source

Creates a new channel that consumes all output from the source channel but emits nothing, preserving only the completion value.

Signature

declare function drain<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
): Channel<never, OutErr, OutDone, InElem, InErr, InDone, Env>;

empty

Added in v4.0.0 Source

Represents a Channel that emits no elements.

Signature

declare const empty: Channel<never>;

end

Added in v4.0.0 Source

Creates a Channel that immediately ends with the specified value.

Signature

declare function end<A>(value: A): Channel<never, never, A>;

endSync

Added in v4.0.0 Source

Creates a Channel that immediately ends with the lazily evaluated value.

Signature

declare function endSync<A>(evaluate: LazyArg<A>): Channel<never, never, A>;

fail

Added in v2.0.0 Source

Constructs a channel that fails immediately with the specified error.

Signature

declare function fail<E>(error: E): Channel<never, E, never>;

failCause

Added in v2.0.0 Source

Constructs a channel that fails immediately with the specified Cause.

When to use

Use when the channel failure must preserve a full Cause, such as defects, interruptions, or combined failures.

Signature

declare function failCause<E>(cause: Cause<E>): Channel<never, E, never>;

Constructs a channel that fails immediately with the specified lazily evaluated Cause.

Signature

declare function failCauseSync<E>(evaluate: LazyArg<Cause<E>>): Channel<never, E, never>;

failSync

Added in v2.0.0 Source

Constructs a channel that fails immediately with the specified lazily evaluated error.

When to use

Use when the error value should be computed each time the channel runs instead of when the channel is constructed.

Signature

declare function failSync<E>(evaluate: LazyArg<E>): Channel<never, E, never>;

flatten

Added in v2.0.0 Source

Flattens a channel of channels.

Signature

declare function flatten<
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env,
  OutErr1,
  OutDone1,
  InElem1,
  InErr1,
  InDone1,
  Env1,
>(
  channels: Channel<
    Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1
  >,
): Channel<
  OutElem,
  OutErr | OutErr1,
  OutDone1,
  InElem & InElem1,
  InErr & InErr1,
  InDone & InDone1,
  Env | Env1
>;

fromArray

Added in v4.0.0 Source

Creates a Channel that emits all elements from an array.

Signature

declare function fromArray<A>(array: readonly Array<A>): Channel<A>

Creates a channel that pulls values from an AsyncIterable.

Details

Each yielded value is emitted as an output element. The iterator's return value becomes the channel's done value. Thrown or rejected iterator errors are converted with onError. If the channel scope closes early and the iterator has a return method, that method is called.

Signature

declare function fromAsyncIterable<A, D, E>(
  iterable: AsyncIterable<A, D>,
  onError: (error: unknown) => E,
): Channel<A, E, D>;

Creates a channel from an AsyncIterable, emitting each yielded value as a single-element non-empty array.

Details

The iterator's return value becomes the channel's done value. Thrown or rejected iterator errors are converted with onError. If the channel scope closes early and the iterator has a return method, that method is called.

Signature

declare function fromAsyncIterableArray<A, D, E>(
  iterable: AsyncIterable<A, D>,
  onError: (error: unknown) => E,
): Channel<readonly [A, A], E, D>;

fromChunk

Added in v4.0.0 Source

Creates a Channel that emits all elements from a chunk.

Signature

declare function fromChunk<A>(chunk: Chunk<A>): Channel<A>;

fromEffect

Added in v2.0.0 Source

Uses an effect to write a single value to the channel.

Signature

declare function fromEffect<A, E, R>(
  effect: Effect<A, E, R>,
): Channel<A, Exclude<E, Done<any>>, void, unknown, unknown, unknown, R>;

Creates a channel that evaluates an effect and uses its successful value as the channel's done value without emitting any output elements.

Details

If the effect fails, the channel fails with the effect's error.

Signature

declare function fromEffectDone<A, E, R>(
  effect: Effect<A, E, R>,
): Channel<never, Exclude<E, Done<any>>, A, unknown, unknown, unknown, R>;

Uses an effect and discards its result.

Signature

declare function fromEffectDrain<A, E, R>(
  effect: Effect<A, E, R>,
): Channel<never, E, void, unknown, unknown, unknown, R>;

Creates a channel from an effect that produces a Take.

Details

A successful Take emits a non-empty array of output elements. A failed Take fails the channel. A done Take completes the channel with its done value.

Signature

declare function fromEffectTake<A, E, Done, E2, R>(
  effect: Effect<Take<A, E, Done>, E2, R>,
): Channel<readonly [A, A], E | E2, Done, unknown, unknown, unknown, R>;

fromIterable

Added in v4.0.0 Source

Creates a Channel that emits all elements from an iterable.

Signature

declare function fromIterable<A, L>(iterable: Iterable<A, L>): Channel<A, never, L>;

Creates a Channel that emits arrays of elements from an iterable.

Signature

declare function fromIterableArray<A, L>(
  iterable: Iterable<A, L>,
  chunkSize: number,
): Channel<readonly [A, A], never, L>;

fromIterator

Added in v4.0.0 Source

Creates a Channel from an iterator.

Signature

declare function fromIterator<A, L>(iterator: LazyArg<Iterator<A, L, any>>): Channel<A, never, L>;

Creates a Channel from an iterator that emits arrays of elements.

Signature

declare function fromIteratorArray<A, L>(
  iterator: LazyArg<Iterator<A, L, any>>,
  chunkSize: number,
): Channel<readonly [A, A], never, L>;

fromPubSub

Added in v2.0.0 Source

Creates a channel from a PubSub that outputs individual values.

Details

This constructor creates a channel that reads from a PubSub by automatically subscribing to it. The channel outputs individual values as they are published to the PubSub, making it ideal for real-time streaming scenarios.

Signature

declare function fromPubSub<A>(pubsub: PubSub<A>): Channel<A>;

Creates a channel from a PubSub that outputs arrays of values.

Details

This constructor creates a channel that reads from a PubSub by automatically subscribing to it and collecting values into arrays. The channel outputs arrays of values in chunks, making it ideal for batch processing scenarios.

Signature

declare function fromPubSubArray<A>(pubsub: PubSub<A>): Channel<readonly [A, A]>;

Subscribes to a PubSub of Take values and exposes them as a channel.

Details

Output Take values are emitted as non-empty arrays. Failed Take values fail the channel. Done Take values complete the channel.

Signature

declare function fromPubSubTake<A, E, Done>(
  pubsub: PubSub<Take<A, E, Done>>,
): Channel<readonly [A, A], E, Done>;

fromPull

Added in v4.0.0 Source

Creates a Channel from an Effect that produces a Pull.

Signature

declare function fromPull<OutElem, OutErr, OutDone, EX, EnvX, Env>(
  effect: Effect<Pull<OutElem, OutErr, OutDone, EnvX>, EX, Env>,
): Channel<
  OutElem,
  EX | Exclude<OutErr, Done<any>>,
  OutDone,
  unknown,
  unknown,
  unknown,
  EnvX | Env
>;

fromQueue

Added in v2.0.0 Source

Creates a channel from a queue.

Signature

declare function fromQueue<A, E>(queue: Dequeue<A, E>): Channel<A, Exclude<E, Done<void>>>;

Creates a channel from a queue that emits arrays of elements.

Signature

declare function fromQueueArray<A, E>(
  queue: Dequeue<A, E>,
): Channel<readonly [A, A], Exclude<E, Done<void>>>;

Creates a channel from a lazily supplied Web ReadableStream.

Signature

declare function fromReadableStream<A, E>(options: {
  readonly evaluate: LazyArg<ReadableStream<A>>;
  readonly onError: (error: unknown) => E;
  readonly releaseLockOnEnd?: boolean;
}): Channel<readonly [A, A], E>;

fromSchedule

Added in v4.0.0 Source

Creates a Channel from a Schedule.

Signature

declare function fromSchedule<O, E, R>(
  schedule: Schedule<O, unknown, E, R>,
): Channel<O, E, O, unknown, unknown, unknown, R>;

Creates a channel from a PubSub subscription.

Signature

declare function fromSubscription<A>(subscription: Subscription<A>): Channel<A>;

Creates a channel from a PubSub subscription that outputs arrays of values.

Details

This constructor creates a channel that reads from a PubSub subscription and outputs arrays of values in chunks. It's useful when you want to process multiple values at once for better performance.

Signature

declare function fromSubscriptionArray<A>(subscription: Subscription<A>): Channel<readonly [A, A]>;

Creates a Channel from a transformation function that operates on upstream pulls.

Signature

declare function fromTransform<OutElem, OutErr, OutDone, InElem, InErr, InDone, EX, EnvX, Env>(
  transform: (
    upstream: Pull<InElem, InErr, InDone>,
    scope: Scope,
  ) => Effect<Pull<OutElem, OutErr, OutDone, EnvX>, EX, Env>,
): Channel<OutElem, EX | Exclude<OutErr, Done<any>>, OutDone, InElem, InErr, InDone, EnvX | Env>;

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.

See

  • fromTransform for a simpler transformation without a forked scope

Signature

declare function fromTransformBracket<
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  EX,
  EnvX,
  Env,
>(
  f: (
    upstream: Pull<InElem, InErr, InDone>,
    scope: Scope,
    forkedScope: Scope,
  ) => Effect<Pull<OutElem, OutErr, OutDone, EnvX>, EX, Env>,
): Channel<OutElem, EX | Exclude<OutErr, Done<any>>, OutDone, InElem, InErr, InDone, EnvX | Env>;

Creates a channel backed by a Web TransformStream, writing upstream values while emitting transformed values from its readable side.

Signature

declare function fromTransformStream<IE, I, O, E>(options: {
  readonly closeOnDone?: boolean;
  readonly evaluate: LazyArg<TransformStream<I, O>>;
  readonly onError: (error: unknown) => E;
  readonly releaseLockOnEnd?: boolean;
}): Channel<readonly [O, O], IE | E, void, readonly [I, I], IE>;

Creates a channel that writes upstream values to a lazily supplied Web WritableStream.

Signature

declare function fromWritableStream<IE, E, A>(options: {
  readonly closeOnDone?: boolean;
  readonly evaluate: LazyArg<WritableStream<A>>;
  readonly onError: (error: unknown) => E;
}): Channel<never, IE | E, void, readonly [A, A], IE>;

identity

Added in v2.0.0 Source

Creates a channel that forwards upstream input elements, input errors, and the upstream done value unchanged.

Signature

declare function identity<Elem, Err, Done>(): Channel<Elem, Err, Done, Elem, Err, Done>;

never

Added in v2.0.0 Source

Represents a Channel that never completes.

Signature

declare const never: Channel<never, never, never>;

succeed

Added in v2.0.0 Source

Creates a Channel that emits a single value and then ends.

Signature

declare function succeed<A>(value: A): Channel<A>;

suspend

Added in v2.0.0 Source

Creates a Channel that lazily evaluates to another channel.

Signature

declare function suspend<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  evaluate: LazyArg<Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>>,
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>;

sync

Added in v2.0.0 Source

Creates a Channel that emits a single value computed by a lazy evaluation.

Signature

declare function sync<A>(evaluate: LazyArg<A>): Channel<A>;

Transforms a Channel by applying a function to its Pull implementation.

Signature

declare function transformPull<
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env,
  OutElem2,
  OutErr2,
  OutDone2,
  Env2,
  OutErrX,
  EnvX,
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  f: (
    pull: Pull<OutElem, OutErr, OutDone>,
    scope: Scope,
  ) => Effect<Pull<OutElem2, OutErr2, OutDone2, Env2>, OutErrX, EnvX>,
): Channel<
  OutElem2,
  OutErrX | Exclude<OutErr2, Done<any>>,
  OutDone2,
  InElem,
  InErr,
  InDone,
  Env | Env2 | EnvX
>;

unwrap

Added in v2.0.0 Source

Constructs a Channel from a scoped effect that will result in a Channel if successful.

Signature

declare function unwrap<OutElem, OutErr, OutDone, InElem, InErr, InDone, R2, E, R>(
  channel: Effect<Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, R2>, E, R>,
): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, R2 | Exclude<R, Scope>>;

Decoding

decodeText

Added in v4.0.0 Source

Decodes incoming Uint8Array chunks into strings using TextDecoder.

Details

Input chunks are decoded with streaming enabled so multi-byte characters may span Uint8Array boundaries. The optional encoding and options are passed to TextDecoder.

Signature

declare function decodeText<Err, Done>(
  encoding?: string,
  options?: TextDecoderOptions,
): Channel<
  readonly [string, string],
  Err,
  Done,
  readonly [Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>],
  Err,
  Done
>;

Destructors

Runs a channel and publishes each output element to a PubSub.

Details

The channel's output values are published as individual PubSub messages. Use options.shutdownOnEnd to shut down the PubSub when channel execution ends.

Signature

declare const runIntoPubSub: {
  <OutElem>(
    pubsub: PubSub<OutElem>,
    options?: {
      readonly shutdownOnEnd?: boolean;
    },
  ): <OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<void, never, Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    pubsub: PubSub<OutElem>,
    options?: {
      readonly shutdownOnEnd?: boolean;
    },
  ): Effect<void, never, 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.

Signature

declare const runIntoPubSubArray: {
  <OutElem>(
    pubsub: PubSub<OutElem>,
    options?: {
      readonly shutdownOnEnd?: boolean;
    },
  ): <OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<OutDone, OutErr, Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
    pubsub: PubSub<OutElem>,
    options?: {
      readonly shutdownOnEnd?: boolean;
    },
  ): Effect<OutDone, OutErr, Env>;
};

runIntoQueue

Added in v4.0.0 Source

Runs a channel and offers each output element into a queue.

Details

When the channel completes, the queue is ended. When the channel fails, the queue is failed with the channel's cause. The returned effect itself completes with void.

Signature

declare const runIntoQueue: {
  <OutElem, OutErr>(
    queue: Queue<OutElem, Done<void> | OutErr>,
  ): <OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<void, never, Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    queue: Queue<OutElem, Done<void> | OutErr>,
  ): Effect<void, never, Env>;
};

Runs a channel that emits non-empty arrays and offers each array element into a queue.

Details

When the channel completes, the queue is ended. When the channel fails, the queue is failed with the channel's cause. The returned effect itself completes with void.

Signature

declare const runIntoQueueArray: {
  <OutElem, OutErr>(
    queue: Queue<OutElem, Done<void> | OutErr>,
  ): <OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<void, never, Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
    queue: Queue<OutElem, Done<void> | OutErr>,
  ): Effect<void, never, Env>;
};

toPubSub

Added in v2.0.0 Source

Converts a channel to a PubSub for concurrent consumption.

Details

shutdownOnEnd indicates whether the PubSub should be shut down when the channel ends. By default this is true.

Signature

declare const toPubSub: {
  (
    options:
      | {
          readonly capacity: "unbounded";
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
        }
      | {
          readonly capacity: number;
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<PubSub<OutElem>, never, Scope | Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    options:
      | {
          readonly capacity: "unbounded";
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
        }
      | {
          readonly capacity: number;
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Effect<PubSub<OutElem>, never, Scope | Env>;
};

Converts an array-emitting channel to a scoped PubSub for concurrent consumption.

Details

Each element inside emitted non-empty arrays is published as an individual PubSub message. shutdownOnEnd indicates whether the PubSub should be shut down when the channel ends. By default this is true.

Signature

declare const toPubSubArray: {
  (
    options:
      | {
          readonly capacity: "unbounded";
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
        }
      | {
          readonly capacity: number;
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<PubSub<OutElem>, never, Scope | Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
    options:
      | {
          readonly capacity: "unbounded";
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
        }
      | {
          readonly capacity: number;
          readonly replay?: number;
          readonly shutdownOnEnd?: boolean;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Effect<PubSub<OutElem>, never, Scope | Env>;
};

toPubSubTake

Added in v4.0.0 Source

Converts a channel to a scoped PubSub of Take values.

Details

Emitted non-empty arrays are published as output Take values. When the channel ends, its final Exit is published so subscribers can observe completion or failure.

Signature

declare const toPubSubTake: {
  (
    options:
      | {
          readonly capacity: "unbounded";
          readonly replay?: number;
        }
      | {
          readonly capacity: number;
          readonly replay?: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutDone, OutDone], OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<PubSub<Take<OutElem, OutErr, OutDone>>, never, Scope | Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
    options:
      | {
          readonly capacity: "unbounded";
          readonly replay?: number;
        }
      | {
          readonly capacity: number;
          readonly replay?: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Effect<PubSub<Take<OutElem, OutErr, OutDone>>, never, Scope | Env>;
};

toPull

Added in v2.0.0 Source

Converts a channel to a scoped Pull for low-level consumption.

Details

The effect requires a Scope. The returned pull should be consumed only while that scope remains open. Pulls are serialized so only one pull is evaluated at a time.

Signature

declare const toPull: <OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
) => Effect.Effect<Pull.Pull<OutElem, OutErr, OutDone>, never, Env | Scope.Scope>;

toPullScoped

Added in v4.0.0 Source

Converts a channel to a Pull within an existing scope.

Signature

declare function toPullScoped<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  scope: Scope,
): Effect<Pull<OutElem, OutErr, OutDone, Env>, never, Env>;

toQueue

Added in v2.0.0 Source

Creates a scoped queue and forks the channel to feed it for concurrent consumption.

Details

Output elements are offered to the queue. Channel completion and failure are signaled through the queue. The queue is shut down when the surrounding scope closes.

Signature

declare const toQueue: {
  (
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<Dequeue<OutElem, Done<void> | OutErr>, never, Scope | Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Effect<Dequeue<OutElem, Done<void> | OutErr>, never, Scope | Env>;
};

toQueueArray

Added in v4.0.0 Source

Creates a scoped queue and forks an array-emitting channel to feed it.

Details

Each element inside emitted non-empty arrays is offered to the queue. Channel completion and failure are signaled through the queue. The queue is shut down when the surrounding scope closes.

Signature

declare const toQueueArray: {
  (
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<Dequeue<OutElem, Done<void> | OutErr>, never, Scope | Env>;
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, unknown, unknown, unknown, Env>,
    options:
      | {
          readonly capacity: "unbounded";
        }
      | {
          readonly capacity: number;
          readonly strategy?: "dropping" | "sliding" | "suspend";
        },
  ): Effect<Dequeue<OutElem, Done<void> | OutErr>, never, Scope | Env>;
};

toTransform

Added in v4.0.0 Source

Converts a Channel back to its underlying transformation function.

Signature

declare function toTransform<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  channel: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
): (
  upstream: Pull<InElem, InErr, InDone>,
  scope: Scope,
) => Effect<Pull<OutElem, OutErr, OutDone, never>, never, Env>;

Encoding

encodeText

Added in v4.0.0 Source

Encodes incoming string chunks into Uint8Array values using TextEncoder.

Details

Each string inside an emitted array is encoded independently.

Signature

declare function encodeText<Err, Done>(): Channel<
  readonly [Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>],
  Err,
  Done,
  readonly [string, string],
  Err,
  Done
>;

Error Handling

catchCause

Added in v2.0.0 Source

Catches any cause of failure from the channel and allows recovery by creating a new channel based on the caught cause.

Signature

declare const catchCause: {
  <OutErr, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    f: (d: Cause<OutErr>) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr1,
    OutDone1 | OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: Cause<OutErr>) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): Channel<
    OutElem | OutElem1,
    OutErr1,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

Recovers from channel failures whose full Cause is selected by a Filter.

When to use

Use when you need to recover a channel only from causes selected by a Filter, while giving the recovery both the selected value and the original Cause.

Details

When the filter succeeds, the recovery function receives the selected value and the original cause. When the filter fails, the returned channel fails with the residual cause produced by the filter.

See

Signature

declare const catchCauseFilter: {
  <OutErr, EB, X extends Cause<any>, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    filter: Filter<Cause<OutErr>, EB, X>,
    f: (
      failure: EB,
      cause: Cause<OutErr>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr1 | Error<X>,
    OutDone1 | OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    EB,
    X extends Cause<any>,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    filter: Filter<Cause<OutErr>, EB, X>,
    f: (
      failure: EB,
      cause: Cause<OutErr>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): Channel<
    OutElem | OutElem1,
    OutErr1 | Error<X>,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

catchCauseIf

Added in v4.0.0 Source

Catches causes of failure that match a specific filter, allowing conditional error recovery based on the type of failure.

When to use

Use to recover a channel only when its full Cause satisfies a boolean predicate.

Details

When the predicate matches, the recovery function receives the original cause. When it does not match, the returned channel fails with the original cause.

See

Signature

declare const catchCauseIf: {
  <OutErr, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    predicate: Predicate<Cause<OutErr>>,
    f: (
      cause: Cause<OutErr>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr | OutErr1,
    OutDone1 | OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: Predicate<Cause<OutErr>>,
    f: (
      cause: Cause<OutErr>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): Channel<
    OutElem | OutElem1,
    OutErr | OutErr1,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

catchFilter

Added in v4.0.0 Source

Recovers from typed channel errors selected by a Filter.

When to use

Use to recover from channel errors with a reusable Filter when matching can also narrow or transform the error before choosing the recovery channel.

Details

Successful filter results are handled by the recovery function. Failed filter results are handled by orElse when provided. Without orElse, failed filter results are re-failed.

See

Signature

declare const catchFilter: {
  <
    OutErr,
    EB,
    X,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    filter: Filter<OutErr, EB, X>,
    f: (failure: EB) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (failure: X) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned ? X : never,
    OutDone1 | OutDone2 | OutDone,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env1 | Env2 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    EB,
    X,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    filter: Filter<OutErr, EB, X>,
    f: (failure: EB) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (failure: X) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): Channel<
    OutElem | OutElem1 | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned ? X : never,
    OutDone | OutDone1 | OutDone2,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env | Env1 | Env2
  >;
};

catchIf

Added in v4.0.0 Source

Recovers from typed channel errors that match a predicate or refinement.

When to use

Use to recover from typed channel errors when a predicate or refinement selects the failures that should switch to a recovery channel.

Details

Matching errors are handled by the recovery function. Non-matching errors are handled by orElse when provided. Without orElse, non-matching errors are re-failed.

See

  • catch for recovering from every typed channel error
  • catchFilter for selecting typed errors with a Filter
  • catchTag for selecting tagged typed errors
  • catchCauseFilter for selecting full causes with a Filter

Signature

declare const catchIf: {
  <
    OutErr,
    EB,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    refinement: Refinement<OutErr, EB>,
    f: (failure: EB) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      failure: Exclude<OutErr, EB>,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned ? Exclude<OutErr, EB> : never,
    OutDone1 | OutDone2 | OutDone,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env1 | Env2 | Env
  >;
  <
    OutErr,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    predicate: Predicate<OutErr>,
    f: (failure: OutErr) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      failure: OutErr,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned ? OutErr : never,
    OutDone1 | OutDone2 | OutDone,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env1 | Env2 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    EB,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    refinement: Refinement<OutErr, EB>,
    f: (failure: EB) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      failure: Exclude<OutErr, EB>,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): Channel<
    OutElem | OutElem1 | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned ? Exclude<OutErr, EB> : never,
    OutDone | OutDone1 | OutDone2,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env | Env1 | Env2
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: Predicate<OutErr>,
    f: (failure: OutErr) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      failure: OutErr,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): Channel<
    OutElem | OutElem1 | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned ? OutErr : never,
    OutDone | OutDone1 | OutDone2,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env | Env1 | Env2
  >;
};

catchReason

Added in v4.0.0 Source

Catches a specific reason within a tagged error.

Signature

declare const catchReason: {
  <
    OutErr,
    K extends string,
    RK extends string,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    errorTag: K,
    reasonTag: RK,
    f: (
      reason: ExtractReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
      error: NarrowReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      reason: ExcludeReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
      error: OmitReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem | Exclude<OutElem2, unassigned>,
      | OutErr1
      | OutErr2
      | Exclude<
          OutErr,
          {
            readonly _tag: K;
          }
        >
      | OutElem2 extends unassigned
      ? ExtractTag<OutErr, K>
      : never,
    OutDone1 | OutDone2 | OutDone,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env1 | Env2 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    K extends string,
    RK extends string,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    errorTag: K,
    reasonTag: RK,
    f: (
      reason: ExtractReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
      error: NarrowReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      reason: ExcludeReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
      error: OmitReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): Channel<
    OutElem | OutElem1 | Exclude<OutElem2, unassigned>,
      | OutErr1
      | OutErr2
      | Exclude<
          OutErr,
          {
            readonly _tag: K;
          }
        >
      | OutElem2 extends unassigned
      ? ExtractTag<OutErr, K>
      : never,
    OutDone | OutDone1 | OutDone2,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env | Env1 | Env2
  >;
};

catchReasons

Added in v4.0.0 Source

Catches multiple reasons within a tagged error using an object of handlers.

Signature

declare const catchReasons: {
  <
    K extends string,
    OutErr,
    Cases extends {
      [RK in string]: (
        reason: ExtractReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
        error: NarrowReason<ExtractTag<NoInfer<OutErr>, K>, RK>,
      ) => Channel<any, any, any, any, any, any, any>;
    },
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    errorTag: K,
    cases: Cases,
    orElse?: (
      reason: ExcludeReason<ExtractTag<NoInfer<OutErr>, K>, Extract<keyof Cases, string>>,
      error: OmitReason<ExtractTag<NoInfer<OutErr>, K>, Extract<keyof Cases, string>>,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    | OutElem
    | Exclude<OutElem2, unassigned>
    | {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<OutElem1, any, any, any, any, any, any>
          ? OutElem1
          : never;
      }[keyof Cases],
      | OutErr2
      | Exclude<
          OutErr,
          {
            readonly _tag: K;
          }
        >
      | OutElem2 extends unassigned
      ? ExtractTag<OutErr, K>
      :
          | never
          | {
              [RK in string | number | symbol]: Cases[RK] extends (
                ...args: Array<any>
              ) => Channel<any, OutErr1, any, any, any, any, any>
                ? OutErr1
                : never;
            }[keyof Cases],
    | OutDone2
    | OutDone
    | {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, OutDone1, any, any, any, any>
          ? OutDone1
          : never;
      }[keyof Cases],
    InElem &
      InElem2 &
      {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, InElem1, any, any, any>
          ? InElem1
          : never;
      }[keyof Cases],
    InErr &
      InErr2 &
      {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, any, InErr1, any, any>
          ? InErr1
          : never;
      }[keyof Cases],
    InDone &
      InDone2 &
      {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, any, any, InDone1, any>
          ? InDone1
          : never;
      }[keyof Cases],
    | Env2
    | Env
    | {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, any, any, any, Env1>
          ? Env1
          : never;
      }[keyof Cases]
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    K extends string,
    Cases extends {
      [RK in string]: (
        reason: ExtractReason<ExtractTag<OutErr, K>, RK>,
        error: NarrowReason<ExtractTag<OutErr, K>, RK>,
      ) => Channel<any, any, any, any, any, any, any>;
    },
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    errorTag: K,
    cases: Cases,
    orElse?: (
      reason: ExcludeReason<ExtractTag<NoInfer<OutErr>, K>, Extract<keyof Cases, string>>,
      error: OmitReason<ExtractTag<NoInfer<OutErr>, K>, Extract<keyof Cases, string>>,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): Channel<
    | OutElem
    | Exclude<OutElem2, unassigned>
    | {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<OutElem1, any, any, any, any, any, any>
          ? OutElem1
          : never;
      }[keyof Cases],
      | OutErr2
      | Exclude<
          OutErr,
          {
            readonly _tag: K;
          }
        >
      | OutElem2 extends unassigned
      ? ExtractTag<OutErr, K>
      :
          | never
          | {
              [RK in string | number | symbol]: Cases[RK] extends (
                ...args: Array<any>
              ) => Channel<any, OutErr1, any, any, any, any, any>
                ? OutErr1
                : never;
            }[keyof Cases],
    | OutDone
    | OutDone2
    | {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, OutDone1, any, any, any, any>
          ? OutDone1
          : never;
      }[keyof Cases],
    InElem &
      InElem2 &
      {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, InElem1, any, any, any>
          ? InElem1
          : never;
      }[keyof Cases],
    InErr &
      InErr2 &
      {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, any, InErr1, any, any>
          ? InErr1
          : never;
      }[keyof Cases],
    InDone &
      InDone2 &
      {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, any, any, InDone1, any>
          ? InDone1
          : never;
      }[keyof Cases],
    | Env
    | Env2
    | {
        [RK in string | number | symbol]: Cases[RK] extends (
          ...args: Array<any>
        ) => Channel<any, any, any, any, any, any, Env1>
          ? Env1
          : never;
      }[keyof Cases]
  >;
};

catchTag

Added in v4.0.0 Source

Recovers from tagged channel errors whose _tag matches one or more tags.

Details

Matching tagged errors are handled by the recovery function. Non-matching errors are handled by orElse when provided. Without orElse, non-matching errors are re-failed.

Signature

declare const catchTag: {
  <
    OutErr,
    K extends string | readonly [Tags<OutErr>, Tags<OutErr>],
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    k: K,
    f: (
      e: ExtractTag<NoInfer<OutErr>, K extends readonly [string, string] ? K[number] : K>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      e: Exclude<
        NoInfer<OutErr>,
        {
          readonly _tag: K;
        }
      >,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned
      ? Exclude<
          OutErr,
          {
            readonly _tag: K;
          }
        >
      : never,
    OutDone1 | OutDone2 | OutDone,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env1 | Env2 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    K extends string | readonly [Tags<OutErr>, Tags<OutErr>],
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
    OutElem2 = unassigned,
    OutErr2 = never,
    OutDone2 = never,
    InElem2 = unknown,
    InErr2 = unknown,
    InDone2 = unknown,
    Env2 = never,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    k: K,
    f: (
      e: ExtractTag<NoInfer<OutErr>, K extends readonly [string, string] ? K[number] : K>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    orElse?: (
      e: Exclude<
        NoInfer<OutErr>,
        {
          readonly _tag: K;
        }
      >,
    ) => Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
  ): Channel<
    OutElem | OutElem1 | Exclude<OutElem2, unassigned>,
    OutErr1 | OutErr2 | OutElem2 extends unassigned
      ? Exclude<
          OutErr,
          {
            readonly _tag: K;
          }
        >
      : never,
    OutDone | OutDone1 | OutDone2,
    InElem & InElem1 & InElem2,
    InErr & InErr1 & InErr2,
    InDone & InDone1 & InDone2,
    Env | Env1 | Env2
  >;
};

ignore

Added in v4.0.0 Source

Ignores all errors in the channel, converting them to an empty channel.

Details

Use the log option to emit the full Cause when the channel fails.

Signature

declare const ignore: <
  Arg extends
    | Channel<any, any, any, any, any, any, any>
    | {
        readonly log?: boolean | Severity;
      }
    | undefined = {
    readonly log?: boolean | Severity;
  },
>(
  selfOrOptions: Arg,
  options?: {
    readonly log?: boolean | Severity;
  },
) => [Arg] extends [
  Channel<
    infer OutElem,
    infer _OutErr,
    infer OutDone,
    infer InElem,
    infer InErr,
    infer InDone,
    infer Env
  >,
]
  ? Channel<OutElem, never, OutDone | void, InElem, InErr, InDone, Env>
  : <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
      self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    ) => Channel<OutElem, never, OutDone | void, InElem, InErr, InDone, Env>;

ignoreCause

Added in v4.0.0 Source

Ignores all errors in the channel including defects, converting them to an empty channel.

When to use

Use when a channel should become best-effort and all failure causes, including defects and interruptions, can be converted to empty output.

Details

Use the log option to emit the full Cause when the channel fails.

Signature

declare const ignoreCause: <
  Arg extends
    | Channel<any, any, any, any, any, any, any>
    | {
        readonly log?: boolean | Severity;
      }
    | undefined = {
    readonly log?: boolean | Severity;
  },
>(
  selfOrOptions: Arg,
  options?: {
    readonly log?: boolean | Severity;
  },
) => [Arg] extends [
  Channel<
    infer OutElem,
    infer _OutErr,
    infer OutDone,
    infer InElem,
    infer InErr,
    infer InDone,
    infer Env
  >,
]
  ? Channel<OutElem, never, OutDone | void, InElem, InErr, InDone, Env>
  : <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
      self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    ) => Channel<OutElem, never, OutDone | void, InElem, InErr, InDone, Env>;

mapError

Added in v2.0.0 Source

Returns a new channel, which is the same as this one, except the failure value of the returned channel is created by applying the specified function to the failure value of this channel.

Signature

declare const mapError: {
  <OutErr, OutErr2>(
    f: (err: OutErr) => OutErr2,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr2, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutErr2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (err: OutErr) => OutErr2,
  ): Channel<OutElem, OutErr2, OutDone, InElem, InErr, InDone, Env>;
};

onError

Added in v4.0.0 Source

Attaches a finalizer that runs only when the channel exits with failure.

Details

The finalizer receives the failure Cause. The original channel failure is preserved. The finalizer itself must not fail.

Signature

declare const onError: {
  <OutDone, OutErr, Env2>(
    finalizer: (cause: Cause<OutErr>) => Effect<unknown, never, Env2>,
  ): <OutElem, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    finalizer: (cause: Cause<OutErr>) => Effect<unknown, never, Env2>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env | Env2>;
};

orDie

Added in v2.0.0 Source

Converts all errors in the channel to defects (unrecoverable failures). This is useful when you want to treat errors as programming errors.

Signature

declare function orDie<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
): Channel<OutElem, never, OutDone, InElem, InErr, InDone, Env>;

retry

Added in v4.0.0 Source

Returns a new channel that retries this channel according to the specified schedule whenever it fails.

Signature

declare const retry: {
  <SO, OutErr, SE, SR>(schedule: Schedule<SO, NoInfer<OutErr>, SE, SR> | ($: <SO, SE, SR>(_: Schedule<SO, NoInfer<OutErr>, SE, SR>) => Schedule<SO, OutErr, SE, SR>) => Schedule<SO, NoInfer<OutErr>, SE, SR>): <OutElem, OutDone, InElem, InErr, InDone, Env>(self: Channel<OutElem, OutErr | SE, OutDone, InElem, InErr, InDone, SR | Env>) => Channel<OutElem, OutErr | SE, OutDone, InElem, InErr, InDone, SR | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, SO, SE, SR>(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>, schedule: Schedule<SO, OutErr, SE, SR> | ($: <SO, SE, SR>(_: Schedule<SO, NoInfer<OutErr>, SE, SR>) => Schedule<SO, OutErr, SE, SR>) => Schedule<SO, NoInfer<OutErr>, SE, SR>): Channel<OutElem, OutErr | SE, OutDone, InElem, InErr, InDone, Env | SR>;
}

tapCause

Added in v4.0.0 Source

Runs an effect with the full failure Cause when the channel fails, then fails the returned channel with the original cause.

When to use

Use when observing the full channel failure Cause is needed without changing successful output or replacing the original cause.

Details

Use this for observing failures, such as logging or metrics. If the observer effect fails, that failure can fail the returned channel.

Signature

declare const tapCause: {
  <OutErr, A, E, R>(
    f: (d: Cause<OutErr>) => Effect<A, E, R>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr | E, void | OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, A, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: Cause<OutErr>) => Effect<A, E, R>,
  ): Channel<OutElem, OutErr | E, void | OutDone, InElem, InErr, InDone, Env | R>;
};

tapError

Added in v4.0.0 Source

Runs an effect when the channel fails with a typed error, then preserves the original channel failure.

Details

The effect is not run for normal channel completion. If the observer effect fails, that failure can fail the returned channel.

Signature

declare const tapError: {
  <OutErr, A, E, R>(
    f: (d: OutErr) => Effect<A, E, R>,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr | E, void | OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, A, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: OutErr) => Effect<A, E, R>,
  ): Channel<OutElem, OutErr | E, void | OutDone, InElem, InErr, InDone, Env | R>;
};

unwrapReason

Added in v4.0.0 Source

Promotes nested reason errors into the channel error, replacing the parent error.

Signature

declare const unwrapReason: {
  <K extends string, OutErr>(
    errorTag: K,
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem,
    | Exclude<
        OutErr,
        {
          readonly _tag: K;
        }
      >
    | ReasonOf<ExtractTag<OutErr, K>>,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, K extends string>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    errorTag: K,
  ): Channel<
    OutElem,
    | Exclude<
        OutErr,
        {
          readonly _tag: K;
        }
      >
    | ReasonOf<ExtractTag<OutErr, K>>,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >;
};

Filtering

filter

Added in v4.0.0 Source

Filters the output elements of a channel using a predicate function. Elements that don't match the predicate are discarded.

Signature

declare const filter: {
  <OutElem, B>(
    refinement: Refinement<OutElem, B>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<B, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem>(
    predicate: Predicate<OutElem>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, B>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    refinement: Refinement<OutElem, B>,
  ): Channel<B, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: Predicate<OutElem>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>;
};

filterArray

Added in v4.0.0 Source

Filters arrays of elements emitted by a channel, applying the filter to each element within the arrays and only emitting non-empty filtered arrays.

Signature

declare const filterArray: {
  <OutElem, B>(
    refinement: Refinement<OutElem, B>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<readonly [B, B], OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem>(
    predicate: Predicate<NoInfer<OutElem>>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, B>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
    refinement: Refinement<OutElem, B>,
  ): Channel<readonly [B, B], OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: Predicate<NoInfer<OutElem>>,
  ): Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>;
};

Filters each element inside emitted non-empty arrays with an effectful predicate.

When to use

Use when filtering array-valued channel outputs requires Effects or services, and arrays that become empty should be skipped.

Details

The predicate receives the element and its index within the array. Elements for which the predicate succeeds with true are kept. Arrays that become empty are discarded. Predicate failures fail the returned channel.

Signature

declare const filterArrayEffect: {
  <OutElem, E, R>(
    predicate: (a: NoInfer<OutElem>, index: number) => Effect<boolean, E, R>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<readonly [OutElem, OutElem], E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, E, R>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: (a: NoInfer<OutElem>, index: number) => Effect<boolean, E, R>,
  ): Channel<readonly [OutElem, OutElem], OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

filterEffect

Added in v4.0.0 Source

Filters output elements with an effectful predicate.

When to use

Use when the keep/discard decision depends on an Effect or service and predicate failures should fail the returned channel.

Details

Elements for which the predicate succeeds with true are emitted. Elements for which the predicate succeeds with false are discarded. Predicate failures fail the returned channel.

Signature

declare const filterEffect: {
  <OutElem, E, R>(
    predicate: (a: OutElem) => Effect<boolean, E, R>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: (a: OutElem) => Effect<boolean, E, R>,
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

filterMap

Added in v4.0.0 Source

Filters and maps output elements using a Filter.

When to use

Use to keep only channel output elements accepted by a Filter and emit each filter success value.

Details

Successful filter results are emitted as mapped values. Failed filter results are discarded. The source channel's errors and done value are preserved.

See

Signature

declare const filterMap: {
  <OutElem, B, X>(
    filter: Filter<OutElem, B, X>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<B, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, B, X>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    filter: Filter<OutElem, B, X>,
  ): Channel<B, OutErr, OutDone, InElem, InErr, InDone, Env>;
};

Filters and maps each element inside emitted non-empty arrays using a Filter.

Details

Successful filter results are kept as mapped values. Failed filter results are removed from the array. Arrays that become empty are discarded.

Signature

declare const filterMapArray: {
  <OutElem, B, X>(
    filter: Filter<NoInfer<OutElem>, B, X>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<readonly [B, B], OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, B, X>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
    filter: Filter<OutElem, B, X>,
  ): Channel<readonly [B, B], OutErr, OutDone, InElem, InErr, InDone, Env>;
};

Filters and maps each element inside emitted non-empty arrays using an effectful Filter.

When to use

Use when array-valued channel outputs need an effectful filter-map that can fail and can discard arrays that become empty.

Details

Successful filter results are kept as mapped values. Failed filter results are removed from the array. Arrays that become empty are discarded. Failures from the effectful filter fail the returned channel.

Signature

declare const filterMapArrayEffect: {
  <OutElem, B, X, EX, RX>(
    filter: FilterEffect<NoInfer<OutElem>, B, X, EX, RX>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<readonly [B, B], EX | OutErr, OutDone, InElem, InErr, InDone, RX | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, B, X, EX, RX>(
    self: Channel<readonly [OutElem, OutElem], OutErr, OutDone, InElem, InErr, InDone, Env>,
    filter: FilterEffect<OutElem, B, X, EX, RX>,
  ): Channel<readonly [B, B], OutErr | EX, OutDone, InElem, InErr, InDone, Env | RX>;
};

Filters and maps output elements using an effectful Filter.

When to use

Use to apply effectful logic that can discard channel output elements and emit transformed values for the elements that pass.

Details

Successful filter results are emitted as mapped values. Failed filter results are discarded. Failures from the effectful filter fail the returned channel.

See

Signature

declare const filterMapEffect: {
  <OutElem, B, X, EX, RX>(
    filter: FilterEffect<OutElem, B, X, EX, RX>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<B, EX | OutErr, OutDone, InElem, InErr, InDone, RX | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, B, X, EX, RX>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    filter: FilterEffect<OutElem, B, X, EX, RX>,
  ): Channel<B, OutErr | EX, OutDone, InElem, InErr, InDone, Env | RX>;
};

Guards

isChannel

Added in v3.5.4 Source

Checks whether a value is a Channel.

Signature

declare function isChannel(
  u: unknown,
): u is Channel<unknown, unknown, unknown, unknown, unknown, unknown, unknown>;

Hooks

onEnd

Added in v4.0.0 Source

Runs an effect when the channel completes successfully.

Details

The effect runs before the original done value is propagated. The effect is not run when the channel fails. If the effect fails, the returned channel fails with that error.

Signature

declare const onEnd: {
  <A, E, R>(
    onEnd: Effect<A, E, R>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, A, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    onEnd: Effect<A, E, R>,
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

onFirst

Added in v4.0.0 Source

Runs an effect the first time the channel emits an output element.

When to use

Use when initialization depends on the first output element rather than only on channel startup.

Details

The effect receives the first emitted element. The first element is still emitted unchanged. The effect is not run if the channel completes without emitting an element.

Signature

declare const onFirst: {
  <OutElem, A, E, R>(
    onFirst: (element: NoInfer<OutElem>) => Effect<A, E, R>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, A, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    onFirst: (element: NoInfer<OutElem>) => Effect<A, E, R>,
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

onStart

Added in v4.0.0 Source

Runs an effect before the channel starts.

Details

The effect's successful value is ignored. If the effect fails, the returned channel fails before running the source channel.

Signature

declare const onStart: {
  <A, E, R>(
    onStart: Effect<A, E, R>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, A, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    onStart: Effect<A, E, R>,
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

Interruption

haltWhen

Added in v4.0.0 Source

Stops a channel when the specified effect completes or fails.

Details

If the effect completes before the channel is done, its success value becomes the returned channel's done value. If the effect fails, the returned channel fails with that error. If the channel completes first, the channel's done value is preserved.

Signature

declare const haltWhen: {
  <OutDone2, OutErr2, Env2>(
    effect: Effect<OutDone2, OutErr2, Env2>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr2 | OutErr, OutDone2 | OutDone, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutDone2, OutErr2, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    effect: Effect<OutDone2, OutErr2, Env2>,
  ): Channel<OutElem, OutErr | OutErr2, OutDone | OutDone2, InElem, InErr, InDone, Env | Env2>;
};

Interrupts a channel when another effect completes.

When to use

Use to race channel execution against an external effect whose success can become the channel's done value.

Details

If the effect completes first, its success value becomes the returned channel's done value. If the channel completes first, the original channel's done value is preserved.

Signature

declare const interruptWhen: {
  <OutDone2, OutErr2, Env2>(
    effect: Effect<OutDone2, OutErr2, Env2>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr2 | OutErr, OutDone2 | OutDone, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutDone2, OutErr2, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    effect: Effect<OutDone2, OutErr2, Env2>,
  ): Channel<OutElem, OutErr | OutErr2, OutDone | OutDone2, InElem, InErr, InDone, Env | Env2>;
};

Mapping

bindTo

Added in v4.0.0 Source

Wraps each output element in an object under the specified field name.

When to use

Use when you need to start a Channel Do-notation chain from an existing output value by assigning that value to a field name.

See

  • Do for starting Do notation from an empty object
  • bind for adding a field produced by another channel
  • let for adding a computed field

Signature

declare const bindTo: {
  <N extends string>(
    name: N,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<{ [K in string]: OutElem }, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, N extends string>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    name: N,
  ): Channel<{ [K in string]: OutElem }, OutErr, OutDone, InElem, InErr, InDone, Env>;
};

Models

Channel interface

Added in v2.0.0 Source

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.

Signature

interface Channel<
  out OutElem,
  out OutErr = never,
  out OutDone = void,
  in InElem = unknown,
  in InErr = unknown,
  in InDone = unknown,
  out Env = never,
>
  extends Variance<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>, Pipeable {
  [ignoreSymbol]?: ChannelUnifyIgnore;
  [typeSymbol]?: unknown;
  [unifySymbol]?: ChannelUnify<Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>>;
}

ChannelUnify interface

Added in v2.0.0 Source

Type-level unification support for Channel values.

Details

This preserves all Channel type parameters when Unify normalizes unions or generic return types that include channels. Users normally do not need to reference this interface directly.

Signature

interface ChannelUnify<
  A extends {
    [typeSymbol]?: any;
  },
> extends EffectUnify<A> {
  Channel?: () => A[typeof typeSymbol] extends
    | Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
    | _
    ? Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
    : never;
}

ChannelUnifyIgnore interface

Added in v2.0.0 Source

Marker used by Unify while resolving Channel values.

Details

It prevents the inherited Effect unifier from being selected when the channel-specific unifier should preserve Channel input, output, and environment type parameters. Users normally do not need to reference this interface directly.

Signature

interface ChannelUnifyIgnore {
  Effect?: true;
}

HaltStrategy type

Added in v4.0.0 Source

Represents strategies for halting merged channels when one completes or fails.

Signature

type HaltStrategy = "left" | "right" | "both" | "either";

Variance interface

Added in v2.0.0 Source

Phantom variance marker for the type parameters of Channel.

Details

Output element, output error, output done, and environment types are covariant. Input element, input error, and input done types are contravariant. This is type-level machinery and is not used directly at runtime.

Signature

interface Variance<out OutElem, out OutErr, out OutDone, in InElem, in InErr, in InDone, out Env> {
  readonly "~effect/Channel": VarianceStruct<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>;
}

VarianceStruct interface

Added in v2.0.0 Source

Structural encoding used by Variance to record each Channel type parameter's variance.

Details

The _OutElem, _OutErr, _OutDone, and _Env fields are covariant; the _InElem, _InErr, and _InDone fields are contravariant. Users normally do not need to reference this interface directly.

Signature

interface VarianceStruct<
  out OutElem,
  out OutErr,
  out OutDone,
  in InElem,
  in InErr,
  in InDone,
  out Env,
> {
  _Env: Covariant<Env>;
  _InDone: Contravariant<InDone>;
  _InElem: Contravariant<InElem>;
  _InErr: Contravariant<InErr>;
  _OutDone: Covariant<OutDone>;
  _OutElem: Covariant<OutElem>;
  _OutErr: Covariant<OutErr>;
}

Other

Signature

declare const catch: {
  <OutErr, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(f: (d: OutErr) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>): <OutElem, OutDone, InElem, InErr, InDone, Env>(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>) => Channel<OutElem1 | OutElem, OutErr1, OutDone1 | OutDone, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env1 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>, f: (d: OutErr) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>): Channel<OutElem | OutElem1, OutErr1, OutDone | OutDone1, InElem & InElem1, InErr & InErr1, InDone & InDone1, Env | Env1>;
}

Signature

declare const let: {
  <N extends string, OutElem extends object, B>(
    name: Exclude<N, keyof OutElem>,
    f: (a: NoInfer<OutElem>) => B,
  ): <OutErr, OutDone, InElem, InErr, InDone, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, R>,
  ) => Channel<
    { [K in string | number | symbol]: K extends keyof OutElem ? OutElem[K] : B },
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    R
  >;
  <OutElem extends object, OutErr, OutDone, InElem, InErr, InDone, R, N extends string, B>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, R>,
    name: Exclude<N, keyof OutElem>,
    f: (a: NoInfer<OutElem>) => B,
  ): Channel<
    { [K in string | number | symbol]: K extends keyof OutElem ? OutElem[K] : B },
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    R
  >;
};

Providing Services

provide

Added in v4.0.0 Source

Provides a Layer or Context to the channel, removing the corresponding service requirements.

Details

Providing a Context delegates to provideContext. Providing a Layer builds the layer in the channel scope. Use options.local to build a fresh layer instance for this provision.

Signature

declare const provide: {
  <A, E = never, R = never>(
    layer: Layer<A, E, R> | Context<A>,
    options?: {
      readonly local?: boolean;
    },
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone, InElem, InErr, InDone, R | Exclude<Env, A>>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, A, E = never, R = never>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    layer: Layer<A, E, R> | Context<A>,
    options?: {
      readonly local?: boolean;
    },
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, R | Exclude<Env, A>>;
};

Provides a Context to the channel, removing the corresponding service requirements from the returned channel.

Signature

declare const provideContext: {
  <R2>(
    context: Context<R2>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Exclude<Env, R2>>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, R2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    context: Context<R2>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Exclude<Env, R2>>;
};

Provides a concrete service for a context key, removing that service requirement from the returned channel.

Signature

declare const provideService: {
  <I, S>(
    key: Key<I, S>,
    service: NoInfer<S>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Exclude<Env, I>>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, I, S>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    key: Key<I, S>,
    service: NoInfer<S>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Exclude<Env, I>>;
};

Provides a service to the channel after obtaining it from an effect.

When to use

Use to supply a channel dependency when constructing the service itself is effectful or can fail.

Details

If the service effect fails, the returned channel fails. The provided service removes the corresponding service requirement from the returned channel.

Signature

declare const provideServiceEffect: {
  <I, S, ES, RS>(
    key: Key<I, S>,
    service: Effect<NoInfer<S>, ES, RS>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, ES | OutErr, OutDone, InElem, InErr, InDone, RS | Exclude<Env, I>>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, I, S, ES, RS>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    key: Key<I, S>,
    service: Effect<NoInfer<S>, ES, RS>,
  ): Channel<OutElem, OutErr | ES, OutDone, InElem, InErr, InDone, RS | Exclude<Env, I>>;
};

Transforms the current context before running the channel.

Details

The function receives the surrounding context and returns the context to provide to the channel. The returned channel requires the services needed to build that context.

Signature

declare const updateContext: {
  <Env, R2>(
    f: (context: Context<R2>) => Context<Env>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone>(
    self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
  ) => Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R2>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, R2>(
    self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
    f: (context: Context<R2>) => Context<Env>,
  ): Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R2>;
};

Updates a service in the current context before running the channel.

Details

The existing service is read from the context. The updated service is provided to the channel under the same key.

Signature

declare const updateService: {
  <I, S>(
    key: Key<I, S>,
    f: (service: NoInfer<S>) => S,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
  ) => Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, I | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, I, S>(
    self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
    service: Key<I, S>,
    f: (service: NoInfer<S>) => S,
  ): Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env | I>;
};

Repetition

forever

Added in v4.0.0 Source

Repeats this channel forever.

Signature

declare function forever<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
): Channel<OutElem, OutErr, never, InElem, InErr, InDone, Env>;

repeat

Added in v4.0.0 Source

Repeats this channel according to the provided schedule.

Signature

declare const repeat: {
  <SO, OutDone, SE, SR>(schedule: Schedule<SO, NoInfer<OutDone>, SE, SR> | ($: <SO, SE, SR>(_: Schedule<SO, NoInfer<OutDone>, SE, SR>) => Schedule<SO, OutDone, SE, SR>) => Schedule<SO, NoInfer<OutDone>, SE, SR>): <OutElem, OutErr, InElem, InErr, InDone, Env>(self: Channel<OutElem, SE | OutErr, OutDone, InElem, InErr, InDone, SR | Env>) => Channel<OutElem, SE | OutErr, OutDone, InElem, InErr, InDone, SR | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, SO, SE, SR>(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>, schedule: Schedule<SO, OutDone, SE, SR> | ($: <SO, SE, SR>(_: Schedule<SO, NoInfer<OutDone>, SE, SR>) => Schedule<SO, OutDone, SE, SR>) => Schedule<SO, NoInfer<OutDone>, SE, SR>): Channel<OutElem, OutErr | SE, OutDone, InElem, InErr, InDone, Env | SR>;
}

Resource Management

ensuring

Added in v2.0.0 Source

Returns a channel with a finalizer effect that is guaranteed to run once the channel begins execution, whether it succeeds or fails.

Signature

declare const ensuring: {
  <Env2>(
    finalizer: Effect<unknown, never, Env2>,
  ): <OutElem, OutDone, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    finalizer: Effect<unknown, never, Env2>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env | Env2>;
};

onExit

Added in v4.0.0 Source

Returns a channel with an exit-aware finalizer that is guaranteed to run once the channel begins execution, whether it succeeds or fails.

Signature

declare const onExit: {
  <OutDone, OutErr, Env2>(
    finalizer: (e: Exit<OutDone, OutErr>) => Effect<unknown, never, Env2>,
  ): <OutElem, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    finalizer: (e: Exit<OutDone, OutErr>) => Effect<unknown, never, Env2>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env | Env2>;
};

scoped

Added in v2.0.0 Source

Runs a channel with a scope provided for the duration of the channel execution, removing the channel's Scope requirement.

Signature

declare function scoped<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Exclude<Env, Scope>>;

Running

runCollect

Added in v2.0.0 Source

Runs a channel and collects all output elements into an array.

Signature

declare function runCollect<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
): Effect<Array<OutElem>, OutErr, Env>;

runCount

Added in v4.0.0 Source

Runs a channel and counts the number of elements it outputs.

Signature

declare function runCount<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
): Effect<number, OutErr, Env>;

runDone

Added in v4.0.0 Source

Runs a channel and outputs the done value.

Signature

declare function runDone<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
): Effect<OutDone, OutErr, Env>;

runDrain

Added in v2.0.0 Source

Runs a channel and discards all output elements, returning only the final result.

Signature

declare function runDrain<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
): Effect<OutDone, OutErr, Env>;

runFold

Added in v4.0.0 Source

Runs a channel and folds over all output elements with an accumulator.

Signature

declare const runFold: {
  <Z, OutElem>(
    initial: LazyArg<Z>,
    f: (acc: Z, o: OutElem) => Z,
  ): <OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<Z, OutErr, Env>;
  <OutElem, OutErr, OutDone, Env, Z>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    initial: LazyArg<Z>,
    f: (acc: Z, o: OutElem) => Z,
  ): Effect<Z, OutErr, Env>;
};

Runs a channel and effectfully folds all output elements with an accumulator.

When to use

Use when folding channel output needs effects, services, or an additional failure channel during accumulation.

Details

The initial accumulator is evaluated lazily. Each output element is passed to the effectful accumulator function. The returned effect succeeds with the final accumulator value.

Signature

declare const runFoldEffect: {
  <OutElem, Z, E, R>(
    initial: LazyArg<Z>,
    f: (acc: Z, o: OutElem) => Effect<Z, E, R>,
  ): <OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<Z, E | OutErr, R | Env>;
  <OutElem, OutErr, OutDone, Env, Z, E, R>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    initial: LazyArg<Z>,
    f: (acc: Z, o: OutElem) => Effect<Z, E, R>,
  ): Effect<Z, OutErr | E, Env | R>;
};

runForEach

Added in v4.0.0 Source

Runs a channel and applies an effect to each output element.

Signature

declare const runForEach: {
  <OutElem, EX, RX>(
    f: (o: OutElem) => Effect<void, EX, RX>,
  ): <OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<OutDone, EX | OutErr, RX | Env>;
  <OutElem, OutErr, OutDone, Env, EX, RX>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    f: (o: OutElem) => Effect<void, EX, RX>,
  ): Effect<OutDone, OutErr | EX, Env | RX>;
};

Runs a channel and applies an effectful predicate to each output element until the predicate returns false.

Details

Returning true continues consuming the channel. Returning false stops consumption early. The returned effect completes with void.

Signature

declare const runForEachWhile: {
  <OutElem, EX, RX>(
    f: (o: OutElem) => Effect<boolean, EX, RX>,
  ): <OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Effect<void, EX | OutErr, RX | Env>;
  <OutElem, OutErr, OutDone, Env, EX, RX>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    f: (o: OutElem) => Effect<boolean, EX, RX>,
  ): Effect<void, OutErr | EX, Env | RX>;
};

runHead

Added in v4.0.0 Source

Runs a channel until the first output element is available, returning it in an Option.

Details

Returns Option.some with the first output element, or Option.none if the channel completes without emitting output.

Signature

declare function runHead<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
): Effect<Option<OutElem>, OutErr, Env>;

runLast

Added in v4.0.0 Source

Runs a channel to completion and returns the last output element in an Option.

Details

Returns Option.some with the last emitted element, or Option.none if the channel completes without emitting output.

Signature

declare function runLast<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
): Effect<Option<OutElem>, OutErr, Env>;

Sequencing

bind

Added in v4.0.0 Source

Adds a field to each object emitted by a channel by running another channel derived from that object.

Details

The field name must not already exist on the emitted object. The derived channel's output becomes the value of the new field. options.concurrency and options.bufferSize control how derived channels are flattened.

Signature

declare const bind: {
  <N extends string, OutElem extends object, B, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>(
    name: Exclude<N, keyof OutElem>,
    f: (a: NoInfer<OutElem>) => Channel<B, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
    options?: {
      readonly bufferSize?: number;
      readonly concurrency?: number | "unbounded";
    },
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    { [K in string | number | symbol]: K extends keyof OutElem ? OutElem[K] : B },
    OutErr2 | OutErr,
    OutDone,
    InElem & InElem2,
    InErr & InErr2,
    InDone & InDone2,
    Env2 | Env
  >;
  <
    OutElem extends object,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    N extends string,
    B,
    OutErr2,
    OutDone2,
    InElem2,
    InErr2,
    InDone2,
    Env2,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    name: Exclude<N, keyof OutElem>,
    f: (a: NoInfer<OutElem>) => Channel<B, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
    options?: {
      readonly bufferSize?: number;
      readonly concurrency?: number | "unbounded";
    },
  ): Channel<
    { [K in string | number | symbol]: K extends keyof OutElem ? OutElem[K] : B },
    OutErr | OutErr2,
    OutDone,
    InElem & InElem2,
    InErr & InErr2,
    InDone & InDone2,
    Env | Env2
  >;
};

combine

Added in v4.0.0 Source

Combines two channels with a stateful pull function.

When to use

Use to coordinate pulling from two channels when each output element depends on both sides and local state.

Details

The combining function receives the current state and pull functions for the left and right channels. It returns the next output element together with the next state.

Signature

declare const combine: {
  <
    OutElem2,
    OutErr2,
    OutDone2,
    InElem2,
    InErr2,
    InDone2,
    Env2,
    S,
    OutElem,
    OutErr,
    OutDone,
    A,
    E,
    R,
  >(
    that: Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
    s: LazyArg<S>,
    f: (
      s: S,
      pullLeft: Pull<OutElem, OutErr, OutDone>,
      pullRight: Pull<OutElem2, OutErr2, OutDone2>,
    ) => Effect<readonly [A, S], E, R>,
  ): <InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    A,
    Exclude<E, Done<any>>,
    Extract<E>,
    InElem & InElem2,
    InErr & InErr2,
    InDone & InDone2,
    Env2 | R | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem2,
    OutErr2,
    OutDone2,
    InElem2,
    InErr2,
    InDone2,
    Env2,
    S,
    A,
    E,
    R,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    that: Channel<OutElem2, OutErr2, OutDone2, InElem2, InErr2, InDone2, Env2>,
    s: LazyArg<S>,
    f: (
      s: S,
      pullLeft: Pull<OutElem, OutErr, OutDone>,
      pullRight: Pull<OutElem2, OutErr2, OutDone2>,
    ) => Effect<readonly [A, S], E, R>,
  ): Channel<
    A,
    Exclude<E, Done<any>>,
    Extract<E>,
    InElem & InElem2,
    InErr & InErr2,
    InDone & InDone2,
    Env | Env2 | R
  >;
};

concat

Added in v4.0.0 Source

Concatenates this channel with another channel, so that the second channel starts emitting values after the first channel has completed.

Signature

declare const concat: {
  <OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    that: Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr1 | OutErr,
    OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    that: Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): Channel<
    OutElem | OutElem1,
    OutErr | OutErr1,
    OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

concatWith

Added in v4.0.0 Source

Concatenates this channel with another channel created from the terminal value of this channel. The new channel is created using the provided function.

Signature

declare const concatWith: {
  <OutDone, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    f: (
      leftover: NoInfer<OutDone>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): <OutElem, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr1 | OutErr,
    OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (
      leftover: NoInfer<OutDone>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): Channel<
    OutElem | OutElem1,
    OutErr | OutErr1,
    OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

embedInput

Added in v2.0.0 Source

Runs an input handler against the upstream pull while the wrapped channel runs without receiving upstream input directly.

Details

The input handler is forked in the channel scope. The wrapped channel is run with an already-completed input.

Signature

declare const embedInput: {
  <InElem, InErr, InDone, R>(
    input: (upstream: Pull<InElem, InErr, InDone>) => Effect<void, never, R>,
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, Env, InErr, InElem, InDone, R>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    input: (upstream: Pull<InElem, InErr, InDone>) => Effect<void, never, R>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env | R>;
};

flatMap

Added in v2.0.0 Source

Maps each output element to a channel and flattens the child channel outputs.

Details

The source channel's done value is preserved. Child channel done values are used only for child-channel completion. By default child channels are run sequentially. Use options.concurrency and options.bufferSize to run child channels concurrently.

Signature

declare const flatMap: {
  <OutElem, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    f: (d: OutElem) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    options?: {
      readonly bufferSize?: number;
      readonly concurrency?: number | "unbounded";
    },
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1,
    OutErr1 | OutErr,
    OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: OutElem) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    options?: {
      readonly bufferSize?: number;
      readonly concurrency?: number | "unbounded";
    },
  ): Channel<
    OutElem1,
    OutErr | OutErr1,
    OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

map

Added in v2.0.0 Source

Maps the output of this channel using the specified function.

Signature

declare const map: {
  <OutElem, OutElem2>(
    f: (o: OutElem, i: number) => OutElem2,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem2, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutElem2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (o: OutElem, i: number) => OutElem2,
  ): Channel<OutElem2, OutErr, OutDone, InElem, InErr, InDone, Env>;
};

mapAccum

Added in v4.0.0 Source

Maps over a channel statefully with an accumulator, where each element can produce multiple output values.

Signature

declare const mapAccum: {
  <S, OutElem, B, E = never, R = never>(initial: LazyArg<S>, f: (s: S, a: NoInfer<OutElem>) => Effect<readonly [S, readonly Array<B>], E, R> | readonly [S, readonly Array<B>], options?: {
    readonly onHalt?: (state: S) => Array<B>;
  }): <OutErr, OutDone, InElem, InErr, InDone, Env>(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>) => Channel<B, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, S, B, E = never, R = never>(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>, initial: LazyArg<S>, f: (s: S, a: NoInfer<OutElem>) => Effect<readonly [S, readonly Array<B>], E, R> | readonly [S, readonly Array<B>], options?: {
    readonly onHalt?: (state: S) => Array<B>;
  }): Channel<B, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
}

mapDone

Added in v4.0.0 Source

Maps the done value of this channel using the specified function.

Signature

declare const mapDone: {
  <OutDone, OutDone2>(
    f: (o: OutDone) => OutDone2,
  ): <OutElem, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr, OutDone2, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutDone2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (o: OutDone) => OutDone2,
  ): Channel<OutElem, OutErr, OutDone2, InElem, InErr, InDone, Env>;
};

Maps the done value of this channel using the specified effectful function.

When to use

Use when the terminal done value transformation needs services or can fail, while emitted elements should pass through unchanged.

Signature

declare const mapDoneEffect: {
  <OutDone, OutDone2, E, R>(
    f: (o: OutDone) => Effect<OutDone2, E, R>,
  ): <OutElem, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, E | OutErr, OutDone2, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutDone2, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (o: OutDone) => Effect<OutDone2, E, R>,
  ): Channel<OutElem, OutErr | E, OutDone2, InElem, InErr, InDone, Env | R>;
};

mapEffect

Added in v2.0.0 Source

Maps each output element with an effectful function, preserving the source channel's done value.

When to use

Use when transforming each channel output needs an Effect, service dependency, failure channel, or configured concurrency.

Details

The mapping function receives the output element and its zero-based index. By default elements are mapped sequentially. Use options.concurrency to map multiple elements concurrently, and options.unordered to allow concurrently mapped outputs to be emitted as soon as they complete.

Signature

declare const mapEffect: {
  <OutElem, OutElem1, OutErr1, Env1>(
    f: (d: OutElem, i: number) => Effect<OutElem1, OutErr1, Env1>,
    options?: {
      readonly concurrency?: number | "unbounded";
      readonly unordered?: boolean;
    },
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem1, OutErr1 | OutErr, OutDone, InElem, InErr, InDone, Env1 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutElem1, OutErr1, Env1>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: OutElem, i: number) => Effect<OutElem1, OutErr1, Env1>,
    options?: {
      readonly concurrency?: number | "unbounded";
      readonly unordered?: boolean;
    },
  ): Channel<OutElem1, OutErr | OutErr1, OutDone, InElem, InErr, InDone, Env | Env1>;
};

mapInput

Added in v2.0.0 Source

Returns a new channel which is the same as this one but applies the given function to the input channelโ€™s input elements.

Signature

declare const mapInput: {
  <InElem, InElem2, InErr, R = never>(
    f: (i: InElem2) => Effect<InElem, InErr, R>,
  ): <OutElem, OutErr, OutDone, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, R | Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem2, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, InElem2, R = never>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (i: InElem2) => Effect<InElem, InErr, R>,
  ): Channel<OutElem, OutErr, OutDone, InElem2, InErr, InDone, Env | R>;
};

Returns a new channel which is the same as this one but applies the given function to the input errors.

Signature

declare const mapInputError: {
  <InErr, InErr2, R = never>(
    f: (i: InErr2) => Effect<InErr, InErr, R>,
  ): <OutElem, OutErr, OutDone, InElem, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, R | Env>,
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr2, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, InErr2, R = never>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (i: InErr2) => Effect<InErr, InErr, R>,
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr2, InDone, Env | R>;
};

Runs a fallback channel if this channel completes without emitting any output elements.

Details

If the source emits at least one element, the source is used unchanged. If the source completes before emitting an element, the fallback function receives the source done value and returns the replacement channel.

Signature

declare const orElseIfEmpty: {
  <OutDone, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    f: (
      leftover: NoInfer<OutDone>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): <OutElem, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1 | OutElem,
    OutErr1 | OutErr,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (
      leftover: NoInfer<OutDone>,
    ) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
  ): Channel<
    OutElem | OutElem1,
    OutErr | OutErr1,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

pipeTo

Added in v2.0.0 Source

Returns a new channel that pipes the output of this channel into the specified channel. The returned channel has the input type of this channel, and the output type of the specified channel, terminating with the value of the specified channel.

Signature

declare const pipeTo: {
  <OutElem2, OutErr2, OutDone2, OutElem, OutErr, OutDone, Env2>(
    that: Channel<OutElem2, OutErr2, OutDone2, OutElem, OutErr, OutDone, Env2>,
  ): <InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem2, OutErr2, OutDone2, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutElem2, OutErr2, OutDone2, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    that: Channel<OutElem2, OutErr2, OutDone2, OutElem, OutErr, OutDone, Env2>,
  ): Channel<OutElem2, OutErr2, OutDone2, InElem, InErr, InDone, Env | Env2>;
};

pipeToOrFail

Added in v2.0.0 Source

Returns a new channel that pipes the output of this channel into the specified channel and preserves this channel's failures without providing them to the other channel for observation.

Signature

declare const pipeToOrFail: {
  <OutElem2, OutErr2, OutDone2, OutElem, OutDone, Env2>(
    that: Channel<OutElem2, OutErr2, OutDone2, OutElem, never, OutDone, Env2>,
  ): <OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem2, OutErr2 | OutErr, OutDone2, InElem, InErr, InDone, Env2 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, OutElem2, OutErr2, OutDone2, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    that: Channel<OutElem2, OutErr2, OutDone2, OutElem, never, OutDone, Env2>,
  ): Channel<OutElem2, OutErr | OutErr2, OutDone2, InElem, InErr, InDone, Env | Env2>;
};

scan

Added in v4.0.0 Source

Transforms a channel statefully by scanning over its output with an accumulator function. Emits the intermediate results of the scan operation.

Signature

declare const scan: {
  <S, OutElem>(
    initial: S,
    f: (s: S, a: NoInfer<OutElem>) => S,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<S, OutErr, OutDone, InElem, InErr, InDone, Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, S>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    initial: S,
    f: (s: S, a: NoInfer<OutElem>) => S,
  ): Channel<S, OutErr, OutDone, InElem, InErr, InDone, Env>;
};

scanEffect

Added in v4.0.0 Source

Transforms a channel statefully by scanning over its output with an effectful accumulator function. Emits the intermediate results of the scan operation.

When to use

Use when maintaining accumulated state over channel output requires Effects or can fail, while still emitting each intermediate state.

Signature

declare const scanEffect: {
  <S, OutElem, E, R>(
    initial: S,
    f: (s: S, a: NoInfer<OutElem>) => Effect<S, E, R>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<S, E | OutErr, OutDone, InElem, InErr, InDone, R | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, S, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    initial: S,
    f: (s: S, a: NoInfer<OutElem>) => Effect<S, E, R>,
  ): Channel<S, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>;
};

schedule

Added in v4.0.0 Source

Runs a schedule step for each output element while preserving the emitted elements.

Details

The schedule receives each output element as input. Schedule delays are applied between emitted elements. If the schedule fails, the returned channel fails. If the schedule finishes, the returned channel completes with the schedule output.

Signature

declare const schedule: {
  <SO, OutElem, SE, SR>(
    schedule: Schedule<SO, NoInfer<OutElem>, SE, SR>,
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, SE | OutErr, OutDone, InElem, InErr, InDone, SR | Env>,
  ) => Channel<OutElem, SE | OutErr, SO | OutDone, InElem, InErr, InDone, SR | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, SO, SE, SR>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    schedule: Schedule<SO, OutElem, SE, SR>,
  ): Channel<OutElem, OutErr | SE, OutDone | SO, InElem, InErr, InDone, Env | SR>;
};

switchMap

Added in v4.0.0 Source

Maps each output element to a channel and emits values from the most recent active child channels.

Details

With the default concurrency of 1, starting a new child channel interrupts the previous child channel. Use options.concurrency to allow more active child channels. The source channel's done value is preserved.

Signature

declare const switchMap: {
  <OutElem, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    f: (d: OutElem) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    options?: {
      readonly bufferSize?: number;
      readonly concurrency?: number | "unbounded";
    },
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<
    OutElem1,
    OutErr1 | OutErr,
    OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >;
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1,
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: OutElem) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>,
    options?: {
      readonly bufferSize?: number;
      readonly concurrency?: number | "unbounded";
    },
  ): Channel<
    OutElem1,
    OutErr | OutErr1,
    OutDone,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env | Env1
  >;
};

tap

Added in v4.0.0 Source

Applies a side effect function to each output element of the channel, returning a new channel that emits the same elements.

Details

The tap function allows you to perform side effects (like logging or debugging) on each element emitted by a channel without modifying the elements themselves.

Signature

declare const tap: {
  <OutElem, X, OutErr1, Env1>(
    f: (d: NoInfer<OutElem>) => Effect<X, OutErr1, Env1>,
    options?: {
      readonly concurrency?: number | "unbounded";
    },
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  ) => Channel<OutElem, OutErr1 | OutErr, OutDone, InElem, InErr, InDone, Env1 | Env>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, X, OutErr1, Env1>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (d: NoInfer<OutElem>) => Effect<X, OutErr1, Env1>,
    options?: {
      readonly concurrency?: number | "unbounded";
    },
  ): Channel<OutElem, OutErr | OutErr1, OutDone, InElem, InErr, InDone, Env | Env1>;
};

Splitting

splitLines

Added in v2.0.0 Source

Splits upstream string chunks into lines, recognizing \n, \r\n, and standalone \r as line terminators. The behavior matches String.linesIterator regardless of how the input is chunked.

Details

A line terminator at the very end of the stream does not produce a trailing empty line (consistent with String.linesIterator). Conversely, if the stream ends without a terminator the final partial line is still emitted.

Signature

declare function splitLines<Err, Done>(): Channel<
  readonly [string, string],
  Err,
  Done,
  readonly [string, string],
  Err,
  Done
>;

Tracing

withSpan

Added in v2.0.0 Source

Runs the channel inside a tracing span with the specified name and options.

Details

The created span is provided as the current parent span while the channel runs. The span is ended with the channel's exit value.

Signature

declare const withSpan: {
  (
    name: string,
    options?: SpanOptions,
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, R>(
    self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R>,
  ) => Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<R, ParentSpan>>;
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, R>(
    self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R>,
    name: string,
    options?: SpanOptions,
  ): Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<R, ParentSpan>>;
};

Transforming

flattenArray

Added in v4.0.0 Source

Flattens a channel that outputs arrays into a channel that outputs individual elements.

Signature

declare function flattenArray<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(self: Channel<readonly Array<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>

flattenTake

Added in v4.0.0 Source

Flattens a channel that emits Take values into a channel that emits the Take outputs directly.

Details

Output Take values are emitted as non-empty arrays. Failed Take values fail the returned channel. Done Take values complete the returned channel.

Signature

declare function flattenTake<
  OutElem,
  OutErr,
  OutDone,
  OutErr2,
  OutDone2,
  InElem,
  InErr,
  InDone,
  Env,
>(
  self: Channel<Take<OutElem, OutErr, OutDone>, OutErr2, OutDone2, InElem, InErr, InDone, Env>,
): Channel<readonly [OutElem, OutElem], OutErr | OutErr2, OutDone, InElem, InErr, InDone, Env>;

Type IDs

TypeId

Added in v4.0.0 Source

Runtime identifier stored on Channel values and used by isChannel to recognize them.

Signature

declare const TypeId: "~effect/Channel";

TypeId type

Added in v4.0.0 Source

String literal type used as the unique brand for Channel values.

Signature

type TypeId = "~effect/Channel";