@@ -9,21 +9,42 @@ export interface UseDebouncedCallbackOptions {
99 maxWait ?: number
1010}
1111
12+ export interface UseDebouncedCallbackOptionsLeading
13+ extends UseDebouncedCallbackOptions {
14+ leading : true
15+ }
16+
1217/**
1318 * Creates a debounced function that will invoke the input function after the
1419 * specified wait.
1520 *
1621 * @param fn a function that will be debounced
1722 * @param waitOrOptions a wait in milliseconds or a debounce configuration
1823 */
19- export default function useDebouncedCallback <
20- TCallback extends ( ...args : any [ ] ) => any ,
21- > (
24+ function useDebouncedCallback < TCallback extends ( ...args : any [ ] ) => any > (
25+ fn : TCallback ,
26+ options : UseDebouncedCallbackOptionsLeading ,
27+ ) : ( ...args : Parameters < TCallback > ) => ReturnType < TCallback >
28+
29+ /**
30+ * Creates a debounced function that will invoke the input function after the
31+ * specified wait.
32+ *
33+ * @param fn a function that will be debounced
34+ * @param waitOrOptions a wait in milliseconds or a debounce configuration
35+ */
36+ function useDebouncedCallback < TCallback extends ( ...args : any [ ] ) => any > (
37+ fn : TCallback ,
38+ waitOrOptions : number | UseDebouncedCallbackOptions ,
39+ ) : ( ...args : Parameters < TCallback > ) => ReturnType < TCallback > | undefined
40+
41+ function useDebouncedCallback < TCallback extends ( ...args : any [ ] ) => any > (
2242 fn : TCallback ,
2343 waitOrOptions : number | UseDebouncedCallbackOptions ,
24- ) : ( ...args : Parameters < TCallback > ) => void {
44+ ) : ( ...args : Parameters < TCallback > ) => ReturnType < TCallback > | undefined {
2545 const lastCallTimeRef = useRef < number | null > ( null )
2646 const lastInvokeTimeRef = useRef ( 0 )
47+ const returnValueRef = useRef < ReturnType < TCallback > > ( )
2748
2849 const isTimerSetRef = useRef ( false )
2950 const lastArgsRef = useRef < unknown [ ] | null > ( null )
@@ -50,10 +71,11 @@ export default function useDebouncedCallback<
5071 isTimerSetRef . current = true
5172 timeout . set ( timerExpired , wait )
5273
53- // Invoke the leading edge.
54- if ( leading ) {
55- invokeFunc ( time )
74+ if ( ! leading ) {
75+ return returnValueRef . current
5676 }
77+
78+ return invokeFunc ( time )
5779 }
5880
5981 function trailingEdge ( time : number ) {
@@ -66,6 +88,7 @@ export default function useDebouncedCallback<
6688 }
6789
6890 lastArgsRef . current = null
91+ return returnValueRef . current
6992 }
7093
7194 function timerExpired ( ) {
@@ -94,7 +117,9 @@ export default function useDebouncedCallback<
94117 lastArgsRef . current = null
95118 lastInvokeTimeRef . current = time
96119
97- return fn ( ...args )
120+ const retValue = fn ( ...args )
121+ returnValueRef . current = retValue
122+ return retValue
98123 }
99124
100125 function shouldInvoke ( time : number ) {
@@ -136,6 +161,10 @@ export default function useDebouncedCallback<
136161 isTimerSetRef . current = true
137162 setTimeout ( timerExpired , wait )
138163 }
164+
165+ return returnValueRef . current
139166 }
140167 } , [ fn , wait , maxWait , leading , trailing ] )
141168}
169+
170+ export default useDebouncedCallback
0 commit comments