UNPKG

@johngw/stream

Version:

Reactive programming tools using the WHATWG Streams API.

41 lines 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ForkableRecallSink = void 0; const Symbol_1 = require("@johngw/stream-common/Symbol"); const ForkableSink_1 = require("@johngw/stream/sinks/ForkableSink"); /** * 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 * ``` */ class ForkableRecallSink extends ForkableSink_1.ForkableSink { #chunk = Symbol_1.empty; write(chunk) { this.#chunk = chunk; return super.write(chunk); } _addController(underlyingSource, queuingStrategy) { const [controller, stream] = super._addController(underlyingSource, queuingStrategy); if (this.#chunk !== Symbol_1.empty) controller.enqueue(this.#chunk); return [controller, stream]; } } exports.ForkableRecallSink = ForkableRecallSink; //# sourceMappingURL=ForkableRecallSink.js.map