summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Tanlak <philipp.tanlak@gmail.com>2024-02-24 16:17:40 +0100
committerPhilipp Tanlak <philipp.tanlak@gmail.com>2024-02-24 16:17:40 +0100
commite449cbeb6c9c14b4a9db26906826e3c4f85da74e (patch)
treeada36e2c5482b99f83c515f0eef8da29318ef4b5
parent0f08b1511cc8d5b1246799a6ea5ce9788c6e32a6 (diff)
Improve file watcher
-rw-r--r--watch.go22
1 files changed, 11 insertions, 11 deletions
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 {