UNPKG

@bazilio-san/af-stream

Version:
56 lines 1.72 kB
import { findIndexOfNearestSmaller } from './utils/find-index-of-nearest-smaller'; import { TS_FIELD } from './constants'; export 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[TS_FIELD]) || 0; this.lastTs = ((_b = this.last) === null || _b === void 0 ? void 0 : _b[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 findIndexOfNearestSmaller(rb, virtualTime); } } //# sourceMappingURL=RecordsBuffer.js.map