-
Notifications
You must be signed in to change notification settings - Fork 19
/
eventcache.go
61 lines (54 loc) · 1.39 KB
/
eventcache.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
package sync
import (
"os"
"time"
"github.com/rjeczalik/notify"
)
type timedEvent struct {
CreatedAt time.Time
notify.EventInfo
}
type eventCache map[string]timedEvent
func (cache eventCache) Add(ev timedEvent) {
lastEvent, ok := cache[ev.Path()]
if ok {
// If the file was quickly created and then destroyed, pretend nothing ever happened.
if lastEvent.Event() == notify.Create && ev.Event() == notify.Remove {
delete(cache, ev.Path())
return
}
}
// Only let the latest event for a path have action.
cache[ev.Path()] = ev
}
// SequentialEvents returns the list of events that pertain to directories.
// The set of returned events is disjoint with ConcurrentEvents.
func (cache eventCache) SequentialEvents() []timedEvent {
var r []timedEvent
for _, ev := range cache {
info, err := os.Stat(ev.Path())
if err == nil && !info.IsDir() {
continue
}
// Include files that have deleted here.
// It's unclear whether they're files or folders.
r = append(r, ev)
}
return r
}
// ConcurrentEvents returns the list of events that are safe to process after SequentialEvents.
// The set of returns events is disjoint with SequentialEvents.
func (cache eventCache) ConcurrentEvents() []timedEvent {
var r []timedEvent
for _, ev := range cache {
info, err := os.Stat(ev.Path())
if err != nil {
continue
}
if info.IsDir() {
continue
}
r = append(r, ev)
}
return r
}