-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.ts
73 lines (61 loc) · 2.21 KB
/
index.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {type DependencyList, useMemo, useRef} from 'react';
import {useUnmountEffect} from '../useUnmountEffect/index.js';
export type ThrottledFunction<Fn extends (...args: any[]) => any> = (
this: ThisParameterType<Fn>,
...args: Parameters<Fn>
) => void;
/**
* Makes passed function throttled, otherwise acts like `useCallback`.
*
* @param callback Function that will be throttled.
* @param deps Dependencies list when to update callback.
* @param delay Throttle delay.
* @param noTrailing If `noTrailing` is true, callback will only execute every
* `delay` milliseconds, otherwise, callback will be executed one final time
* after the last throttled-function call.
*/
export function useThrottledCallback<Fn extends (...args: any[]) => any>(
callback: Fn,
deps: DependencyList,
delay: number,
noTrailing = false,
): ThrottledFunction<Fn> {
const timeout = useRef<ReturnType<typeof setTimeout>>();
const lastCall = useRef<{args: Parameters<Fn>; this: ThisParameterType<Fn>}>();
useUnmountEffect(() => {
if (timeout.current) {
clearTimeout(timeout.current);
timeout.current = undefined;
}
});
return useMemo(() => {
const execute = (context: ThisParameterType<Fn>, args: Parameters<Fn>) => {
lastCall.current = undefined;
callback.apply(context, args);
timeout.current = setTimeout(() => {
timeout.current = undefined;
// If trailing execution is not disabled - call callback with last
// received arguments and context
if (!noTrailing && lastCall.current) {
execute(lastCall.current.this, lastCall.current.args);
lastCall.current = undefined;
}
}, delay);
};
const wrapped = function (this, ...args) {
if (timeout.current) {
// If we cant execute callback immediately - save its arguments and
// context to execute it when delay is passed
lastCall.current = {args, this: this};
return;
}
execute(this, args);
} as ThrottledFunction<Fn>;
Object.defineProperties(wrapped, {
length: {value: callback.length},
name: {value: `${callback.name || 'anonymous'}__throttled__${delay}`},
});
return wrapped;
// eslint-disable-next-line react-hooks/exhaustive-deps,@typescript-eslint/no-unsafe-assignment
}, [delay, noTrailing, ...deps]);
}