UNPKG

ix

Version:

The Interactive Extensions for JavaScript

73 lines (71 loc) 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bufferCountOrTime = void 0; const tslib_1 = require("tslib"); const index_js_1 = require("../index.js"); const map_js_1 = require("./map.js"); const merge_js_1 = require("../merge.js"); const withabort_js_1 = require("./withabort.js"); const timerEvent = {}; const ended = {}; class BufferCountOrTime extends index_js_1.AsyncIterableX { constructor(source, bufferSize, maxWaitTime) { super(); this.source = source; this.bufferSize = bufferSize; this.maxWaitTime = maxWaitTime; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { var _b, e_1, _c, _d; const buffer = []; const timer = (0, index_js_1.interval)(this.maxWaitTime).pipe((0, map_js_1.map)(() => timerEvent)); const source = (0, index_js_1.concat)(this.source, (0, index_js_1.of)(ended)); const merged = (0, merge_js_1.merge)(source, timer); try { for (var _e = true, _f = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(merged, signal)), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) { _d = _g.value; _e = false; const item = _d; if (item === ended) { break; } if (item !== timerEvent) { buffer.push(item); } if (buffer.length >= this.bufferSize || (buffer.length && item === timerEvent)) { yield yield tslib_1.__await(buffer.slice()); buffer.length = 0; } } } 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; } } if (buffer.length) { yield yield tslib_1.__await(buffer); } }); } } /** * Projects each element of an async-iterable sequence into consecutive buffers * which are emitted when either the threshold count or time is met. * * @template TSource The type of elements in the source sequence. * @param {number} count The size of the buffer. * @param {number} time The threshold number of milliseconds to wait before flushing a non-full buffer * @returns {OperatorAsyncFunction<TSource, TSource[]>} An operator which returns an async-iterable sequence * of buffers */ function bufferCountOrTime(count, time) { return function bufferOperatorFunction(source) { return new BufferCountOrTime(source, count, time); }; } exports.bufferCountOrTime = bufferCountOrTime; //# sourceMappingURL=buffercountortime.js.map