-
Notifications
You must be signed in to change notification settings - Fork 13
/
watcher_darwin.go
68 lines (57 loc) · 1.25 KB
/
watcher_darwin.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
68
// +build darwin
package main
import (
"log"
"time"
"github.com/fsnotify/fsevents"
)
// newWatcher returns a new file watcher.
func newWatcher() (*watcher, error) {
return &watcher{
changes: make(chan struct{}, 100),
}, nil
}
// watcher watches files for changes.
// changes are signaled via the "changes" channel.
// errors are logged.
type watcher struct {
watcher *fsevents.EventStream
changes chan struct{}
}
func (w *watcher) init(path string) {
dev, err := fsevents.DeviceForPath("/")
if err != nil {
log.Printf("watcher init error: %v", err)
return
}
es := &fsevents.EventStream{
Latency: 10 * time.Millisecond,
Flags: fsevents.FileEvents | fsevents.NoDefer,
Paths: []string{path},
Device: dev,
}
es.Start()
w.watcher = es
go func() {
for msg := range es.Events {
for _, event := range msg {
if event.Flags&fsevents.ItemModified == fsevents.ItemModified {
w.changes <- struct{}{}
}
}
}
}()
}
// Watch watches a file for changes.
func (w *watcher) Watch(file string) {
if w.watcher == nil {
w.init(file)
} else {
w.watcher.Paths = append(w.watcher.Paths, file)
w.watcher.Restart()
}
}
// StopWatchingAll stops watching all files.
func (w *watcher) StopWatchingAll() {
panic("unimplemented")
}