-
Notifications
You must be signed in to change notification settings - Fork 2
/
ticker.go
131 lines (110 loc) · 2.64 KB
/
ticker.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package events
import (
"reflect"
"sync"
"time"
)
// NewTicker creates new PeriodicEmitter
func NewTicker(emitter *Emitter) *PeriodicEmitter {
actions := make(chan func())
ticker := &PeriodicEmitter{
Emitter: emitter,
actions: actions,
events: make(map[string]*time.Ticker),
timers: []reflect.SelectCase{{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(actions)}},
mapping: make(map[int]string),
}
go ticker.run()
return ticker
}
// PeriodicEmitter is a source of periodic events
type PeriodicEmitter struct {
*Emitter
stopOnce sync.Once
actions chan func()
mapping map[int]string
events map[string]*time.Ticker
timers []reflect.SelectCase
}
func (emitter *PeriodicEmitter) Stop() {
emitter.stopOnce.Do(func() {
close(emitter.actions)
})
}
// RegisterEvent registers new periodic event
func (emitter *PeriodicEmitter) RegisterEvent(event string, period interface{}, handlers ...Listener) {
var timer *time.Ticker
switch value := period.(type) {
case time.Duration:
timer = time.NewTicker(value)
case *time.Ticker:
timer = value
default:
return
}
emitter.actions <- func() {
emitter.mapping[len(emitter.timers)] = event
emitter.timers = append(emitter.timers, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(timer.C)})
emitter.events[event] = timer
if len(handlers) > 0 {
emitter.AddEventListeners(event, handlers...)
}
emitter.refresh()
}
}
// RemoveEvent removes provided event
func (emitter *PeriodicEmitter) RemoveEvent(event string) {
emitter.actions <- func() {
if emitter.remove(event) {
emitter.refresh()
}
}
}
func (emitter *PeriodicEmitter) remove(event string) bool {
timer, exists := emitter.events[event]
if !exists {
return false
}
timer.Stop()
delete(emitter.events, event)
emitter.RemoveEventListeners(event)
return true
}
func (emitter *PeriodicEmitter) stop() {
for event := range emitter.events {
emitter.remove(event)
}
}
func (emitter *PeriodicEmitter) refresh() {
emitter.timers = []reflect.SelectCase{emitter.timers[0]}
for event, timer := range emitter.events {
emitter.mapping[len(emitter.timers)] = event
emitter.timers = append(emitter.timers, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(timer.C),
})
}
}
func (emitter *PeriodicEmitter) run() {
for {
index, value, opened := reflect.Select(emitter.timers)
switch index {
case 0:
if opened {
value.Call(nil)
} else {
emitter.stop()
return
}
default:
if event, exists := emitter.mapping[index]; exists {
if opened {
emitter.Fire(event)
} else {
delete(emitter.events, event)
emitter.refresh()
}
}
}
}
}