-
Notifications
You must be signed in to change notification settings - Fork 29
/
gow_watch_notify.go
54 lines (45 loc) · 1.02 KB
/
gow_watch_notify.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
package main
import (
"path/filepath"
"github.com/mitranim/gg"
"github.com/rjeczalik/notify"
)
// Implementation of `Watcher` that uses "github.com/rjeczalik/notify".
type WatchNotify struct {
Mained
Done gg.Chan[struct{}]
Events gg.Chan[notify.EventInfo]
}
func (self *WatchNotify) Init(main *Main) {
self.Mained.Init(main)
self.Done.Init()
self.Events.InitCap(1)
paths := main.Opt.WatchDirs
verb := main.Opt.Verb && !gg.Equal(paths, OptDefault().WatchDirs)
for _, path := range paths {
// In "github.com/rjeczalik/notify", the "..." syntax is used to signify
// recursive watching.
path = filepath.Join(path, `...`)
if verb {
log.Printf(`watching %q`, path)
}
gg.Try(notify.Watch(path, self.Events, notify.All))
}
}
func (self *WatchNotify) Deinit() {
self.Done.SendZeroOpt()
if self.Events != nil {
notify.Stop(self.Events)
}
}
func (self WatchNotify) Run() {
main := self.Main()
for {
select {
case <-self.Done:
return
case event := <-self.Events:
main.OnFsEvent(event)
}
}
}