summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 {