UNPKG

@johngw/stream

Version:

Reactive programming tools using the WHATWG Streams API.

60 lines 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ControllableStream = void 0; const ControllableSource_1 = require("@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) * ``` */ class ControllableStream extends ReadableStream { #source; constructor(strategy) { const source = new ControllableSource_1.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(); } } exports.ControllableStream = ControllableStream; //# sourceMappingURL=ControllableStream.js.map