@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
103 lines (101 loc) • 4.61 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await, __extends, __generator } from "tslib";
import { AsyncIterableX, interval, concat, of } from '../index.js';
import { map } from './map.js';
import { merge } from '../merge.js';
import { wrapWithAbort } from './withabort.js';
var timerEvent = {};
var ended = {};
var BufferCountOrTime = /** @class */ (function (_super) {
__extends(BufferCountOrTime, _super);
function BufferCountOrTime(source, bufferSize, maxWaitTime) {
var _this = _super.call(this) || this;
_this.source = source;
_this.bufferSize = bufferSize;
_this.maxWaitTime = maxWaitTime;
return _this;
}
BufferCountOrTime.prototype[Symbol.asyncIterator] = function (signal) {
return __asyncGenerator(this, arguments, function _a() {
var buffer, timer, source, merged, _b, _c, _d, item, e_1_1;
var _e, e_1, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
buffer = [];
timer = interval(this.maxWaitTime).pipe(map(function () { return timerEvent; }));
source = concat(this.source, of(ended));
merged = merge(source, timer);
_h.label = 1;
case 1:
_h.trys.push([1, 8, 9, 14]);
_b = true, _c = __asyncValues(wrapWithAbort(merged, signal));
_h.label = 2;
case 2: return [4 /*yield*/, __await(_c.next())];
case 3:
if (!(_d = _h.sent(), _e = _d.done, !_e)) return [3 /*break*/, 7];
_g = _d.value;
_b = false;
item = _g;
if (item === ended) {
return [3 /*break*/, 7];
}
if (item !== timerEvent) {
buffer.push(item);
}
if (!(buffer.length >= this.bufferSize || (buffer.length && item === timerEvent))) return [3 /*break*/, 6];
return [4 /*yield*/, __await(buffer.slice())];
case 4: return [4 /*yield*/, _h.sent()];
case 5:
_h.sent();
buffer.length = 0;
_h.label = 6;
case 6:
_b = true;
return [3 /*break*/, 2];
case 7: return [3 /*break*/, 14];
case 8:
e_1_1 = _h.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 14];
case 9:
_h.trys.push([9, , 12, 13]);
if (!(!_b && !_e && (_f = _c.return))) return [3 /*break*/, 11];
return [4 /*yield*/, __await(_f.call(_c))];
case 10:
_h.sent();
_h.label = 11;
case 11: return [3 /*break*/, 13];
case 12:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 13: return [7 /*endfinally*/];
case 14:
if (!buffer.length) return [3 /*break*/, 17];
return [4 /*yield*/, __await(buffer)];
case 15: return [4 /*yield*/, _h.sent()];
case 16:
_h.sent();
_h.label = 17;
case 17: return [2 /*return*/];
}
});
});
};
return BufferCountOrTime;
}(AsyncIterableX));
/**
* 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
*/
export function bufferCountOrTime(count, time) {
return function bufferOperatorFunction(source) {
return new BufferCountOrTime(source, count, time);
};
}
//# sourceMappingURL=buffercountortime.js.map