UNPKG

@reactivex/ix-es2015-cjs

Version:

The Interactive Extensions for JavaScript

81 lines (79 loc) 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buffer = exports.BufferAsyncIterable = 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 BufferAsyncIterable extends asynciterablex_js_1.AsyncIterableX { constructor(source, count, skip) { super(); this._source = source; this._count = count; this._skip = skip; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { var _b, e_1, _c, _d; (0, aborterror_js_1.throwIfAborted)(signal); const buffers = []; 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 (i % this._skip === 0) { buffers.push([]); } for (const buff of buffers) { buff.push(item); } if (buffers.length > 0 && buffers[0].length === this._count) { yield yield tslib_1.__await(buffers.shift()); } i++; } } 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; } } while (buffers.length > 0) { yield yield tslib_1.__await(buffers.shift()); } }); } } exports.BufferAsyncIterable = BufferAsyncIterable; /** * Projects each element of an async-iterable sequence into consecutive non-overlapping * buffers which are produced based on element count information. * * @template TSource The type of elements in the source sequence. * @param {number} count The length of each buffer. * @param {number} [skip] An optional number of elements to skip between creation of consecutive buffers. * @returns {OperatorAsyncFunction<TSource, TSource[]>} An operator which returns anm async-iterable sequence with * consecutive non-overlapping buffers based upon element count information. */ function buffer(count, skip) { let s = skip; if (s == null) { s = count; } return function bufferOperatorFunction(source) { return new BufferAsyncIterable(source, count, s); }; } exports.buffer = buffer; /** * Projects each element of an async-iterable sequence into consecutive non-overlapping * buffers which are produced based on element count information. * @param count Length of each buffer. * @param skip Number of elements to skip between creation of consecutive buffers. */ //# sourceMappingURL=buffer.js.map