UNPKG

ix

Version:

The Interactive Extensions for JavaScript

55 lines (53 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.slice = exports.SliceAsyncIterable = 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 SliceAsyncIterable extends asynciterablex_js_1.AsyncIterableX { constructor(source, begin, end) { super(); this._source = source; this._begin = begin; this._end = end; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { (0, aborterror_js_1.throwIfAborted)(signal); const source = (0, withabort_js_1.wrapWithAbort)(this._source, signal); const it = source[Symbol.asyncIterator](); let begin = this._begin; let next; while (begin > 0 && !(next = yield tslib_1.__await(it.next())).done) { begin--; } let end = this._end; if (end > 0) { while (!(next = yield tslib_1.__await(it.next())).done) { yield yield tslib_1.__await(next.value); if (--end === 0) { break; } } } }); } } exports.SliceAsyncIterable = SliceAsyncIterable; /** * Returns the elements from the source async-iterable sequence only after the function that returns a promise produces an element. * * @template TSource The type of elements in the source sequence. * @param {number} begin Zero-based index at which to begin extraction. * @param {number} [end=Infinity] Zero-based index before which to end extraction. * @returns {MonoTypeOperatorAsyncFunction<TSource>} An async-iterable containing the extracted elements. */ function slice(begin, end = Infinity) { return function sliceOperatorFunction(source) { return new SliceAsyncIterable(source, begin, end); }; } exports.slice = slice; //# sourceMappingURL=slice.js.map