UNPKG

@wavequery/conductor

Version:
39 lines 1.1 kB
import { EventEmitter } from "events"; export class EventStream extends EventEmitter { constructor(options = {}) { super(); this.buffer = []; this.throttleTimeout = null; this.bufferSize = options.bufferSize || 1000; this.throttleInterval = options.throttleInterval || 100; } push(event) { this.buffer.push({ ...event, timestamp: new Date(), }); if (this.buffer.length > this.bufferSize) { this.buffer.shift(); } this.scheduleEmit(); } scheduleEmit() { if (this.throttleTimeout) return; this.throttleTimeout = setTimeout(() => { this.emit("events", this.buffer); this.throttleTimeout = null; }, this.throttleInterval); } getBuffer() { return [...this.buffer]; } clear() { this.buffer = []; if (this.throttleTimeout) { clearTimeout(this.throttleTimeout); this.throttleTimeout = null; } } } //# sourceMappingURL=event-stream.js.map