UNPKG

@johngw/stream

Version:

Reactive programming tools using the WHATWG Streams API.

35 lines 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.race = void 0; const Async_1 = require("@johngw/stream-common/Async"); const Stream_1 = require("@johngw/stream-common/Stream"); /** * Returns a `ReadableStream` that mirrors the first source stream to queue an item. * * @group Sources * @example * ``` * race([ * ----20----40----60------ * --1-----2-----3--------- * -----------0-----0----0- * ]) * * --1-----2-----3--------- * ``` */ function race(streams, queuingStrategy) { if (!streams.length) return (0, Stream_1.immediatelyClosingReadableStream)(); const readers = streams.map((stream) => stream.getReader()); return new ReadableStream({ async pull(controller) { return Promise.race(readers.map((reader) => reader.read())).then((result) => result.done ? controller.close() : controller.enqueue(result.value), (error) => controller.error(error)); }, async cancel(reason) { await (0, Async_1.all)(readers, (reader) => reader.cancel(reason)); }, }, queuingStrategy); } exports.race = race; //# sourceMappingURL=race.js.map