-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticker.go
More file actions
62 lines (56 loc) · 1.17 KB
/
ticker.go
File metadata and controls
62 lines (56 loc) · 1.17 KB
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
package gotime
import (
"time"
)
// The terminology may be a bit confusing, but in this package, a Ticker is
// actually a Timer with the ticker parameter set to true. This reduces a lot
// of duplicated code in mockLoop.
// Ticker provides a drop-in replacement for time.Ticker.
type Ticker struct {
C <-chan time.Time
timer *Timer
ticker *time.Ticker
}
// Stop ends the ticker.
func (t *Ticker) Stop() {
if t.ticker != nil {
t.ticker.Stop()
return
}
t.timer.Stop()
}
// Reset stops the ticker and restarts it with the specified duration.
func (t *Ticker) Reset(d time.Duration) {
if t.ticker != nil {
t.ticker.Reset(d)
return
}
loop.chanResetTimer <- &resetTimerParams{
timer: t.timer,
duration: d,
}
<-loop.chanAny
}
// NewTicker creates a new Ticker that will send on the channel C every time
// the specified duration elapses.
func NewTicker(d time.Duration) *Ticker {
if loop == nil {
ticker := time.NewTicker(d)
return &Ticker{
C: ticker.C,
ticker: ticker,
}
}
var (
timer = &Timer{
ticker: true,
duration: d,
}
t = &Ticker{
timer: timer,
}
)
loop.chanNewTimer <- t.timer
t.C = <-loop.chanTimeChan
return t
}