/** Object format used when passing multiple requests to RequestQueue at once. */ export type Request = { key: string; requestAction: () => Promise; }; export declare const DEFAULT_REQUEST_CANCEL_REASON = "request cancelled"; /** * Manages a queue of asynchronous requests with unique string keys, which can be added to or cancelled. * If redundant requests with the same key are issued, the request action will only be run once per key * while the original request is still in the queue. */ export default class RequestQueue { /** * The maximum number of requests that can be handled concurrently. * Once reached, additional requests will be queued up to run once a running request completes. */ private maxActiveRequests; /** * The maximum number of requests that can be handled concurrently if only low-priority requests are waiting. Set * lower than `concurrencyLimit` to always leave space for high-priority requests. Cannot be set higher than * `concurrencyLimit`. */ private maxLowPriorityRequests; /** A queue of requests that are ready to be executed, in order of request time. */ private queue; /** A queue of low-priority tasks that are ready to be executed. `queue` must be empty before any of these tasks run. */ private queueLowPriority; /** Stores all requests, even those that are currently active. */ private allRequests; /** Stores requests whose actions are currently being run. */ private activeRequests; /** * Creates a new RequestQueue. * @param maxActiveRequests The maximum number of requests that will be handled concurrently. This is 10 by default. * @param maxLowPriorityRequests The maximum number of low-priority requests that will be handled concurrently. Equal * to `maxActiveRequests` by default, but may be set lower to always leave space for new high-priority requests. */ constructor(maxActiveRequests?: number, maxLowPriorityRequests?: number); /** * Stores request metadata to the internal map of all pending requests. * @param key string identifier of the request. * @param requestAction callable function action of the request. * @returns a reference to the new, registered RequestItem. */ private registerRequest; /** * Moves a registered request into the processing queue, clearing any timeouts on the request. * @param key string identifier of the request. * @param lowPriority Whether this request should be added with low priority. False by default. */ private addRequestToQueue; /** * Adds a request with a unique key to the queue, if it doesn't already exist. * @param key The key used to track the request. * @param requestAction Function that will be called to complete the request. The function * will be run only once per unique key while the request exists, and may be deferred by the * queue at any time. * @param lowPriority Whether this request should be added with low priority. False by default. * @param delayMs Minimum delay, in milliseconds, before this request should be executed. * * NOTE: Cancelling a request while the action is running WILL NOT stop the action. If this behavior is desired, * actions must be responsible for checking the RequestQueue, determining if the request is still valid (e.g. * using `.hasRequest()`), and stopping or returning early. * * @returns A promise that will resolve on completion of the request, or reject if the request is cancelled. * If multiple requests are issued with the same key, a promise for the first request will be returned * until the request is resolved or cancelled. * Note that the return type of the promise will match that of the first request's instance. */ addRequest(key: string, requestAction: () => Promise, lowPriority?: boolean, delayMs?: number): Promise; /** * Adds multiple requests to the queue, with an optional delay between each. * @param requests An array of RequestItems, which include a key and a request action. * @param lowPriority Whether these requests should be added with low priority. False by default. * @param delayMs An optional minimum delay in milliseconds to be added between each request. * For example, a delay of 10 ms will cause the second request to be added to the processing queue * after 10 ms, the third to added after 20 ms, and so on. Set to 10 ms by default. * @returns An array of promises corresponding to the provided requests. (i.e., the `i`th value * of the returned array will be a Promise for the resolution of `requests[i]`). If a request * with a matching key is already pending, returns the promise for the initial request. */ addRequests(requests: Request[], lowPriority?: boolean, delayMs?: number): Promise[]; /** * Attempts to remove and run the next queued request item, if resources are available. * @returns true if a request was started, or false if there are too many * requests already active. */ private dequeue; /** * Removes any request matching the provided key from the queue and rejects its promise. * @param key The key that should be matched against. * @param cancelReason A message or object that will be used as the promise rejection. */ cancelRequest(key: string, cancelReason?: unknown): void; /** * Rejects all request promises and clears the queue. * @param cancelReason A message or object that will be used as the promise rejection. */ cancelAllRequests(cancelReason?: unknown): void; /** * Returns whether a request with the given key exists in the RequestQueue and is not cancelled. * @param key the key to search for. * @returns true if the request is in the RequestQueue. */ hasRequest(key: string): boolean; /** * Returns whether the request with the given key is currently running (not waiting in the queue). * @param key the key to search for. * @returns true if the request is actively running. */ requestRunning(key: string): boolean; }