2 * Ensures that a function returning a promise is only called once at a time.
4 * If it is called while the previous promise is resolving, another call to the
5 * function will be queued, and run once, with the arguments of the last call
6 * after the previous promise has resolved. The returned promise resolves after
7 * the queued promise resolves.
10 export const onceWithQueue = <R>(fn: () => Promise<R>): (() => Promise<R>) => {
20 const clearPromise = () => {
21 STATE.promise = undefined;
24 const next = (): Promise<R> => {
26 // eslint-disable-next-line
31 // If a queue has already been set up, update the arguments.
36 // If a promise is running, set up the queue.
38 STATE.queued = STATE.promise.then(next);
44 // Set up independent resolve handlers.
45 STATE.promise.then(clearPromise).catch(clear);