Skip to content

Sink

Consumes values from a Stream and produces one final result.

A Sink may read no input, a fixed amount of input, or keep reading until a condition is met. If it reads more than it needs, it can return leftovers so the stream can continue from those values. Sinks are used to collect, fold, search, count, or otherwise reduce streamed input, and they can be composed when a stream needs more than one consuming step.

80 exports Added in v2.0.0 Source

Constructors

collect

Added in v4.0.0 Source

Accumulates incoming elements into an array.

When to use

Use when you need a sink result containing all upstream input elements.

See

  • take for collecting only a fixed number of input elements

Signature

declare function collect<In>(): Sink<Array<In>, In>;

count

Added in v2.0.0 Source

A sink that counts the number of elements fed to it.

When to use

Use to consume input and return only the number of elements received.

Signature

declare const count: Sink<number, unknown>;

die

Added in v2.0.0 Source

Creates a sink halting with a specified defect.

Signature

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

drain

Added in v2.0.0 Source

Consumes and ignores all stream inputs.

When to use

Use to consume all upstream input and complete with void when the input values and any aggregate result are not needed.

See

  • count for consuming all input while returning the number of elements
  • forEach for consuming all input while running an effect for each element

Signature

declare const drain: Sink<void, unknown>;

every

Added in v2.0.0 Source

A sink that returns whether all elements satisfy the specified predicate.

When to use

Use to reduce a stream to a boolean that is true only when every input satisfies a pure predicate.

See

  • some for the dual any-match check

Signature

declare function every<In>(predicate: Predicate<In>): Sink<boolean, In, In>;

fail

Added in v2.0.0 Source

A sink that always fails with the specified error.

Signature

declare function fail<E>(e: E): Sink<never, unknown, never, E>;

failCause

Added in v2.0.0 Source

Creates a sink halting with a specified Cause.

Signature

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

Creates a sink halting with a specified lazily evaluated Cause.

Signature

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

failSync

Added in v2.0.0 Source

A sink that always fails with the specified lazily evaluated error.

Signature

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

find

Added in v4.0.0 Source

Creates a sink containing the first value matched by a synchronous predicate.

When to use

Use to scan stream input until the first matching element is found and return that element as an Option.

Details

Returns Option.none if the upstream stream ends before a match is found. Refinement predicates narrow the returned value type. The matching input is consumed; any later elements from the same pulled array are returned as leftovers.

See

  • findEffect for an effectful predicate that can fail or require services

Signature

declare const find: {
  <In, Out>(refinement: Refinement<In, Out>): Sink<Option<Out>, In, In>;
  <In>(predicate: Predicate<In>): Sink<Option<In>, In, In>;
};

findEffect

Added in v2.0.0 Source

Creates a sink containing the first value matched by an effectful predicate.

When to use

Use when you need to run effects, fail, or use services while searching for the first matching input.

Details

Returns Option.some with the first input whose predicate result is true, or Option.none if the upstream stream ends first. If the predicate effect fails, the sink fails with the same error.

See

  • find for the synchronous predicate variant

Signature

declare function findEffect<In, E, R>(
  predicate: (input: In) => Effect<boolean, E, R>,
): Sink<Option<In>, In, In, E, R>;

forEach

Added in v2.0.0 Source

A sink that executes the provided effectful function for every item fed to it.

Signature

declare function forEach<In, X, E, R>(
  f: (input: In) => Effect<X, E, R>,
): Sink<void, In, never, E, R>;

forEachArray

Added in v4.0.0 Source

A sink that executes the provided effectful function for every Chunk fed to it.

Signature

declare function forEachArray<In, X, E, R>(
  f: (input: readonly [In, In]) => Effect<X, E, R>,
): Sink<void, In, never, E, R>;

forEachWhile

Added in v2.0.0 Source

Runs an effectful function for each input element while it returns true.

Details

The sink stops consuming input when the function returns false or when the upstream stream ends, and completes with void.

