@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
37 lines • 1.16 kB
JavaScript
import { empty } from '@johngw/stream-common/Symbol';
import { ForkableSink } from '@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
* ```
*/
export class ForkableRecallSink extends ForkableSink {
#chunk = empty;
write(chunk) {
this.#chunk = chunk;
return super.write(chunk);
}
_addController(underlyingSource, queuingStrategy) {
const [controller, stream] = super._addController(underlyingSource, queuingStrategy);
if (this.#chunk !== empty)
controller.enqueue(this.#chunk);
return [controller, stream];
}
}
//# sourceMappingURL=ForkableRecallSink.js.map