Expand description
interface LatestConstructor<TFn extends AnyFn> {
areArgsEqual?: (a: Parameters<TFn>, b: Parameters<TFn>) => boolean;
fn: TFn;
updateArgs?: LatestUpdateArgsFn<TFn>;
}Properties§
are Args Equal?: { ... }Optional function to compare if arguments of 2 calls are equal.
By default, separate calls are considered different, and the result of the latest call will be returned. However, if the function is pure, and the argument of a new call is the same as the call being executed, then the result of the call being executed will be returned. In other words, the new call will not result in another execution of the function.
fn: TFnFunction to be wrapped
update Args?: LatestUpdateArgsFn<TFn>Optional function to update the arguments.
By default, when new calls are made while the previous call is being executed,
The function will be executed again with the latest arguments. This function
is used to change this behavior and is called when new calls are made. In other words,
the default value for this function is (_current, _middle, latest) => latest.
The arguments are:
current: The arguments of the call currently being executedlatest: The argument of this new callmiddle: If more than one call is made while the previous call is being executed, this array contains arguments of the calls betweencurrentandlatestnext: This is the returned value of the previous call to updateArgs, i.e. the args to be executed next.
Args for constructing
latestlatestis an async event wrapper that always resolve to the result of the latest callExample
In the example below, both call will return the result of the second call (2)
Advanced Usage
See the constructor options for more advanced usage, for example, control how arguments are updated when new calls are made.