diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2024-02-24 16:17:40 +0100 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2024-02-24 16:17:40 +0100 |
| commit | e449cbeb6c9c14b4a9db26906826e3c4f85da74e (patch) | |
| tree | ada36e2c5482b99f83c515f0eef8da29318ef4b5 /watch.go | |
| parent | 0f08b1511cc8d5b1246799a6ea5ce9788c6e32a6 (diff) | |
Improve file watcher
Diffstat (limited to 'watch.go')
| -rw-r--r-- | watch.go | 22 |
1 files changed, 11 insertions, 11 deletions
@@ -8,6 +8,7 @@ import ( "errors" "fmt" "os" + "time" "github.com/fsnotify/fsnotify" ) @@ -33,26 +34,25 @@ func Watch(path string, fn func(string) error) error { return fn(string(data)) } - if err := update(); err != nil { - if errors.Is(err, StopWatch) { - return nil - } - return err + if err := update(); errors.Is(err, StopWatch) { + return nil } for { select { - case _, ok := <-watcher.Events: + case e, ok := <-watcher.Events: if !ok { return nil } - watcher.Remove(path) - watcher.Add(path) - if err := update(); err != nil { - if errors.Is(err, StopWatch) { + if e.Has(fsnotify.Rename) { + time.Sleep(10 * time.Millisecond) + watcher.Remove(path) + watcher.Add(path) + } + if e.Has(fsnotify.Write) || e.Has(fsnotify.Rename) { + if err := update(); errors.Is(err, StopWatch) { return nil } - return nil } case err, ok := <-watcher.Errors: if !ok { |