Reuses existing timer in tickAndDebounce

pull/1/head
Martin Boehm 2018-02-01 12:26:12 +01:00
parent a07c414e72
commit 1f36acc084
1 changed files with 7 additions and 6 deletions

View File

@ -193,20 +193,21 @@ func tickAndDebounce(tickTime time.Duration, debounceTime time.Duration, input c
timer := time.NewTimer(tickTime)
Loop:
for {
timerChan := timer.C
select {
case _, ok := <-input:
timer.Stop()
// exit loop on closed channel
if !timer.Stop() {
<-timer.C
}
// exit loop on closed input channel
if !ok {
break Loop
}
// debounce for debounceTime
timer = time.NewTimer(debounceTime)
case <-timerChan:
timer.Reset(debounceTime)
case <-timer.C:
// do the action and start the loop again
f()
timer = time.NewTimer(tickTime)
timer.Reset(tickTime)
}
}
}