gosuki/watch/reducer.go
Chakib Ben Ziane 280341d4e0 Refactoring + doc updates + pass Context to modules
- modules receive on Init() a context with Cli context
- Eval symlinks on profile dir
- WIP handle multiple profiles
2023-02-18 23:08:12 +01:00

33 lines
689 B
Go

package watch
import "time"
// Run reducer in its own thread when the watcher is started
// It receives a struct{event, func} and runs the func only once in the interval
func ReduceEvents(interval time.Duration,
w WatchRunner) {
log.Debug("Running reducer")
eventsIn := w.Watch().eventsChan
timer := time.NewTimer(interval)
var events []bool
for {
select {
case <-eventsIn:
// log.Debug("[reducuer] received event, resetting watch interval !")
timer.Reset(interval)
events = append(events, true)
case <-timer.C:
if len(events) > 0 {
log.Debug("<reduce>: running run event")
w.Run()
// Empty events queue
events = make([]bool, 0)
}
}
}
}