-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdebounce.go
45 lines (43 loc) · 1.06 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
package async
import (
"context"
"time"
)
// Debounce events fired over a channel by a specified duration, ensuring no events
// are handled until a certain interval of time has passed.
func Debounce(ctx context.Context, interval time.Duration, eventsChan <-chan interface{}, handler func(interface{})) {
var timer *time.Timer
defer func() {
if timer != nil {
timer.Stop()
}
}()
for {
select {
// Wait until an event is triggered.
case event := <-eventsChan:
timer = time.NewTimer(interval)
loop:
for {
// If an event is received, wait the specified interval before calling the handler.
// If another event is received before the interval has passed, store
// it and reset the timer.
select {
case event = <-eventsChan:
// Reset timer.
timer.Stop()
timer = time.NewTimer(interval)
case <-timer.C:
// Stop the current timer, handle the request, and wait for more events.
timer.Stop()
handler(event)
break loop
case <-ctx.Done():
return
}
}
case <-ctx.Done():
return
}
}
}