All files / src/hof reuseInFlight.js

7.69% Statements 1/13
0% Branches 0/4
0% Functions 0/4
7.69% Lines 1/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361x                                                                      
const DEFAULT_CONFIG = {
	createKey(...args) { return JSON.stringify(args); }
};
 
// for a given Promise-generating function, track each execution by the stringified
// arguments. if the function is called again with the same arguments, then instead
// of generating a new promise, an existing in-flight promise is used instead. This
// prevents unnecessary repetition of async function calls while the same function
// is still in flight.
export default function reuseInFlight(asyncFn, config) {
	config = {
		...DEFAULT_CONFIG,
		...(config || {})
	};
 
	const inflight = {};
 
	return async function debounced(...args) {
		const key = config.createKey(...args);
		if (!inflight.hasOwnProperty(key)) {
			// WE DO NOT AWAIT, we are storing the promise itself
			inflight[key] = asyncFn.apply(this, args)
				.then(results => {
					// self invalidate
					delete inflight[key];
					return results;
				}, err => {
					// still self-invalidate, then rethrow
					delete inflight[key];
					throw err;
				});
		}
 
		return inflight[key];
	};
}