forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_emitter.go
126 lines (107 loc) · 3.08 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
package playwright
import (
"math"
"reflect"
"sync"
)
type (
eventRegister struct {
once []interface{}
on []interface{}
}
eventEmitter struct {
eventsMutex sync.Mutex
events map[string]*eventRegister
addEventHandlers []func(name string, handler interface{})
removeEventHandlers []func(name string, handler interface{})
}
)
func (e *eventEmitter) Emit(name string, payload ...interface{}) {
e.eventsMutex.Lock()
defer e.eventsMutex.Unlock()
if _, ok := e.events[name]; !ok {
return
}
payloadV := make([]reflect.Value, 0)
for _, p := range payload {
payloadV = append(payloadV, reflect.ValueOf(p))
}
callHandlers := func(handlers []interface{}) {
for _, handler := range handlers {
handlerV := reflect.ValueOf(handler)
handlerV.Call(payloadV[:int(math.Min(float64(handlerV.Type().NumIn()), float64(len(payloadV))))])
}
}
callHandlers(e.events[name].on)
callHandlers(e.events[name].once)
e.events[name].once = make([]interface{}, 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) addEventHandler(handler func(name string, handler interface{})) {
e.addEventHandlers = append(e.addEventHandlers, handler)
}
func (e *eventEmitter) removeEventHandler(handler func(name string, handler interface{})) {
e.removeEventHandlers = append(e.removeEventHandlers, handler)
}
func (e *eventEmitter) RemoveListener(name string, handler interface{}) {
for _, mitm := range e.removeEventHandlers {
mitm(name, handler)
}
e.eventsMutex.Lock()
defer e.eventsMutex.Unlock()
if _, ok := e.events[name]; !ok {
return
}
handlerPtr := reflect.ValueOf(handler).Pointer()
onHandlers := []interface{}{}
for idx := range e.events[name].on {
eventPtr := reflect.ValueOf(e.events[name].on[idx]).Pointer()
if eventPtr != handlerPtr {
onHandlers = append(onHandlers, e.events[name].on[idx])
}
}
e.events[name].on = onHandlers
onceHandlers := []interface{}{}
for idx := range e.events[name].once {
eventPtr := reflect.ValueOf(e.events[name].once[idx]).Pointer()
if eventPtr != handlerPtr {
onceHandlers = append(onceHandlers, e.events[name].once[idx])
}
}
e.events[name].once = onceHandlers
}
func (e *eventEmitter) ListenerCount(name string) int {
count := 0
e.eventsMutex.Lock()
for key := range e.events {
count += len(e.events[key].on) + len(e.events[key].once)
}
e.eventsMutex.Unlock()
return count
}
func (e *eventEmitter) addEvent(name string, handler interface{}, once bool) {
for _, mitm := range e.addEventHandlers {
mitm(name, handler)
}
e.eventsMutex.Lock()
if _, ok := e.events[name]; !ok {
e.events[name] = &eventRegister{
on: make([]interface{}, 0),
once: make([]interface{}, 0),
}
}
if once {
e.events[name].once = append(e.events[name].once, handler)
} else {
e.events[name].on = append(e.events[name].on, handler)
}
e.eventsMutex.Unlock()
}
func (e *eventEmitter) initEventEmitter() {
e.events = make(map[string]*eventRegister)
}