From e449cbeb6c9c14b4a9db26906826e3c4f85da74e Mon Sep 17 00:00:00 2001 From: Philipp Tanlak Date: Sat, 24 Feb 2024 16:17:40 +0100 Subject: Improve file watcher --- watch.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'watch.go') diff --git a/watch.go b/watch.go index afeead4..60fdabb 100644 --- a/watch.go +++ b/watch.go @@ -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 { -- cgit v1.2.3