@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
63 lines • 2.77 kB
JavaScript
import { overrideObject } from '@johngw/stream-common/Object';
/**
* A much less abstract version of the {@see TransformStream} that lets you interact
* with both the `Readable` and `Writable` side of the stream.
*
* @remarks
* We're implementing the {@see ReadableWritablePair} interface here, but the name
* `WritableReadablePair` is closer to the truth. Chunks are first given to the `WritableStream`
* which is then read from the `ReadableStream`.
*/
export class WritableReadablePair {
writable;
readable;
constructor(sink = {}, source = {}, { readableStrategy = new CountQueuingStrategy({ highWaterMark: 0 }), writableStrategy, } = {}) {
this.writable = new WritableStream({
start: async (controller) => {
this.
this.
error: (controller, reason) => {
controller.error(reason);
this.
},
});
await sink.start?.(this.
},
abort: async (reason) => {
await sink.abort?.(reason);
this.
},
close: async () => {
await sink.close?.();
this.
},
write: (chunk) => sink.write?.(chunk, this.
}, writableStrategy);
this.readable = new ReadableStream({
start: async (controller) => {
this.
this.
close: (controller) => {
controller.close();
this.
},
error: (controller, reason) => {
controller.error(reason);
this.
},
});
await source.start?.(this.
},
cancel: async (reason) => {
await source.cancel?.(reason);
this.
},
pull: () => source.pull?.(this.
}, readableStrategy);
}
}
//# sourceMappingURL=WritableReadablePair.js.map