Hyperlinkv0.8.0-beta.28

Channel

Channel.bufferconsteffect/Channel.ts:6747
(
  options:
    | { readonly capacity: "unbounded" }
    | {
        readonly capacity: number
        readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
      }
): <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" | undefined
      }
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>

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.

BufferingbufferArray
Source effect/Channel.ts:674738 lines
export const buffer: {
  (
    options: { readonly capacity: "unbounded" } | {
      readonly capacity: number
      readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
    }
  ): <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" | undefined
    }
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
} = dual(2, <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" | undefined
  }
): Channel<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.offer(queue, value)),
      Effect.forever({ disableYield: true }),
      Effect.onError((cause) => Queue.failCause(queue, cause)),
      Effect.forkIn(scope)
    )
    return Queue.take(queue)
  })))
Referenced by 1 symbols