-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebounce.go
294 lines (267 loc) · 8.09 KB
/
debounce.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package function
import (
"sync"
"time"
)
type options struct {
leading bool
trailing bool
maxWait time.Duration
}
type Option func(*options)
// WithLeading returns an Option that sets whether the function is invoked on
// the leading edge. The default is false.
func WithLeading(leading bool) Option {
return func(o *options) {
o.leading = leading
}
}
// WithTrailing returns an Option that sets whether the function is invoked on
// the trailing edge. The default is true.
func WithTrailing(trailing bool) Option {
return func(o *options) {
o.trailing = trailing
}
}
// WithMaxWait returns an Option that sets the maximum time the function is
// allowed to be delayed before it is invoked. A nonpositive value is treated as
// no max wait. The default is no max wait.
func WithMaxWait(maxWait time.Duration) Option {
return func(o *options) {
o.maxWait = maxWait
}
}
// A Control struct contains control methods for a debounced function.
type Control struct {
Cancel func()
Flush func()
Pending func() bool
}
type ControlWithReturnValue[T any] struct {
Cancel func()
Flush func() T
Pending func() bool
}
// Debounce is a special case of [DebounceWithCustomSignature] where the
// function has no arguments and returns no value.
func Debounce(fn func(), wait time.Duration, opts ...Option) (debounced func()) {
d, _ := DebounceWithCustomSignature(func(...interface{}) interface{} {
fn()
return nil
}, wait, opts...)
debounced = func() { d() }
return
}
// DebounceControl is a special case of [DebounceWithCustomSignature] where the
// function has no arguments and returns no value.
func DebounceControl(fn func(), wait time.Duration, opts ...Option) (debounced func(), control Control) {
d, c := DebounceWithCustomSignature(func(...interface{}) interface{} {
fn()
return nil
}, wait, opts...)
debounced = func() { d() }
control = Control{
Cancel: c.Cancel,
Flush: func() { c.Flush() },
Pending: c.Pending,
}
return
}
// DebounceWithCustomSignature creates a debounced function that delays invoking
// fn until after wait has elapsed since the last time the debounced function
// was invoked. fn is invoked with the last arguments provided to the debounced
// function. Subsequent calls to the debounced function return the result of the
// last fn invocation.
//
// A control struct is also returned which comes with the following methods:
// - Cancel() cancels any pending invocation;
// - Flush() immediately invokes any pending invocation;
// - Pending() returns whether there is a pending invocation.
//
// The wait timeout should be positive.
//
// Options can be provided to customize behavior:
// - [WithLeading]: whether the function is invoked on the leading edge of the
// wait timeout.
// - [WithTrailing]: whether the function is invoked on the trailing edge of
// the wait timeout.
// - [WithMaxWait]: the maximum time fn is allowed to be delayed before it's
// invoked.
//
// If the leading and trailing options are both true, fn is invoked on the
// trailing edge of the timeout only if the debounced function is invoked more
// than once during the wait timeout.
//
// The debounced function as well as control functions are safe for concurrent
// use by multiple goroutines. In particular, invocations of fn are serialized;
// using nonlocal variables in fn without synchronization won't lead to data
// races (provided they aren't accessed outside of fn at the same time).
func DebounceWithCustomSignature[T1, T2 any](
fn func(args ...T1) T2,
wait time.Duration,
opts ...Option,
) (debounced func(args ...T1) T2, control ControlWithReturnValue[T2]) {
o := &options{
leading: false,
trailing: true,
maxWait: 0,
}
for _, opt := range opts {
opt(o)
}
leading := o.leading
trailing := o.trailing
hasMaxWait := o.maxWait > 0
maxWait := o.maxWait
if wait > maxWait {
maxWait = wait
}
// Locking is necessary in this Go port; the JS implementation is
// thread-safe only because JS is single-threaded.
var lock sync.RWMutex
var lastCallTime time.Time
var lastInvokeTime time.Time
var lastArgs []T1
var lastArgsActive bool
var timer *time.Timer
var result T2
// A function named ...Locked is a function that must be called with the
// lock held.
var invokeFuncLocked func(time.Time) T2
var leadingEdgeLocked func(time.Time) T2
var remainingWaitLocked func(time.Time) time.Duration
var shouldInvokeLocked func(time.Time) bool
var timerExpired func()
var trailingEdgeLocked func(time.Time) T2
var cancel func()
var flush func() T2
var pending func() bool
invokeFuncLocked = func(t time.Time) T2 {
lastInvokeTime = t
result = fn(lastArgs...)
lastArgs = nil
lastArgsActive = false
return result
}
leadingEdgeLocked = func(t time.Time) T2 {
// Reset any `maxWait` timer.
lastInvokeTime = t
// Start the timer for the trailing edge.
timer = time.AfterFunc(wait, timerExpired)
// Invoke the leading edge.
if leading {
return invokeFuncLocked(t)
}
return result
}
remainingWaitLocked = func(t time.Time) time.Duration {
timeSinceLastCall := t.Sub(lastCallTime)
timeSinceLastInvoke := t.Sub(lastInvokeTime)
timeWaiting := wait - timeSinceLastCall
if hasMaxWait && timeWaiting > maxWait-timeSinceLastInvoke {
return maxWait - timeSinceLastInvoke
}
return timeWaiting
}
shouldInvokeLocked = func(t time.Time) bool {
timeSinceLastCall := t.Sub(lastCallTime)
timeSinceLastInvoke := t.Sub(lastInvokeTime)
// Either this is the first call, activity has stopped and we're at the
// trailing edge, the system time has gone backwards and we're treating
// it as the trailing edge, or we've hit the `maxWait` limit.
//
// Note that we preserved the timeSinceLastCall < 0 condition from
// lodash, even though elapsed time should always be positive in Go as
// time measurements are done with the monotonic clock.
return lastCallTime.IsZero() || timeSinceLastCall >= wait || timeSinceLastCall < 0 || (hasMaxWait && timeSinceLastInvoke >= maxWait)
}
timerExpired = func() {
lock.Lock()
defer lock.Unlock()
t := time.Now()
if shouldInvokeLocked(t) {
trailingEdgeLocked(t)
return
}
// Restart the timer.
timer = time.AfterFunc(remainingWaitLocked(t), timerExpired)
}
trailingEdgeLocked = func(t time.Time) T2 {
timer = nil
// Only invoke if `fn` has been debounced at least once.
if trailing && lastArgsActive {
return invokeFuncLocked(t)
}
lastArgs = nil
lastArgsActive = false
return result
}
cancel = func() {
lock.Lock()
defer lock.Unlock()
if timer != nil {
timer.Stop()
}
lastCallTime = time.Time{}
lastInvokeTime = time.Time{}
lastArgs = nil
lastArgsActive = false
timer = nil
}
flush = func() T2 {
lock.Lock()
defer lock.Unlock()
if timer == nil {
return result
}
return trailingEdgeLocked(time.Now())
}
pending = func() bool {
lock.RLock()
defer lock.RUnlock()
return timer != nil
}
debounced = func(args ...T1) T2 {
lock.Lock()
defer lock.Unlock()
t := time.Now()
isInvoking := shouldInvokeLocked(t)
lastCallTime = t
lastArgs = args
lastArgsActive = true
if isInvoking {
if timer == nil {
return leadingEdgeLocked(t)
}
if hasMaxWait {
// Handle invocations in a tight loop.
timer = time.AfterFunc(wait, timerExpired)
return invokeFuncLocked(t)
}
}
if timer == nil {
timer = time.AfterFunc(wait, timerExpired)
}
return result
}
control = ControlWithReturnValue[T2]{
Cancel: cancel,
Flush: flush,
Pending: pending,
}
return
}
// Throttle is a special case of [Debounce] with both edges enabled, and maxWait
// set to wait.
func Throttle(fn func(), wait time.Duration) (throttled func(), control Control) {
return DebounceControl(fn, wait, WithLeading(true), WithTrailing(true), WithMaxWait(wait))
}
// ThrottleWithCustomSignature is a special case of
// [DebounceWithCustomSignature] with both edges enabled, and maxWait set to
// wait.
func ThrottleWithCustomSignature[T1, T2 any](
fn func(args ...T1) T2,
wait time.Duration,
) (throttled func(args ...T1) T2, control ControlWithReturnValue[T2]) {
return DebounceWithCustomSignature(fn, wait, WithLeading(true), WithTrailing(true), WithMaxWait(wait))
}