From f7f114b2d84210e61be47d80e2a65a5b0cce2869 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Fri, 15 Nov 2013 12:46:49 -0800 Subject: [PATCH] Help only fire config reload once per save. --- config/config.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 375b89e..eb2a6f0 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ import ( "io" "os" "path/filepath" + "time" ) const DefaultPostfix = "_config.json" @@ -118,12 +119,26 @@ func NewWatchConfig(filepath string, decode DecodeConfig, defaultConfig interfac } func (wc *WatchConfig) run() { + // Work around watch events being sent more then once. + ticker := time.NewTicker(time.Second) + trigger := false + waitOnce := false for { select { case <-wc.close: return case <-wc.watch.Event: - wc.C <- wc + trigger = true + case <-ticker.C: + // Think of this as a PLC state machine. + if trigger && waitOnce { + wc.C <- wc + trigger = false + waitOnce = false + } + if trigger && !waitOnce { + waitOnce = true + } } } }