-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.go
110 lines (92 loc) · 2.66 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
// Package debounce provides functions to debounce function calls, i.e., to
// ensure that a function is only executed after a certain amount of time has
// passed since the last call.
//
// Debouncing can be useful in scenarios where function calls may be triggered
// rapidly, such as in response to user input, but the underlying operation is
// expensive and only needs to be performed once per batch of calls.
package debounce
import (
"sync"
"time"
)
// New returns a debounced function that delays invoking f until after wait time
// has elapsed since the last time the debounced function was invoked.
//
// The returned cancel function can be used to cancel any pending invocation of
// f, but is not required to be called, so can be ignored if not needed.
//
// Both debounced and cancel functions are safe for concurrent use in
// goroutines, and can both be called multiple times.
//
// The debounced function does not wait for f to complete, so f needs to be
// thread-safe as it may be invoked again before the previous invocation
// completes.
func New(wait time.Duration, f func()) (debounced func(), cancel func()) {
var mux sync.Mutex
timer := stoppedTimer(f)
debounced = func() {
mux.Lock()
defer mux.Unlock()
timer.Reset(wait)
}
cancel = func() {
mux.Lock()
defer mux.Unlock()
timer.Stop()
}
return debounced, cancel
}
// NewWithMaxWait returns a debounced function like New, but with a maximum wait
// time of maxWait, which is the maximum time f is allowed to be delayed before
// it is invoked.
//
// The returned cancel function can be used to cancel any pending invocation of
// f, but is not required to be called, so can be ignored if not needed.
//
// Both debounced and cancel functions are safe for concurrent use in
// goroutines, and can both be called multiple times.
//
// The debounced function does not wait for f to complete, so f needs to be
// thread-safe as it may be invoked again before the previous invocation
// completes.
func NewWithMaxWait(
wait, maxWait time.Duration,
f func(),
) (debounced func(), cancel func()) {
var mux sync.Mutex
var dirty bool
var timer *time.Timer
var maxTimer *time.Timer
cb := func() {
mux.Lock()
defer mux.Unlock()
if !dirty {
return
}
go f()
timer.Stop()
maxTimer.Stop()
dirty = false
}
timer = stoppedTimer(cb)
maxTimer = stoppedTimer(cb)
debounced = func() {
mux.Lock()
defer mux.Unlock()
timer.Reset(wait)
// Mark as dirty, and start maxTimer if we were not already dirty.
if !dirty {
dirty = true
maxTimer.Reset(maxWait)
}
}
cancel = func() {
mux.Lock()
defer mux.Unlock()
timer.Stop()
maxTimer.Stop()
dirty = false
}
return debounced, cancel
}