Hyperlinkv0.8.0-beta.28

Sink

Sink.Sinkinterfaceeffect/Sink.ts:66
Sink<A, In, L, E, R>

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

Example (Running a sink with a stream)

import { Effect, Sink, Stream } from "effect"

// Create a simple sink that always succeeds with a value
const sink: Sink.Sink<number> = Sink.succeed(42)

// Use the sink to consume a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)

Effect.runPromise(program).then(console.log)
// Output: 42
models
Source effect/Sink.ts:66110 lines
export interface Sink<out A, in In = unknown, out L = never, out E = never, out R = never>
  extends Sink.Variance<A, In, L, E, R>, Pipeable
{
  readonly transform: (
    upstream: Pull.Pull<NonEmptyReadonlyArray<In>, never, void>,
    scope: Scope.Scope
  ) => Effect.Effect<End<A, L>, E, R>
  [Unify.typeSymbol]?: unknown
  [Unify.unifySymbol]?: SinkUnify<this>
  [Unify.ignoreSymbol]?: SinkUnifyIgnore
}

/**
 * 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.
 *
 * @category models
 * @since 4.0.0
 */
export type End<A, L = never> = readonly [value: A, leftover?: NonEmptyReadonlyArray<L> | undefined]

const endVoid = Effect.succeed([void 0] as End<void, never>)

/**
 * 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.
 *
 * @category models
 * @since 2.0.0
 */
export interface SinkUnify<A extends { [Unify.typeSymbol]?: any }> extends Effect.EffectUnify<A> {
  Sink?: () => A[Unify.typeSymbol] extends
    | Sink<
      infer A,
      infer In,
      infer L,
      infer E,
      infer R
    >
    | infer _ ? Sink<A, In, L, E, R>
    : never
}

/**
 * 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.
 *
 * @category models
 * @since 2.0.0
 */
export interface SinkUnifyIgnore {
  Effect?: true
}

/**
 * Namespace containing types and interfaces for Sink variance and type relationships.
 *
 * @since 2.0.0
 */
export declare namespace Sink {
  /**
   * Type-level variance marker for `Sink`.
   *
   * **Details**
   *
   * The result `A`, leftovers `L`, errors `E`, and services `R` are
   * covariant. The input type `In` is contravariant because values flow into
   * the sink.
   *
   * @category models
   * @since 2.0.0
   */
  export interface Variance<out A, in In, out L, out E, out R> {
    readonly [TypeId]: VarianceStruct<A, In, L, E, R>
  }
  /**
   * Structural encoding used by `Sink.Variance` to record each `Sink` type
   * parameter's variance.
   *
   * **Details**
   *
   * `_A`, `_L`, `_E`, and `_R` are covariant markers. `_In` is a
   * contravariant marker.
   *
   * @category models
   * @since 2.0.0
   */
  export interface VarianceStruct<out A, in In, out L, out E, out R> {
    _A: Types.Covariant<A>
    _In: Types.Contravariant<In>
    _L: Types.Covariant<L>
    _E: Types.Covariant<E>
    _R: Types.Covariant<R>
  }
}
Referenced by 82 symbols