-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebounce.go
56 lines (46 loc) · 1.1 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
package main
import (
"strings"
"time"
gorph "github.com/sean9999/go-fsnotify-recursively"
"github.com/sean9999/rebouncer"
)
type gev = gorph.GorphEvent
func debounce(messyEvents chan gev) rebouncer.Rebouncer[gev] {
// simply pass in all events
var ingestFn rebouncer.Ingester[gev] = func(fsEvents chan<- gev) {
for ev := range messyEvents {
fsEvents <- ev
}
}
// accumulate all events except ones involving .DS_Store and .git/**
var reduceFn rebouncer.Reducer[gev] = func(evs []gev) []gev {
out := make([]gev, 0)
for _, thisEv := range evs {
if strings.HasPrefix(thisEv.Path, ".git") {
continue
}
if thisEv.Path == ".DS_Store" {
continue
}
isUnique := true
for _, thatEv := range out {
if thatEv.Path == thisEv.Path {
isUnique = false
break
}
}
if isUnique {
out = append(out, thisEv)
}
}
return out
}
// flush every second
var quantizeFn rebouncer.Quantizer[gev] = func(queue []gev) bool {
time.Sleep(1 * time.Second)
return len(queue) > 0
}
rebby := rebouncer.NewRebouncer(ingestFn, reduceFn, quantizeFn, 1024)
return rebby
}