Signature

declare function forEachWhile<In, E, R>(
  f: (input: In) => Effect<boolean, E, R>,
): Sink<void, In, never, E, R>;

Runs an effectful function for each non-empty input array while it returns true.

Details

The sink stops consuming input when the function returns false or when the upstream stream ends, and completes with void.

Signature

declare function forEachWhileArray<In, E, R>(
  f: (input: readonly [In, In]) => Effect<boolean, E, R>,
): Sink<void, In, never, E, R>;

fromChannel

Added in v2.0.0 Source

Creates a sink from a Channel.

When to use

Use to create a Sink from a Channel that processes non-empty arrays of input values.

See

  • toChannel for converting a Sink back to a Channel

Signature

declare function fromChannel<L, In, E, A, R>(
  channel: Channel<never, E, End<A, L>, readonly [In, In], never, void, R>,
): Sink<A, In, L, E, R>;

fromEffect

Added in v2.0.0 Source

Creates a sink that ignores upstream input and completes with the success value of the provided effect.

Details

If the effect fails, the sink fails with the same error.

Signature

declare function fromEffect<A, E, R>(effect: Effect<A, E, R>): Sink<A, unknown, never, E, R>;

Creates a sink that ignores upstream input and completes from an effect that already returns an End.

When to use

Use when you need to create a sink from an effect that returns both the sink result value and optional leftovers.

Signature

declare function fromEffectEnd<A, E, R, L = never>(
  effect: Effect<End<A, L>, E, R>,
): Sink<A, unknown, L, E, R>;

fromPubSub

Added in v2.0.0 Source

Creates a sink that publishes every consumed input element to a PubSub.

Details

The sink completes with void when the upstream stream ends.

Signature

declare function fromPubSub<A>(pubsub: PubSub<A>): Sink<void, A>;

fromQueue

Added in v2.0.0 Source

Creates a sink that offers every consumed input element to a queue.

Details

When the upstream stream ends, the sink ends the queue and completes with void.

Signature

declare function fromQueue<A>(queue: Queue<A, Done<void>>): Sink<void, A>;

Creates a Sink from a low-level transform function.

Details

The transform receives the upstream pull of non-empty input arrays and the active scope, and returns an effect that completes with the sink's End value.

Signature

declare function fromTransform<In, A, E, R, L = never>(
  transform: (
    upstream: Pull<readonly [In, In], never, void>,
    scope: Scope,
  ) => Effect<End<A, L>, E, R>,
): Sink<A, In, L, E, R>;

Creates a sink that writes its input to a Web WritableStream.

Signature

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

last

Added in v2.0.0 Source

Creates a sink containing the last value.

When to use

Use when you need to consume all upstream input and keep only the final element.

Details

Returns Option.some(last) with the final input value, or Option.none when the upstream ends without input.

Gotchas

This sink produces a result only when the upstream ends, so it does not complete for a stream that does not end.

See

  • head for taking the first input value instead

Signature

declare function last<In>(): Sink<Option<In>, In>;

make

Added in v4.0.0 Source

Creates a pipe-style constructor for sinks over input type In.

Details

The returned function exposes the sink input as a Stream<In>, applies the provided pipeline, and uses the final effect's success value as the sink result.

Signature

declare function make<In>(): Constructor<In>;

never

Added in v2.0.0 Source

A sink that never completes.

Signature

declare const never: Sink<unknown>;

some

Added in v2.0.0 Source

A sink that returns whether an element satisfies the specified predicate.

When to use

Use to reduce a stream to a boolean that is true when any input satisfies a pure predicate.

See

  • every for the all-match check

Signature

declare function some<In>(predicate: Predicate<In>): Sink<boolean, In, In>;

succeed

Added in v2.0.0 Source

A sink that immediately ends with the specified value.

Signature

declare function succeed<A, L = never>(a: A, leftovers?: readonly [L, L]): Sink<A, unknown, L>;

sum

Added in v2.0.0 Source

Creates a sink which sums up its inputs.

Signature

declare const sum: Sink<number, number>;

suspend

Added in v2.0.0 Source

A sink that is created from a lazily evaluated sink.

Signature

declare function suspend<A, In, L, E, R>(
  evaluate: LazyArg<Sink<A, In, L, E, R>>,
): Sink<A, In, L, E, R>;

sync

Added in v2.0.0 Source

A sink that immediately ends with the specified lazily evaluated value.

Signature

declare function sync<A>(a: LazyArg<A>): Sink<A>;

take

Added in v2.0.0 Source

Collects up to n input elements into an array.

Details

If n is less than or equal to zero, the sink completes with an empty array. If more elements are pulled than needed, the remaining elements from the same array are returned as leftovers.

Signature

declare function take<In>(n: number): Sink<Array<In>, In, In>;

takeUntil

Added in v4.0.0 Source

Collects input elements until the predicate returns true, including the matching element in the result.

Signature

declare function takeUntil<In>(predicate: Predicate<In>): Sink<Array<In>, In, In>;

Collects input elements effectfully until the predicate returns true, including the matching element in the result.

Details

If the predicate effect fails, the sink fails with the same error.

Signature

declare function takeUntilEffect<In, E, R>(
  predicate: (input: In) => Effect<boolean, E, R>,
): Sink<Array<In>, In, In, E, R>;

takeWhile

Added in v4.0.0 Source

Collects the longest input prefix whose elements satisfy the predicate or refinement.

Details

The first failing input is consumed and excluded from the result. Any later elements from the same pulled array are returned as leftovers.

Signature

declare const takeWhile: {
  <In, Out>(refinement: Refinement<In, Out>): Sink<Array<Out>, In, In>;
  <In>(predicate: Predicate<In>): Sink<Array<In>, In, In>;
};

Collects input elements effectfully while the predicate succeeds.

Details

The first input for which the predicate returns false is consumed and excluded from the result. Any later elements from the same pulled array are returned as leftovers.

Signature

declare const takeWhileEffect: <In, E, R>(
  predicate: (input: In) => Effect<boolean, E, R>,
) => Sink<Array<In>, In, In, E, R>;

Applies a Filter to input elements while it succeeds, collecting each successful output.

Details

The first input for which the filter fails is consumed and excluded from the result. Any later elements from the same pulled array are returned as leftovers.

Signature

declare function takeWhileFilter<In, Out, X>(filter: Filter<In, Out, X>): Sink<Array<Out>, In, In>;

Applies a FilterEffect to input elements effectfully while it succeeds, collecting each successful output.

Details

The first input for which the filter fails is consumed and excluded from the result. Any later elements from the same pulled array are returned as leftovers.

Signature

declare function takeWhileFilterEffect<In, Out, X, E, R>(
  filter: FilterEffect<In, Out, X, E, R>,
): Sink<Array<Out>, In, In, E, R>;

timed

Added in v2.0.0 Source

A sink that drains all input and returns the elapsed duration.

Signature

declare const timed: Sink<Duration.Duration, unknown>;

toChannel

Added in v2.0.0 Source

Creates a Channel from a Sink.

Signature

declare function toChannel<A, In, L, E, R>(
  self: Sink<A, In, L, E, R>,
): Channel<never, E, End<A, L>, readonly [In, In], never, void, R>;

unwrap

Added in v2.0.0 Source

Creates a sink produced from a scoped effect.

Signature

declare function unwrap<A, In, L, E, R, R2>(
  effect: Effect<Sink<A, In, L, E, R2>, E, R>,
): Sink<A, In, L, E, R2 | Exclude<R, Scope>>;

Error Handling

catchCause

Added in v4.0.0 Source

Handles failures from this sink by inspecting the full Cause.

When to use

Use to recover from a sink failure based on the full Cause instead of only the typed error value.

Details

When this sink fails, the handler effect is run and its success value becomes the sink result. If the handler fails, the returned sink fails with that error.

See

  • catch for recovering from typed errors only
  • orElse for recovering by switching to another sink

Signature

declare const catchCause: {
  <E, A2, E2, R2>(
    f: (error: Cause<NoInfer<E>>) => Effect<A2, E2, R2>,
  ): <A, In, L, R>(self: Sink<A, In, L, E, R>) => Sink<A2 | A, In, L, E, R2 | R>;
  <A, In, L, E, R, A2, E2, R2>(
    self: Sink<A, In, L, E, R>,
    f: (error: Cause<E>) => Effect<A2, E2, R2>,
  ): Sink<A | A2, In, L, E2, R | R2>;
};

orElse

Added in v2.0.0 Source

Runs a fallback sink if this sink fails with a typed error.

Details

The fallback is built from the error and continues consuming from the same upstream stream. If the upstream stream had already ended, the fallback sees the upstream end instead.

Signature

declare const orElse: {
  <E, A2, In2, L2, E2, R2>(
    f: (error: NoInfer<E>) => Sink<A2, In2, L2, E2, R2>,
  ): <A, In, L, R>(self: Sink<A, In, L, E, R>) => Sink<A2 | A, In & In2, L2 | L, E | E2, R2 | R>;
  <A, In, L, E, R, A2, In2, L2, E2, R2>(
    self: Sink<A, In, L, E, R>,
    f: (error: E) => Sink<A2, In2, L2, E2, R2>,
  ): Sink<A | A2, In & In2, L | L2, E | E2, R | R2>;
};

Filtering

Drops leftovers produced by a sink.

Details

The sink result is preserved, but any leftover elements are discarded instead of being returned to downstream sink composition. This does not continue pulling additional elements from the upstream stream.

Signature

declare function ignoreLeftover<A, In, L, E, R>(
  self: Sink<A, In, L, E, R>,
): Sink<A, In, never, E, R>;

Folding

fold

Added in v2.0.0 Source

A sink that folds its inputs with the provided function, termination predicate and initial state.

When to use

Use to accumulate stream input element by element with an effectful step and stop based on the accumulated state.

Details

The initial state is evaluated lazily. Each input element is folded with the effectful function, and the sink continues while contFn returns true. If the sink stops in the middle of a pulled array, the remaining elements from that array are returned as leftovers.

See

  • foldArray for folding each pulled non-empty input array at once
  • foldUntil for folding until a fixed maximum number of elements is consumed

Signature

declare function fold<S, In, E = never, R = never>(
  s: LazyArg<S>,
  contFn: Predicate<S>,
  f: (s: S, input: In) => Effect<S, E, R>,
): Sink<S, In, In, E, R>;

foldArray

Added in v4.0.0 Source

Folds non-empty input arrays into state with an effectful function.

When to use

Use to update state with an effectful function once per pulled non-empty input array when batch-level processing is the natural unit.

Details

The initial state is evaluated lazily. After each pulled array is folded, the sink continues while contFn returns true; otherwise it completes with the current state.

See

  • fold for folding element by element and returning leftovers when stopping mid-array
  • reduceWhileArrayEffect for array-level effectful reducing that checks the predicate before consuming input

Signature

declare function foldArray<S, In, E = never, R = never>(
  s: LazyArg<S>,
  contFn: Predicate<S>,
  f: (s: S, input: readonly [In, In]) => Effect<S, E, R>,
): Sink<S, In, never, E, R>;

foldUntil

Added in v2.0.0 Source

Folds input elements into state until the specified maximum number of elements has been consumed or the upstream stream ends.

Details

If the sink stops in the middle of a pulled array, the remaining elements from that array are returned as leftovers.

Signature

declare function foldUntil<S, In, E = never, R = never>(
  s: LazyArg<S>,
  max: number,
  f: (s: S, input: In) => Effect<S, E, R>,
): Sink<S, In, In, E, R>;

reduce

Added in v4.0.0 Source

A sink that reduces its inputs using the provided function f starting from the provided initial state.

Signature

declare function reduce<S, In>(initial: LazyArg<S>, f: (s: S, input: In) => S): Sink<S, In>;

reduceArray

Added in v4.0.0 Source

A sink that reduces its inputs using the provided function f starting from the specified initial state.

Signature

declare function reduceArray<S, In>(
  initial: LazyArg<S>,
  f: (s: S, input: readonly [In, In]) => S,
): Sink<S, In>;

reduceEffect

Added in v4.0.0 Source

A sink that reduces its inputs using the provided effectful function f starting from the specified initial state.

Signature

declare function reduceEffect<S, In, E, R>(
  initial: LazyArg<S>,
  f: (s: S, input: In) => Effect<S, E, R>,
): Sink<S, In, never, E, R>;

reduceWhile

Added in v4.0.0 Source

A sink that reduces input elements from the provided initial state with f while the specified predicate returns true.

Signature

declare function reduceWhile<S, In>(
  initial: LazyArg<S>,
  predicate: Predicate<S>,
  f: (s: S, input: In) => S,
): Sink<S, In, In>;

A sink that reduces non-empty input arrays from the provided initial state with f while the specified predicate returns true.

Signature

declare function reduceWhileArray<S, In>(
  initial: LazyArg<S>,
  contFn: Predicate<S>,
  f: (s: S, input: readonly [In, In]) => S,
): Sink<S, In>;

A sink that effectfully reduces non-empty input arrays from the provided initial state with f while the specified predicate returns true.

Signature

declare function reduceWhileArrayEffect<S, In, E, R>(
  initial: LazyArg<S>,
  predicate: Predicate<S>,
  f: (s: S, input: readonly [In, In]) => Effect<S, E, R>,
): Sink<S, In, never, E, R>;

A sink that effectfully reduces input elements from the provided initial state with f while the specified predicate returns true.

Signature

declare function reduceWhileEffect<S, In, E, R>(
  initial: LazyArg<S>,
  predicate: Predicate<S>,
  f: (s: S, input: In) => Effect<S, E, R>,
): Sink<S, In, In, E, R>;

Guards

isSink

Added in v4.0.0 Source

Checks whether a value is a Sink.

Signature

declare function isSink(u: unknown): u is Sink<unknown, never, unknown, unknown, unknown>;

Mapping

as

Added in v2.0.0 Source

Sets the sink's result to a constant value.

When to use

Use to keep a sink's input consumption, errors, requirements, and leftovers while replacing only its result with a known value.

See

  • map for computing the replacement from the original result

Signature

declare const as: {
  <A2>(a2: A2): <A, In, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A2, In, L, E, R>;
  <A, In, L, E, R, A2>(self: Sink<A, In, L, E, R>, a2: A2): Sink<A2, In, L, E, R>;
};

map

Added in v2.0.0 Source

Transforms this sink's result.

When to use

Use to compute a new result from the original sink result while preserving the sink's input consumption behavior.

Details

The transformed sink preserves the original sink's input type, leftovers, errors, and requirements.

See

  • mapEffect for effectful result transformations
  • as for replacing the result with a constant value
  • mapEnd for transforming both the result and leftovers

Signature

declare const map: {
  <A, A2>(f: (a: A) => A2): <In, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A2, In, L, E, R>;
  <A, In, L, E, R, A2>(self: Sink<A, In, L, E, R>, f: (a: A) => A2): Sink<A2, In, L, E, R>;
};

mapEffect

Added in v2.0.0 Source

Transforms this sink's result effectfully.

When to use

Use when you need a sink result transformation that is effectful, can fail, or requires services.

Details

The transformed sink preserves the original sink's input consumption and leftovers while adding the errors and requirements of the transformation.

See

  • map for pure result transformations
  • mapEffectEnd for effectfully transforming both the result and leftovers
  • flatMap for continuing with another sink based on the result

Signature

declare const mapEffect: {
  <A, A2, E2, R2>(
    f: (a: A) => Effect<A2, E2, R2>,
  ): <In, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A2, In, L, E2 | E, R2 | R>;
  <A, In, L, E, R, A2, E2, R2>(
    self: Sink<A, In, L, E, R>,
    f: (a: A) => Effect<A2, E2, R2>,
  ): Sink<A2, In, L, E | E2, R | R2>;
};

mapEffectEnd

Added in v4.0.0 Source

Transforms the full End produced by this sink effectfully.

Details

This can change both the result value and the optional leftovers, and the transformation can fail or require services.

Signature

declare const mapEffectEnd: {
  <A, L, A2, E2, R2, L2 = never>(
    f: (end: End<A, L>) => Effect<End<A2, L2>, E2, R2>,
  ): <In, E, R>(self: Sink<A, In, L, E, R>) => Sink<A2, In, L2, E2 | E, R2 | R>;
  <A, In, L, E, R, A2, E2, R2, L2 = never>(
    self: Sink<A, In, L, E, R>,
    f: (end: End<A, L>) => Effect<End<A2, L2>, E2, R2>,
  ): Sink<A2, In, L2, E | E2, R | R2>;
};

mapEnd

Added in v4.0.0 Source

Transforms the full End produced by this sink.

Details

This can change both the result value and the optional leftovers.

Signature

declare const mapEnd: {
  <A, L, A2, L2 = never>(
    f: (a: End<A, L>) => End<A2, L2>,
  ): <In, E, R>(self: Sink<A, In, L, E, R>) => Sink<A2, In, L2, E, R>;
  <A, In, L, E, R, A2, L2 = never>(
    self: Sink<A, In, L, E, R>,
    f: (a: End<A, L>) => End<A2, L2>,
  ): Sink<A2, In, L2, E, R>;
};

mapError

Added in v2.0.0 Source

Transforms the errors emitted by this sink using f.

Signature

declare const mapError: {
  <E, E2>(f: (error: E) => E2): <A, In, L, R>(self: Sink<A, In, L, E, R>) => Sink<A, In, L, E2, R>;
  <A, In, L, E, R, E2>(self: Sink<A, In, L, E, R>, f: (error: E) => E2): Sink<A, In, L, E2, R>;
};

mapInput

Added in v2.0.0 Source

Transforms this sink's input elements.

Signature

declare const mapInput: {
  <In0, In>(
    f: (input: In0) => In,
  ): <A, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In0, L, E, R>;
  <A, In, L, E, R, In0>(self: Sink<A, In, L, E, R>, f: (input: In0) => In): Sink<A, In0, L, E, R>;
};

Transforms each non-empty array of upstream input before it is fed to this sink.

Signature

declare const mapInputArray: {
  <In0, In>(
    f: (input: readonly [In0, In0]) => readonly [In, In],
  ): <A, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In0, L, E, R>;
  <A, In, L, E, R, In0>(
    self: Sink<A, In, L, E, R>,
    f: (input: readonly [In0, In0]) => readonly [In, In],
  ): Sink<A, In0, L, E, R>;
};

Transforms each non-empty array of upstream input effectfully before it is fed to this sink.

Signature

declare const mapInputArrayEffect: {
  <In0, In, E2, R2>(
    f: (input: readonly [In0, In0]) => Effect<readonly [In, In], E2, R2>,
  ): <A, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In0, L, E2 | E, R2 | R>;
  <A, In, L, E, R, In0, E2, R2>(
    self: Sink<A, In, L, E, R>,
    f: (input: readonly [In0, In0]) => Effect<readonly [In, In], E2, R2>,
  ): Sink<A, In0, L, E | E2, R | R2>;
};

Transforms this sink's input elements effectfully.

Signature

