From 00d46b36f0da668fb02ac4a0943d214acf334284 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 15 Apr 2022 16:37:37 +0900 Subject: [PATCH] fix: add a timer to support that files are changing at a high speed (#33) --- cmd/watcher.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmd/watcher.go b/cmd/watcher.go index 0a3b2af..0488b12 100644 --- a/cmd/watcher.go +++ b/cmd/watcher.go @@ -2,11 +2,13 @@ package cmd import ( "regexp" + "time" "github.com/fsnotify/fsnotify" ) const ignorePattern = `\.swp$|~$|^\.DS_Store$|^4913$` +const lockTime = 100 * time.Millisecond func createWatcher(dir string) (*fsnotify.Watcher, error) { watcher, err := fsnotify.NewWatcher() @@ -19,16 +21,26 @@ func createWatcher(dir string) (*fsnotify.Watcher, error) { } func watch(done <-chan interface{}, errorChan chan<- error, reload chan<- bool, watcher *fsnotify.Watcher) { + isLocked := false for { select { case event := <-watcher.Events: + if isLocked { + break + } if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create { r := regexp.MustCompile(ignorePattern) if r.MatchString(event.Name) { logDebug("Debug [ignore]: `%s`", event.Name) } else { logInfo("Change detected in %s, refreshing", event.Name) + isLocked = true reload <- true + timer := time.NewTimer(lockTime) + go func() { + <-timer.C + isLocked = false + }() } } case err := <-watcher.Errors: