@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
56 lines • 1.38 kB
JavaScript
import { ControllableSource } from '@johngw/stream/sources/ControllableSource';
/**
* A ControllableStream is ReadableStream that can have chunks
* queued to from an external source.
*
* @group Sources
* @example
* Queuing items externally.
*
* ```
* const controller = new ControllableStream<number>()
* controller.enqueue(1)
* controller.enqueue(2)
* controller.enqueue(3)
* controller.close()
* ```
*
* Registering pull subscribers externally.
*
* ```
* const controller = new ControllableStream<number>()
* let i = -1
* controller.onPull(() => ++i)
* ```
*/
export class ControllableStream extends ReadableStream {
#source;
constructor(strategy) {
const source = new ControllableSource();
super(source, strategy);
this.#source = source;
}
get desiredSize() {
return this.#source.desiredSize;
}
/**
* Register a pull subscriber.
*
* @remark
* When the stream is ready to pull it will pull from all
* subscribers until the desired size has been fulfilled.
*/
onPull(pullListener) {
return this.#source.onPull(pullListener);
}
error(error) {
this.#source.error(error);
}
enqueue(chunk) {
this.#source.enqueue(chunk);
}
close() {
this.#source.close();
}
}
//# sourceMappingURL=ControllableStream.js.map