-
Notifications
You must be signed in to change notification settings - Fork 1
/
debouncer.go
67 lines (59 loc) · 1.3 KB
/
debouncer.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
/*
© 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"sync"
"time"
)
type Debouncer[T any] struct {
d time.Duration
in <-chan T
sender func([]T)
errFn func(err error)
wg sync.WaitGroup
ctx context.Context
}
// Debouncer debounces event stream values.
// sender and errFb functions must be thread-safe.
// Debouncer is shutdown by in channel close or via context.
func NewDebouncer[T any](d time.Duration, in <-chan T,
sender func([]T),
errFn func(err error), ctx context.Context) (db *Debouncer[T]) {
db0 := Debouncer[T]{d: d, in: in, sender: sender, ctx: ctx}
db0.wg.Add(1)
go db0.debouncerThread()
return &db0
}
func (db *Debouncer[T]) Wait() {
db.wg.Wait()
}
func (db *Debouncer[T]) debouncerThread() {
defer db.wg.Done()
Recover(Annotation(), nil, db.errFn)
timer := time.NewTimer(time.Second)
timer.Stop()
defer timer.Stop()
done := db.ctx.Done()
var values []T
for {
select {
case value, ok := <-db.in:
if !ok {
if len(values) > 0 {
db.sender(values)
}
return // in closed return
}
values = append(values, value)
timer.Reset(db.d)
case <-done:
return // ctx shutdown return
case <-timer.C:
db.sender(values)
values = nil
}
}
}