@@ -17,22 +17,18 @@ type useDebouncedFunctionReturn<Args extends unknown[]> = Readonly<{
17
17
*
18
18
* If the debounce time changes while a callback has been queued to fire, the
19
19
* callback will be canceled completely. You will need to restart the debounce
20
- * process by calling debounced again.
20
+ * process by calling the returned-out function again.
21
21
*/
22
22
export function useDebouncedFunction <
23
23
// Parameterizing on the args instead of the whole callback function type to
24
- // avoid type contra-variance issues; want to avoid need for type assertions
24
+ // avoid type contra-variance issues
25
25
Args extends unknown [ ] = unknown [ ] ,
26
26
> (
27
27
callback : ( ...args : Args ) => void | Promise < void > ,
28
28
debounceTimeMs : number ,
29
29
) : useDebouncedFunctionReturn < Args > {
30
30
const timeoutIdRef = useRef < number | null > ( null ) ;
31
31
const cancelDebounce = useCallback ( ( ) => {
32
- // Clearing timeout because, even though hot-swapping the timeout value
33
- // while keeping any active debounced functions running was possible, it
34
- // seemed like it'd be ripe for bugs. Can redesign the logic if that ends up
35
- // becoming an actual need down the line.
36
32
if ( timeoutIdRef . current !== null ) {
37
33
window . clearTimeout ( timeoutIdRef . current ) ;
38
34
}
@@ -69,6 +65,9 @@ export function useDebouncedFunction<
69
65
return { debounced, cancelDebounce } as const ;
70
66
}
71
67
68
+ /**
69
+ * Takes any value, and returns out a debounced version of it.
70
+ */
72
71
export function useDebouncedValue < T = unknown > (
73
72
value : T ,
74
73
debounceTimeMs : number ,
0 commit comments