blob: e909a6c60922f12579d498087e6762f38f96080e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package helper
import "time"
// Ticker ticks on the channel returned by C to signal something.
type Ticker interface {
C() <-chan time.Time
Stop()
Reset()
}
// NewTimerTicker returns a Ticker that ticks after the specified interval
// has passed since the previous Reset call.
func NewTimerTicker(interval time.Duration) Ticker {
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return &timerTicker{timer: timer, interval: interval}
}
type timerTicker struct {
timer *time.Timer
interval time.Duration
}
func (tt *timerTicker) C() <-chan time.Time { return tt.timer.C }
func (tt *timerTicker) Reset() { tt.timer.Reset(tt.interval) }
func (tt *timerTicker) Stop() { tt.timer.Stop() }
// ManualTicker implements a ticker that ticks when Tick is called.
// Stop and Reset functions call the provided functions.
type ManualTicker struct {
c chan time.Time
StopFunc func()
ResetFunc func()
}
func (mt *ManualTicker) C() <-chan time.Time { return mt.c }
func (mt *ManualTicker) Stop() { mt.StopFunc() }
func (mt *ManualTicker) Reset() { mt.ResetFunc() }
func (mt *ManualTicker) Tick() { mt.c <- time.Now() }
// NewManualTicker returns a Ticker that can be manually controlled.
func NewManualTicker() *ManualTicker {
return &ManualTicker{
c: make(chan time.Time, 1),
StopFunc: func() {},
ResetFunc: func() {},
}
}
// NewCountTicker returns a ManualTicker with a ResetFunc that
// calls the provided callback on Reset call after it has been
// called N times.
func NewCountTicker(n int, callback func()) *ManualTicker {
ticker := NewManualTicker()
ticker.ResetFunc = func() {
n--
if n < 0 {
callback()
return
}
ticker.Tick()
}
return ticker
}
|