declare const mapInputEffect: {
  <In0, In, E2, R2>(
    f: (input: In0) => Effect<In, E2, R2>,
  ): <A, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In0, L, E2 | E, R2 | R>;
  <A, In, L, E, R, In0, E2, R2>(
    self: Sink<A, In, L, E, R>,
    f: (input: In0) => Effect<In, E2, R2>,
  ): Sink<A, In0, L, E | E2, R | R2>;
};

mapLeftover

Added in v2.0.0 Source

Transforms the leftovers emitted by this sink using f.

Signature

declare const mapLeftover: {
  <L, L2>(
    f: (leftover: L) => L2,
  ): <A, In, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In, L2, E, R>;
  <A, In, L, E, R, L2>(self: Sink<A, In, L, E, R>, f: (leftover: L) => L2): Sink<A, In, L2, E, R>;
};

summarized

Added in v2.0.0 Source

Runs a summary effect when the sink starts and again when it completes.

Signature

declare const summarized: {
  <A2, E2, R2, A3>(
    summary: Effect<A2, E2, R2>,
    f: (start: A2, end: A2) => A3,
  ): <A, In, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<[A, A3], In, L, E2 | E, R2 | R>;
  <A, In, L, E, R, A2, E2, R2, A3>(
    self: Sink<A, In, L, E, R>,
    summary: Effect<A2, E2, R2>,
    f: (start: A2, end: A2) => A3,
  ): Sink<[A, A3], In, L, E | E2, R | R2>;
};

withDuration

Added in v2.0.0 Source

Returns the sink that executes this one and times its execution.

Signature

declare function withDuration<A, In, L, E, R>(
  self: Sink<A, In, L, E, R>,
): Sink<[A, Duration], In, L, E, R>;

Models

End type

Added in v4.0.0 Source

Tuple returned when a Sink finishes.

Details

The first element is the sink result. The optional second element contains a non-empty array of leftover input that was pulled but not consumed.

Signature

type End<A, L = never> = readonly [value: A, leftover?: NonEmptyReadonlyArray<L> | undefined];

Sink interface

Added in v2.0.0 Source

A Sink<A, In, L, E, R> is used to consume elements produced by a Stream. You can think of a sink as a function that will consume a variable amount of In elements (could be 0, 1, or many), might fail with an error of type E, and will eventually yield a value of type A together with a remainder of type L (i.e. any leftovers).

Signature

interface Sink<out A, in In = unknown, out L = never, out E = never, out R = never>
  extends Variance<A, In, L, E, R>, Pipeable {
  [ignoreSymbol]?: SinkUnifyIgnore;
  [typeSymbol]?: unknown;
  [unifySymbol]?: SinkUnify<Sink<A, In, L, E, R>>;
  readonly transform: (
    upstream: Pull<readonly [In, In], never, void>,
    scope: Scope,
  ) => Effect<End<A, L>, E, R>;
}

SinkUnify interface

Added in v2.0.0 Source

Type-level unification support for Sink values.

Details

This preserves the result, input, leftover, error, and service type parameters when Effect's Unify machinery normalizes generic values that include sinks. Users normally do not need to reference this interface directly.

Signature

interface SinkUnify<
  A extends {
    [typeSymbol]?: any;
  },
> extends EffectUnify<A> {
  Sink?: () => A[typeof typeSymbol] extends Sink<A, In, L, E, R> | _ ? Sink<A, In, L, E, R> : never;
}

SinkUnifyIgnore interface

Added in v2.0.0 Source

Marker used by Effect's Unify machinery for Sink values.

Details

It prevents the inherited Effect unifier from being selected when sink-specific unification should preserve the Sink type parameters. Users normally do not need to reference this interface directly.

Signature

interface SinkUnifyIgnore {
  Effect?: true;
}

Other

Signature

declare const catch: {
  <E, A2, E2, R2>(f: (error: NoInfer<E>) => Effect<A2, E2, R2>): <A, In, L, R>(self: Sink<A, In, L, E, R>) => Sink<A2 | A, In, L, E2, R2 | R>;
  <A, In, L, E, R, A2, E2, R2>(self: Sink<A, In, L, E, R>, f: (error: E) => Effect<A2, E2, R2>): Sink<A | A2, In, L, E2, R | R2>;
}

