-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
watch.go
175 lines (139 loc) · 3.08 KB
/
watch.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package os
import (
"fmt"
"sync"
"time"
"cuelang.org/go/cue"
"github.com/fsnotify/fsnotify"
hofcontext "github.com/hofstadter-io/hof/flow/context"
"github.com/hofstadter-io/hof/flow/flow"
"github.com/hofstadter-io/hof/lib/yagu"
)
type Watch struct{}
func NewWatch(val cue.Value) (hofcontext.Runner, error) {
return &Watch{}, nil
}
func (T *Watch) Run(ctx *hofcontext.Context) (interface{}, error) {
// todo, check failure modes, fill, not return error?
// (in all tasks)
// do failed message handlings fail the client connection and IRC flow?
val := ctx.Value
var globs []string
var handler cue.Value
ferr := func() error {
ctx.CUELock.Lock()
defer func() {
ctx.CUELock.Unlock()
}()
handler = val.LookupPath(cue.ParsePath("handler"))
if !handler.Exists() {
return fmt.Errorf("fs.Watch task missing 'handler' field at %s", val.Path())
}
if handler.Err() != nil {
return handler.Err()
}
globListVal := val.LookupPath(cue.ParsePath("globs"))
if !globListVal.Exists() {
return fmt.Errorf("fs.Watch task missing 'globs' field at %s", val.Path())
}
if globListVal.Err() != nil {
return globListVal.Err()
}
iter, err := globListVal.List()
if err != nil {
return err
}
for iter.Next() {
gv := iter.Value()
if gv.Err() != nil {
return gv.Err()
}
gs, err := gv.String()
if err != nil {
return err
}
globs = append(globs, gs)
}
return nil
}()
if ferr != nil {
return nil, ferr
}
files, err := yagu.FilesFromGlobs(globs)
if err != nil {
return nil, ferr
}
fmt.Printf("watching %d files\n", len(files))
// todo (good-first-issue), configurable
debounce := New(time.Millisecond * 50)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// todo, fill event into handler
// probably want to customize content
// v = v.FillPath(cue.ParsePath("event"), event)
if event.Op&fsnotify.Write == fsnotify.Write {
debounce(func() {
// todo
// TODO, compile and run pipeline
v := val.Context().CompileString("{...}")
v = v.Unify(handler)
// fmt.Println(v)
p, err := flow.OldFlow(ctx, v)
if err != nil {
fmt.Println("Error(flow/new):", err)
return
}
err = p.Start()
if err != nil {
fmt.Println("Error(flow/run):", err)
return
}
})
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Println("error:", err)
}
}
}()
for _, file := range files {
err = watcher.Add(file)
if err != nil {
return nil, err
}
}
<-done
return nil, nil
}
func New(after time.Duration) func(f func()) {
d := &debouncer{after: after}
return func(f func()) {
d.add(f)
}
}
type debouncer struct {
mu sync.Mutex
after time.Duration
timer *time.Timer
}
func (d *debouncer) add(f func()) {
d.mu.Lock()
defer d.mu.Unlock()
if d.timer != nil {
d.timer.Stop()
}
d.timer = time.AfterFunc(d.after, f)
}