From 9d20985cdea23980addbcd1c7faf7fe9c2cb9654 Mon Sep 17 00:00:00 2001 From: hilyjiang Date: Tue, 25 Nov 2014 00:42:16 +0800 Subject: [PATCH] Wait 1s before autobuild util there is no file change. --- watch.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/watch.go b/watch.go index 1e41718..f18dd3c 100644 --- a/watch.go +++ b/watch.go @@ -28,10 +28,10 @@ import ( ) var ( - cmd *exec.Cmd - state sync.Mutex - eventTime = make(map[string]int64) - buildPeriod time.Time + cmd *exec.Cmd + state sync.Mutex + eventTime = make(map[string]int64) + scheduleTime time.Time ) func NewWatcher(paths []string, files []string, isgenerate bool) { @@ -51,16 +51,10 @@ func NewWatcher(paths []string, files []string, isgenerate bool) { if checkTMPFile(e.Name) { continue } - if !chekcIfWatchExt(e.Name) { + if !checkIfWatchExt(e.Name) { continue } - // Prevent duplicated builds. - if buildPeriod.Add(1 * time.Second).After(time.Now()) { - continue - } - buildPeriod = time.Now() - mt := getFileModTime(e.Name) if t := eventTime[e.Name]; mt == t { ColorLog("[SKIP] # %s #\n", e.String()) @@ -71,7 +65,19 @@ func NewWatcher(paths []string, files []string, isgenerate bool) { if isbuild { ColorLog("[EVEN] %s\n", e) - go Autobuild(files, isgenerate) + go func() { + // Wait 1s before autobuild util there is no file change. + scheduleTime = time.Now().Add(1 * time.Second) + for { + time.Sleep(scheduleTime.Sub(time.Now())) + if time.Now().After(scheduleTime) { + break + } + return + } + + Autobuild(files, isgenerate) + }() } case err := <-watcher.Error: ColorLog("[WARN] %s\n", err.Error()) // No need to exit here @@ -227,8 +233,8 @@ func checkTMPFile(name string) bool { var watchExts = []string{".go"} -// chekcIfWatchExt returns true if the name HasSuffix . -func chekcIfWatchExt(name string) bool { +// checkIfWatchExt returns true if the name HasSuffix . +func checkIfWatchExt(name string) bool { for _, s := range watchExts { if strings.HasSuffix(name, s) { return true