@drip_sync/drip
Version:
Scalable incremental sync for MongoDB aggregation pipelines
41 lines (40 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlushBuffer = void 0;
const assert_1 = require("assert");
class FlushBuffer {
_items = [];
_timer;
_maxLength;
_flusher;
constructor(maxLength, flusher) {
this._maxLength = maxLength;
this._flusher = flusher;
}
async push(t) {
this._items.push(t);
if (this._items.length >= this._maxLength) {
await this._flush();
}
else {
this._timer ??= setTimeout(() => void this._flush(), 0);
}
}
_flush() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = undefined;
}
(0, assert_1.strict)(this._items.length > 0);
const items = this._items;
this._items = [];
return this._flusher(items);
}
abort() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = undefined;
}
}
}
exports.FlushBuffer = FlushBuffer;