From 2e5accc9d82b8540c4438d04b1fc228728de427c Mon Sep 17 00:00:00 2001 From: Abdullah Saleem Date: Mon, 21 Jan 2019 17:20:44 +0500 Subject: [PATCH] debounce functionality added --- cmd/watcher/main.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/watcher/main.go b/cmd/watcher/main.go index 60e2827..9275745 100644 --- a/cmd/watcher/main.go +++ b/cmd/watcher/main.go @@ -24,6 +24,7 @@ func main() { stdinPipe := flag.Bool("pipe", false, "pipe event's info to command's stdin") keepalive := flag.Bool("keepalive", false, "keep alive when a cmd returns code != 0") ignore := flag.String("ignore", "", "comma separated list of paths to ignore") + debounce := flag.String("debounce", "0ms", "debounce the watch events for specified duration") flag.Parse() @@ -68,6 +69,11 @@ func main() { } } + debounceDuration, err := time.ParseDuration(*debounce) + if err != nil { + log.Fatalln(err) + } + done := make(chan struct{}) go func() { defer close(done) @@ -77,6 +83,9 @@ func main() { case event := <-w.Event: // Print the event's info. fmt.Println(event) + if debounceDuration > 0 { + debounceEvents(debounceDuration, w.Event) + } // Run the command if one was specified. if *cmd != "" { @@ -150,8 +159,8 @@ func main() { }() // Run the command before watcher starts if one was specified. - go func() { - if *cmd != "" && *startcmd { + if *cmd != "" && *startcmd { + go func() { c := exec.Command(cmdName, cmdArgs...) c.Stdin = os.Stdin c.Stdout = os.Stdout @@ -159,8 +168,8 @@ func main() { if err := c.Run(); err != nil { log.Fatalln(err) } - } - }() + }() + } // Start the watching process. if err := w.Start(parsedInterval); err != nil { @@ -169,3 +178,15 @@ func main() { <-closed } + +func debounceEvents(t time.Duration, c <-chan watcher.Event) { + for timeout := time.After(t); ; { + select { + case event := <-c: + // Print the event's info. + fmt.Println(event) + case <-timeout: + return + } + } +}