ix
Version:
The Interactive Extensions for JavaScript
52 lines (50 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.takeUntil = exports.TakeUntilAsyncIterable = 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 safeRace_js_1 = require("../../util/safeRace.js");
const DONE_PROMISE_VALUE = undefined;
/** @ignore */
class TakeUntilAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(source, other) {
super();
this._source = source;
this._other = other;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
(0, aborterror_js_1.throwIfAborted)(signal);
const donePromise = this._other(signal).then(() => DONE_PROMISE_VALUE);
const itemsAsyncIterator = (0, withabort_js_1.wrapWithAbort)(this._source, signal)[Symbol.asyncIterator]();
for (;;) {
const itemPromise = itemsAsyncIterator.next();
const result = yield tslib_1.__await((0, safeRace_js_1.safeRace)([donePromise, itemPromise]));
if (result === DONE_PROMISE_VALUE || result.done) {
break;
}
yield yield tslib_1.__await(result.value);
}
});
}
}
exports.TakeUntilAsyncIterable = TakeUntilAsyncIterable;
/**
* Returns the elements from the source async-iterable sequence until the other function
* that returns a promise produces an element.
*
* @template TSource The type of the elements in the source sequence.
* @param {(signal?: AbortSignal) => Promise<any>} other A function that terminates the propagation of
* elements in the source sequence.
* @returns {MonoTypeOperatorAsyncFunction<TSource>} An async-iterable sequence containing the elements of the
* source sequence up to the point the other function which returns a promise interrupted further propagation.
*/
function takeUntil(other) {
return function takeUntilOperatorFunction(source) {
return new TakeUntilAsyncIterable(source, other);
};
}
exports.takeUntil = takeUntil;
//# sourceMappingURL=takeuntil.js.map