-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch.go
46 lines (39 loc) · 844 Bytes
/
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
//go:build !embed
// +build !embed
package mermaidlive
import (
"log"
"github.com/cskr/pubsub/v2"
"github.com/fsnotify/fsnotify"
)
func init() {
log.Println("using filesystem resources")
}
func StartWatching(eventPublisher *pubsub.PubSub[string, Event]) *fsnotify.Watcher {
watcher, err := fsnotify.NewWatcher()
crashOnError(err)
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
log.Println("modified: ", event.Name)
Refresh()
eventPublisher.Pub(NewSimpleEvent("ResourcesRefreshed"), Topic, ClusterMessageTopic)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error: ", err)
}
}
}()
err = watcher.Add(uiSrc)
crashOnError(err)
return watcher
}