/** Type definition for queued request */
export interface QueuedRequest {
taskKey: string;
key: INPUT;
resolve: (result: OUTPUT) => void;
reject: (error: any) => void;
}
/** Performance metrics for the request queue */
export interface RequestQueueMetrics {
currentQueueLength: number;
maxQueueLengthReached: number;
currentConcurrentRequests: number;
maxConcurrentRequestsReached: number;
}
/**
* A specialized queue for managing concurrent request limits in the Promise Cacher.
*
* This class handles:
* - Queuing requests when concurrent limits are reached
* - Processing queued requests when slots become available
* - Tracking queue performance metrics
* - Efficient batch processing of multiple requests
*
* @template INPUT - The type of keys used to identify cache entries
* @template OUTPUT - The type of values returned by cached promises
*/
export class RequestQueue {
/** Internal queue storage for pending requests */
private queue: QueuedRequest[] = [];
/** Set to track currently running concurrent requests */
private concurrentRequests = new Set();
/** Performance metrics tracking */
private metrics: RequestQueueMetrics = {
currentQueueLength: 0,
maxQueueLengthReached: 0,
currentConcurrentRequests: 0,
maxConcurrentRequestsReached: 0,
};
/**
* Gets the current number of queued requests.
*/
public get length(): number {
return this.queue.length;
}
/**
* Gets the current queue performance metrics.
*/
public get performanceMetrics(): RequestQueueMetrics {
return { ...this.metrics };
}
/**
* Checks if the queue is empty.
*/
public get isEmpty(): boolean {
return this.queue.length === 0;
}
/**
* Adds a request to the queue and returns a promise that resolves when the request is processed.
*
* @param taskKey - The transformed cache key
* @param key - The original input key
* @returns Promise that resolves to the cached or freshly fetched value
*/
public enqueue(taskKey: string, key: INPUT): Promise