UNPKG

ix

Version:

The Interactive Extensions for JavaScript

64 lines (62 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.combineLatest = exports.CombineLatestAsyncIterable = void 0; const tslib_1 = require("tslib"); const asynciterablex_js_1 = require("./asynciterablex.js"); const identity_js_1 = require("../util/identity.js"); const withabort_js_1 = require("./operators/withabort.js"); const aborterror_js_1 = require("../aborterror.js"); const safeRace_js_1 = require("../util/safeRace.js"); // eslint-disable-next-line @typescript-eslint/no-empty-function const NEVER_PROMISE = new Promise(() => { }); function wrapPromiseWithIndex(promise, index) { return promise.then((value) => ({ value, index })); } /** @ignore */ class CombineLatestAsyncIterable extends asynciterablex_js_1.AsyncIterableX { constructor(sources) { super(); this._sources = sources; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { (0, aborterror_js_1.throwIfAborted)(signal); const length = this._sources.length; const iterators = new Array(length); const nexts = new Array(length); let hasValueAll = false; const values = new Array(length); const hasValues = new Array(length); let active = length; hasValues.fill(false); for (let i = 0; i < length; i++) { const iterator = (0, withabort_js_1.wrapWithAbort)(this._sources[i], signal)[Symbol.asyncIterator](); iterators[i] = iterator; nexts[i] = wrapPromiseWithIndex(iterator.next(), i); } while (active > 0) { const next = (0, safeRace_js_1.safeRace)(nexts); const { value: { value: value$, done: done$ }, index, } = yield tslib_1.__await(next); if (done$) { nexts[index] = NEVER_PROMISE; active--; } else { values[index] = value$; hasValues[index] = true; const iterator$ = iterators[index]; nexts[index] = wrapPromiseWithIndex(iterator$.next(), index); if (hasValueAll || (hasValueAll = hasValues.every(identity_js_1.identity))) { yield yield tslib_1.__await(values); } } } }); } } exports.CombineLatestAsyncIterable = CombineLatestAsyncIterable; function combineLatest(...sources) { return new CombineLatestAsyncIterable(sources); } exports.combineLatest = combineLatest; //# sourceMappingURL=combinelatest.js.map