UNPKG

@johngw/stream

Version:

Reactive programming tools using the WHATWG Streams API.

46 lines (42 loc) 1.34 kB
import { empty, Empty } from '@johngw/stream-common/Symbol' import { ForkableSink } from '@johngw/stream/sinks/ForkableSink' import { ControllableSource } from '@johngw/stream/sources/ControllableSource' /** * An extension to the {@link ForkableSink:class} that immediately * queues the last received chunk to any fork. * * @group Sinks * @see {@link ForkableRecallStream:class} * @example * ``` * const forkable = new ForkableRecallSink<number>() * const writable = new WritableStream(forkable) * await fromCollection([1, 2, 3, 4, 5, 6, 7]).pipeTo(writable) * ``` * * Now the stream has finished, if we fork from it we'll * receive the last thing that was emitted. * * ``` * await forkable.fork().pipeTo(write(console.info)) * // 7 * ``` */ export class ForkableRecallSink<T> extends ForkableSink<T> { #chunk: T | Empty = empty override write(chunk: T) { this.#chunk = chunk return super.write(chunk) } protected override _addController( underlyingSource?: UnderlyingDefaultSource<T>, queuingStrategy?: QueuingStrategy<T> ): readonly [ControllableSource<T>, ReadableStream<T>] { const [controller, stream] = super._addController( underlyingSource, queuingStrategy ) if (this.#chunk !== empty) controller.enqueue(this.#chunk) return [controller, stream] } }