UNPKG

@reactivex/ix-es2015-cjs

Version:

The Interactive Extensions for JavaScript

77 lines (75 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withLatestFrom = exports.WithLatestFromAsyncIterable = void 0; const tslib_1 = require("tslib"); const asynciterablex_js_1 = require("../asynciterablex.js"); const withabort_js_1 = require("./withabort.js"); const aborterror_js_1 = require("../../aborterror.js"); const identity_js_1 = require("../../util/identity.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 WithLatestFromAsyncIterable extends asynciterablex_js_1.AsyncIterableX { constructor(source, others) { super(); this._source = source; this._others = others; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { (0, aborterror_js_1.throwIfAborted)(signal); const length = this._others.length; const newLength = length + 1; const iterators = new Array(newLength); const nexts = new Array(newLength); let hasValueAll = false; const hasValue = new Array(length); const values = new Array(length); hasValue.fill(false); for (let i = 0; i < length; i++) { const iterator = (0, withabort_js_1.wrapWithAbort)(this._others[i], signal)[Symbol.asyncIterator](); iterators[i] = iterator; nexts[i] = wrapPromiseWithIndex(iterator.next(), i); } const it = (0, withabort_js_1.wrapWithAbort)(this._source, signal)[Symbol.asyncIterator](); iterators[length] = it; nexts[length] = wrapPromiseWithIndex(it.next(), length); for (;;) { const next = (0, safeRace_js_1.safeRace)(nexts); const { value: { value: value$, done: done$ }, index, } = yield tslib_1.__await(next); if (index === length) { if (done$) { break; } const iterator$ = iterators[index]; nexts[index] = wrapPromiseWithIndex(iterator$.next(), index); if (hasValueAll) { yield yield tslib_1.__await([value$, ...values]); } } else if (done$) { nexts[index] = NEVER_PROMISE; } else { values[index] = value$; hasValue[index] = true; hasValueAll = hasValue.every(identity_js_1.identity); const iterator$ = iterators[index]; nexts[index] = wrapPromiseWithIndex(iterator$.next(), index); } } }); } } exports.WithLatestFromAsyncIterable = WithLatestFromAsyncIterable; function withLatestFrom(...sources) { return function withLatestFromOperatorFunction(source) { return new WithLatestFromAsyncIterable(source, sources); }; } exports.withLatestFrom = withLatestFrom; //# sourceMappingURL=withlatestfrom.js.map