Hyperlinkv0.8.0-beta.28

Channel

Channel.bufferArrayconsteffect/Channel.ts:6813
(
  options:
    | { readonly capacity: "unbounded" }
    | {
        readonly capacity: number
        readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
      }
): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<
    Arr.NonEmptyReadonlyArray<OutElem>,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >
) => Channel<
  Arr.NonEmptyReadonlyArray<OutElem>,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>
<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<
    Arr.NonEmptyReadonlyArray<OutElem>,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >,
  options:
    | { readonly capacity: "unbounded" }
    | {
        readonly capacity: number
        readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
      }
): Channel<
  Arr.NonEmptyReadonlyArray<OutElem>,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>

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.

Bufferingbuffer
Source effect/Channel.ts:681338 lines
export const bufferArray: {
  (
    options: { readonly capacity: "unbounded" } | {
      readonly capacity: number
      readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
    }
  ): <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>
  ) => Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>,
    options: { readonly capacity: "unbounded" } | {
      readonly capacity: number
      readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
    }
  ): Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>
} = dual(2, <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>,
  options: { readonly capacity: "unbounded" } | {
    readonly capacity: number
    readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
  }
): Channel<Arr.NonEmptyReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env> =>
  fromTransform(Effect.fnUntraced(function*(upstream, scope) {
    const pull = yield* toTransform(self)(upstream, scope)
    const queue = yield* Queue.make<OutElem, OutErr | Cause.Done<OutDone>>({
      capacity: options.capacity === "unbounded" ? undefined : options.capacity,
      strategy: options.capacity === "unbounded" ? undefined : options.strategy
    })
    yield* Scope.addFinalizer(scope, Queue.shutdown(queue))
    yield* pull.pipe(
      Effect.flatMap((value) => Queue.offerAll(queue, value)),
      Effect.forever({ disableYield: true }),
      Effect.onError((cause) => Queue.failCause(queue, cause)),
      Effect.forkIn(scope)
    )
    return Queue.takeAll(queue)
  })))
Referenced by 1 symbols