@bugspotter/sdk
Version:
Professional bug reporting SDK with screenshots, session replay, and automatic error capture for web applications
78 lines (77 loc) • 2.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CircularBuffer = void 0;
/**
* A generic circular buffer implementation for storing a fixed number of items.
* When the buffer is full, new items overwrite the oldest items.
*
* @template T The type of items stored in the buffer
*/
class CircularBuffer {
constructor(maxSize) {
this.maxSize = maxSize;
this.items = [];
this.index = 0;
this.count = 0;
if (maxSize <= 0) {
throw new Error('CircularBuffer maxSize must be greater than 0');
}
}
/**
* Add an item to the buffer. If the buffer is full, the oldest item is overwritten.
*/
add(item) {
if (this.count < this.maxSize) {
this.items.push(item);
this.count++;
}
else {
this.items[this.index] = item;
}
this.index = (this.index + 1) % this.maxSize;
}
/**
* Get all items in chronological order (oldest to newest).
* Returns a copy of the internal array.
*/
getAll() {
if (this.count < this.maxSize) {
return [...this.items];
}
// Return items in chronological order when buffer is full
return [...this.items.slice(this.index), ...this.items.slice(0, this.index)];
}
/**
* Clear all items from the buffer.
*/
clear() {
this.items = [];
this.index = 0;
this.count = 0;
}
/**
* Get the current number of items in the buffer.
*/
get size() {
return this.count;
}
/**
* Get the maximum capacity of the buffer.
*/
get capacity() {
return this.maxSize;
}
/**
* Check if the buffer is empty.
*/
get isEmpty() {
return this.count === 0;
}
/**
* Check if the buffer is full.
*/
get isFull() {
return this.count >= this.maxSize;
}
}
exports.CircularBuffer = CircularBuffer;