|
@@ -123,17 +123,25 @@ export const debounce = <T extends any[]>(
|
|
|
timeout: number,
|
|
|
) => {
|
|
|
let handle = 0;
|
|
|
- let lastArgs: T;
|
|
|
+ let lastArgs: T | null = null;
|
|
|
const ret = (...args: T) => {
|
|
|
lastArgs = args;
|
|
|
clearTimeout(handle);
|
|
|
- handle = window.setTimeout(() => fn(...args), timeout);
|
|
|
+ handle = window.setTimeout(() => {
|
|
|
+ lastArgs = null;
|
|
|
+ fn(...args);
|
|
|
+ }, timeout);
|
|
|
};
|
|
|
ret.flush = () => {
|
|
|
clearTimeout(handle);
|
|
|
- fn(...(lastArgs || []));
|
|
|
+ if (lastArgs) {
|
|
|
+ const _lastArgs = lastArgs;
|
|
|
+ lastArgs = null;
|
|
|
+ fn(..._lastArgs);
|
|
|
+ }
|
|
|
};
|
|
|
ret.cancel = () => {
|
|
|
+ lastArgs = null;
|
|
|
clearTimeout(handle);
|
|
|
};
|
|
|
return ret;
|