async-wrappers
Version:
A set of wrapper functions to perform debouncing, throttling, retrying etc.
147 lines (127 loc) • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.callArguments = exports.extendArguments = exports.combineArguments = exports.latestArguments = void 0;
/**
* A reducer used to create the invocation arguments for a function over
* multiple calls.
*
* It receives the current invocation arguments and the call arguments and
* returns the new invocation arguments.
*
* @category Argument Reducer
*/
/**
* @internal
* extracts arguments from function or uses a supplied tuple
*/
/**
* An implementation [[ArgumentsReducer]] for a particular invocation
* function.
*
* @typeparam InvokeFunc the type of the invocation function.
* @typeparam CallFuncOrArgs the arguments or type of the call function.
*
* @category Argument Reducer
*/
/**
* @internal
*/
/**
* An arguments reducer that will use the latest call arguments as
* the invocation arguments.
*
* Example:
* ```
* // the sequence of calls below to a call function
* call(1);
* call(1,2,3);
* call(4,5);
* call([1]);
* call('last');
* // would be the equivalent to the invoke call below.
* invoke('last');
* ```
*
* @category Argument Reducer
*/
const latestArguments = (invokeArgs, callArgs) => callArgs;
/**
* An arguments reducer that takes combines all arguments from every call
* function into an array that is used as the first argument to the invoke
* function.
*
* Example:
* ```
* // the sequence of calls below to a call function
* call(1);
* call(1,2,3);
* call(4,5);
* call([1]);
* call('last');
* // would be the equivalent to the invoke call below.
* invoke([1, 1, 2, 3, 4, 5, [1], 'last']);
* ```
*
* @category Argument Reducer
*/
exports.latestArguments = latestArguments;
const combineArguments = (invokeArgs, callArgs) => {
let [combined] = invokeArgs;
if (combined === undefined) {
invokeArgs[0] = combined = [];
}
combined.push(...callArgs);
return invokeArgs;
};
/**
* Reducer that extends the invocation arguments array with all arguments
* given to it.
*
* Example:
* ```
* // the sequence of calls below to a call function
* call(1);
* call(1,2,3);
* call(4,5);
* call([1]);
* call('last');
* // would be the equivalent to the invoke call below.
* invoke(1, 1, 2, 3, 4, 5, [1], 'last');
* ```
*
* @category Argument Reducer
*/
exports.combineArguments = combineArguments;
const extendArguments = (invokeArgs, callArgs) => {
invokeArgs.push(...callArgs);
return invokeArgs;
};
/**
* Reducer that provices the invocation function with a single array of
* arrays of all calls.
*
* ```
* // the sequence of calls below to a call function
* call(1);
* call(1,2,3);
* call(4,5);
* call([1]);
* call('last');
* // would be the equivalent to the invoke call below.
* invoke([[1], [1, 2, 3], [4, 5], [[1]], ['last']]);
* ```
*
* @category Argument Reducer
*/
exports.extendArguments = extendArguments;
const callArguments = (invokeArgs, callArgs) => {
let [calls] = invokeArgs;
if (calls === undefined) {
invokeArgs[0] = calls = [];
}
calls.push(callArgs);
return [calls];
};
exports.callArguments = callArguments;