Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: reload watch dir when add new folder #156

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
//
// Unique values sent to the channel are stored in an internal map and all
// are processed once the the interval is up.
func debounce(interval time.Duration, input chan string, firstCallback func(arg string), callback func(arg string)) {
func debounce(interval time.Duration, input chan string, exit chan struct{}, firstCallback func(arg string), callback func(arg string)) {
// keep a log of unique paths
var items = make(map[string]bool)
var item string
Expand All @@ -37,6 +37,9 @@ func debounce(interval time.Duration, input chan string, firstCallback func(arg
callback(path)
delete(items, path)
}
case <-exit:
log.Info("debounce return")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This log statement can be removed

return
}
}
}
Expand Down Expand Up @@ -76,9 +79,9 @@ func (w *FSWatcher) Close() {
// WatchDir sets up the filesystem watcher for baseDir and all existing subdirectories
func (w *FSWatcher) WatchDir(baseDir string) error {
c := make(chan string)

exit := make(chan struct{})
// debounced call to create / update tileset
go debounce(500*time.Millisecond, c, func(path string) {
go debounce(500*time.Millisecond, c, exit, func(path string) {
// callback for first time path is debounced
id, err := w.generateID(path, baseDir)
if err != nil {
Expand Down Expand Up @@ -128,7 +131,6 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
}
return
})

go func() {
for {
select {
Expand All @@ -146,9 +148,14 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
}

path := event.Name

if ext := filepath.Ext(path); ext != ".mbtiles" {
xjellyx marked this conversation as resolved.
Show resolved Hide resolved
continue
exit <- struct{}{}
err := w.WatchDir(baseDir)
if err != nil {
return
}
log.Info("reload watch dir")
return
}

if _, err := os.Stat(path + "-journal"); err == nil {
Expand Down