@tak-ps/node-tak
Version:
Lightweight JavaScript library for communicating with TAK Server
47 lines • 1.25 kB
JavaScript
// A fixed-size Ring Buffer (Circular Queue) for zero-allocation operations.
export class Queue {
buffer;
capacity;
head;
tail;
_length;
constructor(capacity = 10000) {
this.capacity = capacity;
this.buffer = new Array(capacity);
this.head = 0;
this.tail = 0;
this._length = 0;
}
// Add item to the queue. Returns false if full.
push(item) {
if (this._length >= this.capacity) {
return false;
}
this.buffer[this.tail] = item;
this.tail = (this.tail + 1) % this.capacity;
this._length++;
return true;
}
// Peek at the next item
peek() {
if (this._length === 0)
return undefined;
return this.buffer[this.head];
}
pop() {
if (this._length === 0)
return undefined;
const item = this.buffer[this.head];
this.buffer[this.head] = undefined; // Clear reference for GC
this.head = (this.head + 1) % this.capacity;
this._length--;
return item;
}
get length() {
return this._length;
}
get isFull() {
return this._length === this.capacity;
}
}
//# sourceMappingURL=queue.js.map