-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
event_emitter.go
163 lines (137 loc) · 3.29 KB
/
event_emitter.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package playwright
import (
"math"
"reflect"
"slices"
"sync"
)
type EventEmitter interface {
Emit(name string, payload ...interface{}) bool
ListenerCount(name string) int
On(name string, handler interface{})
Once(name string, handler interface{})
RemoveListener(name string, handler interface{})
RemoveListeners(name string)
}
type (
eventEmitter struct {
eventsMutex sync.Mutex
events map[string]*eventRegister
hasInit bool
}
eventRegister struct {
sync.Mutex
listeners []listener
}
listener struct {
handler interface{}
once bool
}
)
func NewEventEmitter() EventEmitter {
return &eventEmitter{}
}
func (e *eventEmitter) Emit(name string, payload ...interface{}) (hasListener bool) {
e.eventsMutex.Lock()
e.init()
evt, ok := e.events[name]
if !ok {
e.eventsMutex.Unlock()
return
}
e.eventsMutex.Unlock()
return evt.callHandlers(payload...) > 0
}
func (e *eventEmitter) Once(name string, handler interface{}) {
e.addEvent(name, handler, true)
}
func (e *eventEmitter) On(name string, handler interface{}) {
e.addEvent(name, handler, false)
}
func (e *eventEmitter) RemoveListener(name string, handler interface{}) {
e.eventsMutex.Lock()
defer e.eventsMutex.Unlock()
e.init()
if evt, ok := e.events[name]; ok {
evt.Lock()
defer evt.Unlock()
evt.removeHandler(handler)
}
}
func (e *eventEmitter) RemoveListeners(name string) {
e.eventsMutex.Lock()
defer e.eventsMutex.Unlock()
e.init()
delete(e.events, name)
}
// ListenerCount count the listeners by name, count all if name is empty
func (e *eventEmitter) ListenerCount(name string) int {
e.eventsMutex.Lock()
defer e.eventsMutex.Unlock()
e.init()
if name != "" {
evt, ok := e.events[name]
if !ok {
return 0
}
return evt.count()
}
count := 0
for key := range e.events {
count += e.events[key].count()
}
return count
}
func (e *eventEmitter) addEvent(name string, handler interface{}, once bool) {
e.eventsMutex.Lock()
defer e.eventsMutex.Unlock()
e.init()
if _, ok := e.events[name]; !ok {
e.events[name] = &eventRegister{
listeners: make([]listener, 0),
}
}
e.events[name].addHandler(handler, once)
}
func (e *eventEmitter) init() {
if !e.hasInit {
e.events = make(map[string]*eventRegister, 0)
e.hasInit = true
}
}
func (er *eventRegister) addHandler(handler interface{}, once bool) {
er.Lock()
defer er.Unlock()
er.listeners = append(er.listeners, listener{handler: handler, once: once})
}
func (er *eventRegister) count() int {
er.Lock()
defer er.Unlock()
return len(er.listeners)
}
func (er *eventRegister) removeHandler(handler interface{}) {
handlerPtr := reflect.ValueOf(handler).Pointer()
er.listeners = slices.DeleteFunc(er.listeners, func(l listener) bool {
return reflect.ValueOf(l.handler).Pointer() == handlerPtr
})
}
func (er *eventRegister) callHandlers(payloads ...interface{}) int {
payloadV := make([]reflect.Value, 0)
for _, p := range payloads {
payloadV = append(payloadV, reflect.ValueOf(p))
}
handle := func(l listener) {
handlerV := reflect.ValueOf(l.handler)
handlerV.Call(payloadV[:int(math.Min(float64(handlerV.Type().NumIn()), float64(len(payloadV))))])
}
er.Lock()
defer er.Unlock()
count := len(er.listeners)
for _, l := range er.listeners {
if l.once {
defer er.removeHandler(l.handler)
}
handle(l)
}
return count
}