From d2a73eec94fa79486a34e58d456451a736576edc Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 1 Aug 2016 16:16:09 +0200 Subject: [PATCH] Fixed multiple events on change --- realize/watcher.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/realize/watcher.go b/realize/watcher.go index 446f91d..291d156 100644 --- a/realize/watcher.go +++ b/realize/watcher.go @@ -8,6 +8,7 @@ import ( "log" "strings" "sync" + "time" ) func InArray(str string, list []string) bool{ @@ -52,29 +53,39 @@ func (h *Config) Watch() error{ } watch := func(val Project){ + watcher, _ = fsnotify.NewWatcher() + + // run, bin, build + val.reload = time.Now().Truncate(time.Second) + for _, dir := range val.Watcher.Paths { path, _ := os.Getwd() current = val.Watcher + // add dir of project if err := filepath.Walk(path + dir, walk); err != nil { fmt.Println(err) } } - for { select { case event := <-watcher.Events: - if event.Op&fsnotify.Chmod == fsnotify.Chmod { - continue + if time.Now().Truncate(time.Second).After(val.reload) { + if event.Op & fsnotify.Chmod == fsnotify.Chmod { + continue + } + if _, err := os.Stat(event.Name); err == nil { + log.Println("event:", event) + // run, bin, build + val.reload = time.Now().Truncate(time.Second) + } } - log.Println("event:", event) case err := <-watcher.Errors: log.Println("error:", err) } } - - wg.Done() watcher.Close() + wg.Done() } // add to watcher