-
Notifications
You must be signed in to change notification settings - Fork 4
/
debounce.go
48 lines (41 loc) · 1.25 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
package main
import (
"time"
)
// Debounces events received on a channel, returning the last event received
// after the delay is past.
func debounce[T interface{}](delay time.Duration, events chan T) chan T {
return debounceWithBuffer(delay, events, 0)
}
// Debounces events received on a channel, returning the last event received
// after the delay is past.
func debounceWithBuffer[T interface{}](delay time.Duration, events chan T, outputBufferSize int) chan T {
output := make(chan T, outputBufferSize)
go func() {
for event := range events {
L:
for {
select {
case tempEvent, ok := <-events:
if !ok {
// But if events is closed go ahead and output the last event and break out of the inner loop
// which will then fall out of the outer loop and close the output
output <- event
break L
} else {
// Replaces the `event` local with the new event that was received while waiting
event = tempEvent
}
case <-time.After(delay):
// Forward the final event present after the delay and break out of the inner loop
// which will wait for the next event to arrive
output <- event
break L
}
}
}
// Close the output once the input is closed
close(output)
}()
return output
}