UNPKG

@bugspotter/sdk

Version:

Professional bug reporting SDK with screenshots, session replay, and automatic error capture for web applications

89 lines (88 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CircularBuffer = void 0; /** * Time-based circular buffer for storing replay events. * Automatically prunes events older than the configured duration. */ class CircularBuffer { constructor(config) { this.events = []; this.duration = config.duration * 1000; // convert to milliseconds } /** * Add an event to the buffer */ add(event) { this.events.push(event); this.prune(); } /** * Add multiple events to the buffer */ addBatch(events) { this.events.push(...events); this.prune(); } /** * Remove events older than the configured duration */ prune() { if (this.events.length === 0) { return; } const now = Date.now(); const cutoffTime = now - this.duration; // Find the first event that should be kept let firstValidIndex = 0; for (let i = 0; i < this.events.length; i++) { if (this.events[i].timestamp >= cutoffTime) { firstValidIndex = i; break; } } // Remove old events if (firstValidIndex > 0) { this.events = this.events.slice(firstValidIndex); } } /** * Get all events in the buffer */ getEvents() { this.prune(); // Ensure we don't return stale events return [...this.events]; // Return a copy } /** * Get compressed event data */ getCompressedEvents() { return this.getEvents(); } /** * Clear all events from the buffer */ clear() { this.events = []; } /** * Get the current size of the buffer */ size() { return this.events.length; } /** * Update the buffer duration */ setDuration(seconds) { this.duration = seconds * 1000; this.prune(); } /** * Get the buffer duration in seconds */ getDuration() { return this.duration / 1000; } } exports.CircularBuffer = CircularBuffer;