2018-11-30 03:15:08 +00:00
|
|
|
package utils
|
2018-11-09 17:25:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"gomark/logging"
|
|
|
|
"gomark/watch"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.GetLogger("WATCH")
|
|
|
|
|
2018-11-13 18:57:07 +00:00
|
|
|
// Run reducer in its own thread when the watcher is started
|
2018-11-09 17:25:50 +00:00
|
|
|
// It receives a struct{event, func} and runs the func only once in the interval
|
2018-11-30 03:15:08 +00:00
|
|
|
func ReduceEvents(interval time.Duration,
|
|
|
|
input chan fsnotify.Event,
|
2018-12-01 17:16:46 +00:00
|
|
|
w watch.Watchable) {
|
2018-11-09 17:25:50 +00:00
|
|
|
log.Debug("Running reducer")
|
|
|
|
|
|
|
|
timer := time.NewTimer(interval)
|
|
|
|
var events []bool
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-input:
|
|
|
|
timer.Reset(interval)
|
|
|
|
events = append(events, true)
|
|
|
|
|
|
|
|
case <-timer.C:
|
|
|
|
if len(events) > 0 {
|
|
|
|
w.Run()
|
|
|
|
|
|
|
|
// Empty events queue
|
|
|
|
events = make([]bool, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|