dbl-utils
Version:
Utilities for dbl, adba and others projects
44 lines (43 loc) • 1.36 kB
TypeScript
/**
* A queue to manage fetch requests with unique identification.
*
* Useful to deduplicate concurrent network calls to the same URL.
*
* @example
* ```ts
* const queue = new FetchQueue(fetch);
* const [a, b] = await Promise.all([
* queue.addRequest('https://example.com/data'),
* queue.addRequest('https://example.com/data')
* ]);
* // only one HTTP request is performed
* ```
*/
export default class FetchQueue {
private queue;
private isRunning;
private fetchFn;
/**
*
* @param {Function} fetchFn - A fetch function (e.g., node-fetch or window.fetch).
*/
constructor(fetchFn: (url: string, options?: RequestInit) => Promise<any>);
/**
* Adds a fetch request to the queue.
* @param {string} url - The URL to fetch.
* @param {RequestInit} [options] - Optional fetch options.
* @returns {Promise<any>} - A promise that resolves with the fetch response.
*/
addRequest(url: string, options?: RequestInit): Promise<any>;
/**
* Runs the queue of fetch requests.
*/
private runQueue;
/**
* Creates a hash to uniquely identify a request.
* @param {string} url - The URL to fetch.
* @param {RequestInit} [options] - Optional fetch options.
* @returns {string} - A hash representing the unique request.
*/
private createHash;
}