@not-true/devtools
Version:
Remote debugging and development tools client library for React Native applications
82 lines (71 loc) • 1.6 kB
text/typescript
import { QueueItem } from '../types';
export class QueueManager {
private queue: QueueItem[] = [];
private maxSize: number;
constructor(maxSize: number = 1000) {
this.maxSize = maxSize;
}
/**
* Add an item to the queue
*/
enqueue(item: QueueItem): void {
// Remove oldest items if queue is at max capacity
while (this.queue.length >= this.maxSize) {
this.queue.shift();
}
this.queue.push(item);
}
/**
* Remove and return the oldest item from the queue
*/
dequeue(): QueueItem | null {
return this.queue.shift() || null;
}
/**
* Check if the queue is empty
*/
isEmpty(): boolean {
return this.queue.length === 0;
}
/**
* Get the current queue size
*/
size(): number {
return this.queue.length;
}
/**
* Clear all items from the queue
*/
clear(): void {
this.queue = [];
}
/**
* Get all items in the queue without removing them
*/
getAll(): QueueItem[] {
return [...this.queue];
}
/**
* Remove items older than the specified timestamp
*/
removeOldItems(maxAge: number): void {
const cutoffTime = Date.now() - maxAge;
this.queue = this.queue.filter(item => item.timestamp >= cutoffTime);
}
/**
* Get queue statistics
*/
getStats(): {
size: number;
maxSize: number;
oldestItem: QueueItem | null;
newestItem: QueueItem | null;
} {
return {
size: this.queue.length,
maxSize: this.maxSize,
oldestItem: this.queue[0] || null,
newestItem: this.queue[this.queue.length - 1] || null,
};
}
}