|
| 1 | +type PromiseCallback<T = any, E = Error> = { |
| 2 | + resolve: (value: T | PromiseLike<T>) => void; |
| 3 | + reject: (reason: E) => void; |
| 4 | +}; |
| 5 | + |
| 6 | +const callbacks = new Map<string, PromiseCallback[]>(); |
| 7 | + |
| 8 | +function hasKey(key: string): boolean { |
| 9 | + return callbacks.has(key); |
| 10 | +} |
| 11 | + |
| 12 | +function addKey(key: string): void { |
| 13 | + callbacks.set(key, []); |
| 14 | +} |
| 15 | + |
| 16 | +function removeKey(key: string): void { |
| 17 | + callbacks.delete(key); |
| 18 | +} |
| 19 | + |
| 20 | +function addCallbackToKey<T>(key: string, callback: PromiseCallback<T>): void { |
| 21 | + const stash = getCallbacksByKey<T>(key); |
| 22 | + stash.push(callback); |
| 23 | + callbacks.set(key, stash); |
| 24 | +} |
| 25 | + |
| 26 | +function getCallbacksByKey<T>(key: string): Array<PromiseCallback<T>> { |
| 27 | + return callbacks.get(key) ?? []; |
| 28 | +} |
| 29 | + |
| 30 | +async function enqueue<T>(key: string): Promise<T> { |
| 31 | + return new Promise<T>((resolve, reject) => { |
| 32 | + const callback: PromiseCallback<T> = {resolve, reject}; |
| 33 | + addCallbackToKey(key, callback); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function dequeue<T>(key: string): Array<PromiseCallback<T>> { |
| 38 | + const stash = getCallbacksByKey<T>(key); |
| 39 | + removeKey(key); |
| 40 | + return stash; |
| 41 | +} |
| 42 | + |
| 43 | +function coalesce<T>(options: {key: string; error?: Error; result?: T}): void { |
| 44 | + const {key, error, result} = options; |
| 45 | + |
| 46 | + for (const callback of dequeue(key)) { |
| 47 | + if (error) { |
| 48 | + /* c8 ignore next 3 */ |
| 49 | + callback.reject(error); |
| 50 | + } else { |
| 51 | + callback.resolve(result); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Enqueue a promise for the group identified by `key`. |
| 58 | + * |
| 59 | + * All requests received for the same key while a request for that key |
| 60 | + * is already being executed will wait. Once the running request settles |
| 61 | + * then all the waiting requests in the group will settle, too. |
| 62 | + * This minimizes how many times the function itself runs at the same time. |
| 63 | + * This function resolves or rejects according to the given function argument. |
| 64 | + * |
| 65 | + * @url https://github.com/douglascayers/promise-coalesce |
| 66 | + */ |
| 67 | +export async function coalesceAsync<T>( |
| 68 | + /** |
| 69 | + * Any identifier to group requests together. |
| 70 | + */ |
| 71 | + key: string, |
| 72 | + /** |
| 73 | + * The function to run. |
| 74 | + */ |
| 75 | + fnc: () => T | PromiseLike<T>, |
| 76 | +): Promise<T> { |
| 77 | + if (!hasKey(key)) { |
| 78 | + addKey(key); |
| 79 | + try { |
| 80 | + const result = await Promise.resolve(fnc()); |
| 81 | + coalesce({key, result}); |
| 82 | + return result; |
| 83 | + } catch (error: any) { |
| 84 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 85 | + coalesce({key, error}); |
| 86 | + // eslint-disable-next-line @typescript-eslint/only-throw-error |
| 87 | + throw error; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return enqueue(key); |
| 92 | +} |
0 commit comments