squabble-mcp
Version:
Engineer-driven development with critical-thinking PM collaboration - MCP server for Claude
84 lines • 2.15 kB
JavaScript
/**
* Circular buffer for efficient in-memory event storage
* Automatically overwrites oldest events when capacity is reached
*/
export class CircularBuffer {
buffer;
writeIndex = 0;
size = 0;
capacity;
constructor(capacity) {
if (capacity <= 0) {
throw new Error('Buffer capacity must be positive');
}
this.capacity = capacity;
this.buffer = new Array(capacity);
}
/**
* Add an item to the buffer
*/
push(item) {
this.buffer[this.writeIndex] = item;
this.writeIndex = (this.writeIndex + 1) % this.capacity;
if (this.size < this.capacity) {
this.size++;
}
}
/**
* Get all items in chronological order
*/
toArray() {
if (this.size === 0)
return [];
const result = [];
if (this.size < this.capacity) {
// Buffer not full yet, simple case
for (let i = 0; i < this.size; i++) {
result.push(this.buffer[i]);
}
}
else {
// Buffer is full, need to read in circular order
// Start from oldest item (writeIndex is next write position)
for (let i = 0; i < this.capacity; i++) {
const index = (this.writeIndex + i) % this.capacity;
result.push(this.buffer[index]);
}
}
return result;
}
/**
* Iterate over items in chronological order
*/
forEach(callback) {
this.toArray().forEach(callback);
}
/**
* Get the most recent N items
*/
getRecent(count) {
const items = this.toArray();
return items.slice(-count);
}
/**
* Clear the buffer
*/
clear() {
this.buffer = new Array(this.capacity);
this.writeIndex = 0;
this.size = 0;
}
/**
* Get current number of items in buffer
*/
getSize() {
return this.size;
}
/**
* Check if buffer is full
*/
isFull() {
return this.size === this.capacity;
}
}
//# sourceMappingURL=circular-buffer.js.map