UNPKG

@bazilio-san/af-stream

Version:
60 lines 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RecordsBuffer = void 0; const find_index_of_nearest_smaller_1 = require("./utils/find-index-of-nearest-smaller"); const constants_1 = require("./constants"); class RecordsBuffer { constructor() { this.buffer = []; this.first = null; this.last = null; this.firstTs = 0; this.lastTs = 0; this.setEdges(); } setEdges() { var _a, _b; const { buffer: rb } = this; this.first = rb[0] || null; this.last = rb.length ? rb[rb.length - 1] : null; this.firstTs = ((_a = this.first) === null || _a === void 0 ? void 0 : _a[constants_1.TS_FIELD]) || 0; this.lastTs = ((_b = this.last) === null || _b === void 0 ? void 0 : _b[constants_1.TS_FIELD]) || 0; } add(forBuffer) { this.buffer.push(...forBuffer); this.setEdges(); } getMsDistance() { const { firstTs, lastTs } = this; if (!this.buffer.length || lastTs < firstTs) { return 0; } return lastTs - firstTs; } shiftBy(length) { return this.buffer.splice(0, length); } unshiftEvents(eventsPacket) { this.buffer.splice(0, 0, ...eventsPacket); } flush() { this.buffer = []; this.first = null; this.last = null; this.firstTs = 0; this.lastTs = 0; } get length() { return this.buffer.length; } // Greatest index of a value less than the specified findIndexOfNearestSmaller(virtualTime) { const { buffer: rb } = this; if (!rb.length) { return -1; } return (0, find_index_of_nearest_smaller_1.findIndexOfNearestSmaller)(rb, virtualTime); } } exports.RecordsBuffer = RecordsBuffer; //# sourceMappingURL=RecordsBuffer.js.map