UNPKG

ix

Version:

The Interactive Extensions for JavaScript

58 lines (56 loc) 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.takeWhile = exports.TakeWhileAsyncIterable = 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"); /** @ignore */ class TakeWhileAsyncIterable extends asynciterablex_js_1.AsyncIterableX { constructor(source, predicate) { super(); this._source = source; this._predicate = predicate; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { var _b, e_1, _c, _d; (0, aborterror_js_1.throwIfAborted)(signal); let i = 0; try { for (var _e = true, _f = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._source, signal)), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) { _d = _g.value; _e = false; const item = _d; if (!(yield tslib_1.__await(this._predicate(item, i++, signal)))) { break; } yield yield tslib_1.__await(item); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_e && !_b && (_c = _f.return)) yield tslib_1.__await(_c.call(_f)); } finally { if (e_1) throw e_1.error; } } }); } } exports.TakeWhileAsyncIterable = TakeWhileAsyncIterable; /** * Returns elements from an async-iterable sequence as long as a specified condition is true. * * @template T The type of the elements in the source sequence. * @param {((value: T, index: number, signal?: AbortSignal) => boolean | Promise<boolean>)} predicate A function to test each element for a condition. * @returns {OperatorAsyncFunction<T, T>} An async-iterable sequence that contains the elements from the input sequence that occur * before the element at which the test no longer passes. */ function takeWhile(predicate) { return function takeWhileOperatorFunction(source) { return new TakeWhileAsyncIterable(source, predicate); }; } exports.takeWhile = takeWhile; //# sourceMappingURL=takewhile.js.map