make

Added in v4.0.0 Source

Companion namespace containing overload types for the pipe-style sink constructor returned by Sink.make.

Sink

Added in v2.0.0 Source

Namespace containing types and interfaces for Sink variance and type relationships.

Providing Services

Provides a Context to this sink.

Details

Services contained in the provided context are removed from the sink's service requirements.

Signature

declare const provideContext: {
  <Provided>(
    context: Context<Provided>,
  ): <A, In, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In, L, E, Exclude<R, Provided>>;
  <A, In, L, E, R, Provided>(
    self: Sink<A, In, L, E, R>,
    context: Context<Provided>,
  ): Sink<A, In, L, E, Exclude<R, Provided>>;
};

Provides a single service implementation to this sink.

Details

The service identified by key is removed from the sink's service requirements.

Signature

declare const provideService: {
  <I, S>(
    key: Key<I, S>,
    value: NoInfer<S>,
  ): <A, In, L, E, R>(self: Sink<A, In, L, E, R>) => Sink<A, In, L, E, Exclude<R, I>>;
  <A, In, L, E, R, I, S>(
    self: Sink<A, In, L, E, R>,
    key: Key<I, S>,
    value: NoInfer<S>,
  ): Sink<A, In, L, E, Exclude<R, I>>;
};

Resource Management

ensuring

Added in v2.0.0 Source

Runs a finalizer effect after this sink completes, fails, or is interrupted.

Details

The original sink result and leftovers are preserved unless the finalizer itself fails.

Signature

declare const ensuring: {
  <X, E2, R2>(
    effect: Effect<X, E2, R2>,
  ): <A, E, In, L, R>(self: Sink<A, In, L, E, R>) => Sink<A, In, L, E2 | E, R2 | R>;
  <A, In, L, E, R, X, E2, R2>(
    self: Sink<A, In, L, E, R>,
    effect: Effect<X, E2, R2>,
  ): Sink<A, In, L, E | E2, R | R2>;
};

onExit

Added in v4.0.0 Source

Runs an effect after this sink completes, fails, or is interrupted.

Details

The effect receives the sink's Exit for the result value. The original sink result and leftovers are preserved unless the finalizer itself fails.

Signature

declare const onExit: {
  <A, E, X, E2, R2>(
    f: (exit: Exit<A, E>) => Effect<X, E2, R2>,
  ): <In, L, R>(self: Sink<A, In, L, E, R>) => Sink<A, In, L, E | E2, R2 | R>;
  <A, In, L, E, R, X, E2, R2>(
    self: Sink<A, In, L, E, R>,
    f: (exit: Exit<A, E>) => Effect<X, E2, R2>,
  ): Sink<A, In, L, E | E2, R | R2>;
};

Sequencing

flatMap

Added in v2.0.0 Source

Runs this sink until it yields a result, then uses that result to create another sink from the provided function which will continue to run until it yields a result.

When to use

Use to compose sinks when the next sink depends on the result produced by the previous sink.

Details

Leftovers from the first sink are fed to the sink returned by f before more upstream input is pulled.

See

  • map for transforming the result without switching sinks
  • mapEffect for effectfully transforming the result without switching sinks

Signature

declare const flatMap: {
  <A, A1, L, In1, L1, E1, R1>(
    f: (a: A) => Sink<A1, In1, L1, E1, R1>,
  ): <In, E, R>(self: Sink<A, In, L, E, R>) => Sink<A1, In & In1, L | L1, E1 | E, R1 | R>;
  <A, In, L, E, R, A1, In1, L1, E1, R1>(
    self: Sink<A, In, L, E, R>,
    f: (a: A) => Sink<A1, In1, L1, E1, R1>,
  ): Sink<A1, In & In1, L | L1, E | E1, R | R1>;
};