async-wrappers
Version:
A set of wrapper functions to perform debouncing, throttling, retrying etc.
129 lines (118 loc) • 2.81 kB
JavaScript
/**
* 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
*/
export var 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
*/
export var combineArguments = (invokeArgs, callArgs) => {
var [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
*/
export var 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
*/
export var callArguments = (invokeArgs, callArgs) => {
var [calls] = invokeArgs;
if (calls === undefined) {
invokeArgs[0] = calls = [];
}
calls.push(callArgs);
return [calls];
};