@@ -9,21 +9,42 @@ export interface UseDebouncedCallbackOptions {
9
9
maxWait ?: number
10
10
}
11
11
12
+ export interface UseDebouncedCallbackOptionsLeading
13
+ extends UseDebouncedCallbackOptions {
14
+ leading : true
15
+ }
16
+
12
17
/**
13
18
* Creates a debounced function that will invoke the input function after the
14
19
* specified wait.
15
20
*
16
21
* @param fn a function that will be debounced
17
22
* @param waitOrOptions a wait in milliseconds or a debounce configuration
18
23
*/
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 > (
22
42
fn : TCallback ,
23
43
waitOrOptions : number | UseDebouncedCallbackOptions ,
24
- ) : ( ...args : Parameters < TCallback > ) => void {
44
+ ) : ( ...args : Parameters < TCallback > ) => ReturnType < TCallback > | undefined {
25
45
const lastCallTimeRef = useRef < number | null > ( null )
26
46
const lastInvokeTimeRef = useRef ( 0 )
47
+ const returnValueRef = useRef < ReturnType < TCallback > > ( )
27
48
28
49
const isTimerSetRef = useRef ( false )
29
50
const lastArgsRef = useRef < unknown [ ] | null > ( null )
@@ -50,10 +71,11 @@ export default function useDebouncedCallback<
50
71
isTimerSetRef . current = true
51
72
timeout . set ( timerExpired , wait )
52
73
53
- // Invoke the leading edge.
54
- if ( leading ) {
55
- invokeFunc ( time )
74
+ if ( ! leading ) {
75
+ return returnValueRef . current
56
76
}
77
+
78
+ return invokeFunc ( time )
57
79
}
58
80
59
81
function trailingEdge ( time : number ) {
@@ -66,6 +88,7 @@ export default function useDebouncedCallback<
66
88
}
67
89
68
90
lastArgsRef . current = null
91
+ return returnValueRef . current
69
92
}
70
93
71
94
function timerExpired ( ) {
@@ -94,7 +117,9 @@ export default function useDebouncedCallback<
94
117
lastArgsRef . current = null
95
118
lastInvokeTimeRef . current = time
96
119
97
- return fn ( ...args )
120
+ const retValue = fn ( ...args )
121
+ returnValueRef . current = retValue
122
+ return retValue
98
123
}
99
124
100
125
function shouldInvoke ( time : number ) {
@@ -136,6 +161,10 @@ export default function useDebouncedCallback<
136
161
isTimerSetRef . current = true
137
162
setTimeout ( timerExpired , wait )
138
163
}
164
+
165
+ return returnValueRef . current
139
166
}
140
167
} , [ fn , wait , maxWait , leading , trailing ] )
141
168
}
169
+
170
+ export default useDebouncedCallback
0 commit comments