@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
43 lines • 1.36 kB
JavaScript
import { all } from '@johngw/stream-common/Async';
import { isAbortableSink, isClosableSink, isStartableSink, isWritableSink, } from '@johngw/stream-common/Stream';
/**
* A collection of `UnderlyingSink`s that implement the `UnderlyingSink`.
*
* @group Sinks
* @example
* ```
* stream.pipeTo(
* new WritableStream(
* new SinkComposite([
* { write: (chunk) => console.info(`1 ${chunk}`) },
* { write: (chunk) => console.info(`2 ${chunk}`) },
* ])
* )
* )
* ```
*/
export class SinkComposite {
#abortableSinks;
#closableSinks;
#startableSinks;
#writableSinks;
constructor(sinks) {
this.#abortableSinks = sinks.filter(isAbortableSink);
this.#closableSinks = sinks.filter(isClosableSink);
this.#startableSinks = sinks.filter(isStartableSink);
this.#writableSinks = sinks.filter(isWritableSink);
}
async abort(reason) {
await all(this.#abortableSinks, (sink) => sink.abort(reason));
}
async close() {
await all(this.#closableSinks, (sink) => sink.close());
}
async start(controller) {
await all(this.#startableSinks, (sink) => sink.start(controller));
}
async write(chunk, controller) {
await all(this.#writableSinks, (sink) => sink.write(chunk, controller));
}
}
//# sourceMappingURL=SinkComposite.js.map