/
events.go
145 lines (115 loc) · 2.86 KB
/
events.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package common
import (
"reflect"
"sync"
)
type Event interface{}
type EventChan chan Event
type EventFunc func(Event)
type EventType reflect.Type
type EventManager struct {
mu sync.Mutex
chans map[EventType][]EventChan
funcs map[EventType][]*EventFunc
}
type AllEvents struct{}
var (
Events *EventManager
)
func init() {
Events = NewEventManager()
}
func NewEventManager() *EventManager {
return &EventManager{
chans: make(map[EventType][]EventChan),
funcs: make(map[EventType][]*EventFunc),
}
}
// NewChanReceiver adds an event listener to the Dog struct instance
func (this *EventManager) NewChanReceiver(event interface{}) EventChan {
this.mu.Lock()
defer this.mu.Unlock()
eventType := reflect.TypeOf(event)
eventListener := make(EventChan)
DebugFunc(eventType)
if _, ok := this.chans[eventType]; ok {
this.chans[eventType] = append(this.chans[eventType], eventListener)
} else {
this.chans[eventType] = []EventChan{eventListener}
}
return eventListener
}
// NewFuncReceiver adds an event listener to the Dog struct instance
func (this *EventManager) NewFuncReceiver(event interface{}, eventFunc EventFunc) *EventFunc {
this.mu.Lock()
defer this.mu.Unlock()
eventType := reflect.TypeOf(event)
DebugFunc()
if _, ok := this.funcs[eventType]; ok {
this.funcs[eventType] = append(this.funcs[eventType], &eventFunc)
} else {
this.funcs[eventType] = []*EventFunc{&eventFunc}
}
return &eventFunc
}
// DestroyFuncReceiver removes an event listener from the Dog struct instance
func (this *EventManager) DestroyChanReceiver(eventChan EventChan) {
this.mu.Lock()
defer this.mu.Unlock()
DebugFunc()
for _, chans := range this.chans {
for i := range chans {
if chans[i] == eventChan {
close(chans[i])
chans = append(chans[:i], chans[i+1:]...)
break
}
}
}
}
// DestroyFuncReceiver removes an event listener from the Dog struct instance
func (this *EventManager) DestroyFuncReceiver(eventFunc *EventFunc) {
this.mu.Lock()
defer this.mu.Unlock()
DebugFunc()
for eventType, funcs := range this.funcs {
for i := range funcs {
if funcs[i] == eventFunc {
funcs = append(funcs[:i], funcs[i+1:]...)
break
}
}
if len(funcs) == 0 {
delete(this.funcs, eventType)
} else {
this.funcs[eventType] = funcs
}
}
}
// Emit emits an event on the Dog struct instance
func (this *EventManager) Emit(event interface{}) bool {
this.mu.Lock()
defer this.mu.Unlock()
b := false
eventType := reflect.TypeOf(event)
DebugFunc(eventType)
if chans, ok := this.chans[eventType]; ok {
for _, receiver := range chans {
receiver <- event
b = true
}
}
if funcs, ok := this.funcs[eventType]; ok {
for _, receiver := range funcs {
(*receiver)(event)
b = true
}
}
if funcs, ok := this.funcs[reflect.TypeOf(AllEvents{})]; ok {
for _, receiver := range funcs {
(*receiver)(event)
b = true
}
}
return b
}