Skip to content

Commit de9e21f

Browse files
author
Jelly
authored
add: reload watch dir when add new folder (#156)
1 parent afad7c1 commit de9e21f

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

watch.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
//
1818
// Unique values sent to the channel are stored in an internal map and all
1919
// are processed once the the interval is up.
20-
func debounce(interval time.Duration, input chan string, firstCallback func(arg string), callback func(arg string)) {
20+
func debounce(interval time.Duration, input chan string, exit chan struct{}, firstCallback func(arg string), callback func(arg string)) {
2121
// keep a log of unique paths
2222
var items = make(map[string]bool)
2323
var item string
@@ -37,6 +37,8 @@ func debounce(interval time.Duration, input chan string, firstCallback func(arg
3737
callback(path)
3838
delete(items, path)
3939
}
40+
case <-exit:
41+
return
4042
}
4143
}
4244
}
@@ -76,9 +78,9 @@ func (w *FSWatcher) Close() {
7678
// WatchDir sets up the filesystem watcher for baseDir and all existing subdirectories
7779
func (w *FSWatcher) WatchDir(baseDir string) error {
7880
c := make(chan string)
79-
81+
exit := make(chan struct{})
8082
// debounced call to create / update tileset
81-
go debounce(500*time.Millisecond, c, func(path string) {
83+
go debounce(500*time.Millisecond, c, exit, func(path string) {
8284
// callback for first time path is debounced
8385
id, err := w.generateID(path, baseDir)
8486
if err != nil {
@@ -128,7 +130,6 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
128130
}
129131
return
130132
})
131-
132133
go func() {
133134
for {
134135
select {
@@ -146,9 +147,25 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
146147
}
147148

148149
path := event.Name
150+
if ext := filepath.Ext(path); ext == "" {
151+
if event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Write == fsnotify.Write ||
152+
event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
153+
154+
// NOTE: we cannot distinguish which incoming event paths
155+
// correspond to directory events or file events, so we
156+
// trigger a reload in all cases
157+
158+
exit <- struct{}{}
159+
err := w.WatchDir(baseDir)
160+
if err != nil {
161+
return
162+
}
163+
log.Info("Reload watch dir on directory change")
164+
return
165+
} else {
166+
continue
167+
}
149168

150-
if ext := filepath.Ext(path); ext != ".mbtiles" {
151-
continue
152169
}
153170

154171
if _, err := os.Stat(path + "-journal"); err == nil {
@@ -180,11 +197,13 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
180197
if err != nil {
181198
log.Errorf("Could not create ID for tileset %q\n%v", path, err)
182199
}
183-
err = w.svcSet.RemoveTileset(id)
184-
if err != nil {
185-
log.Errorf("Could not remove tileset %q with ID %q\n%v", path, id, err)
186-
} else {
187-
log.Infof("Removed tileset %q with ID %q\n", path, id)
200+
if w.svcSet.HasTileset(id) {
201+
err = w.svcSet.RemoveTileset(id)
202+
if err != nil {
203+
log.Errorf("Could not remove tileset %q with ID %q\n%v", path, id, err)
204+
} else {
205+
log.Infof("Removed tileset %q with ID %q\n", path, id)
206+
}
188207
}
189208
}
190209

0 commit comments

Comments
 (0)