-
Notifications
You must be signed in to change notification settings - Fork 1
/
listeners.go
162 lines (117 loc) · 2.74 KB
/
listeners.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
package eventbus
import (
"reflect"
"sync"
)
type Listeners struct {
callbacks map[*reflect.Value]*reflect.Value
mtx *sync.RWMutex
remove RemoveCallback
sliceCallbacks []*reflect.Value
signature *reflect.Type
}
func isSameType(v1, v2 reflect.Type) bool {
if v1.Kind() != v2.Kind() {
return false
}
return v1.AssignableTo(v2)
}
func NewListeners(remove RemoveCallback) *Listeners {
return &Listeners{
callbacks: make(map[*reflect.Value]*reflect.Value),
mtx: &sync.RWMutex{},
remove: remove,
}
}
func (l *Listeners) IsSame(v *reflect.Value) bool {
l.mtx.Lock()
if l.signature == nil {
l.mtx.Unlock()
return true
}
signature := *l.signature
l.mtx.Unlock()
vType := v.Type()
lenIn := vType.NumIn()
if signature.NumIn() != lenIn {
return false
}
for i := 0; i < lenIn; i++ {
if !isSameType(signature.In(i), vType.In(i)) {
return false
}
}
return true
}
func (l *Listeners) IsValidPayload(payload []interface{}) ([]reflect.Value, bool) {
l.mtx.Lock()
if l.signature == nil {
l.mtx.Unlock()
return nil, false
}
signature := *l.signature
l.mtx.Unlock()
var (
isVariadic = signature.IsVariadic()
lastNrInput = signature.NumIn() - 1
lenPayload = len(payload)
elem reflect.Type
)
//Check for payload to have the minimum of required params
if (!isVariadic && signature.NumIn() != lenPayload) ||
(isVariadic && lenPayload < lastNrInput) {
return nil, false
}
//Transform and validate payload
transformedPayload := make([]reflect.Value, len(payload))
for i, j := 0, 0; i < lenPayload; i++ {
transformedPayload[i] = reflect.ValueOf(payload[i])
if !isVariadic || j < lastNrInput {
elem = signature.In(j)
j++
} else if i == j {
elem = signature.In(j).Elem()
}
if !isSameType(elem, transformedPayload[i].Type()) {
return nil, false
}
}
return transformedPayload, true
}
//Delete callbacks and self delete himself from bus
func (l *Listeners) Delete(index *reflect.Value) {
l.mtx.Lock()
delete(l.callbacks, index)
hasToRemove := len(l.callbacks) == 0 && l.remove != nil
l.signature = nil
l.sliceCallbacks = nil
l.mtx.Unlock()
if hasToRemove {
l.remove()
}
}
func (l *Listeners) Add(callback *reflect.Value) {
l.mtx.Lock()
if _, isCallback := l.callbacks[callback]; !isCallback {
l.callbacks[callback] = callback
l.sliceCallbacks = append(l.sliceCallbacks, callback)
}
if l.signature == nil {
typeValue := callback.Type()
l.signature = &typeValue
}
l.mtx.Unlock()
}
func (l *Listeners) Callbacks() []*reflect.Value {
l.mtx.RLock()
if l.sliceCallbacks == nil {
callbacks := make([]*reflect.Value, len(l.callbacks))
j := 0
for i := range l.callbacks {
callbacks[j] = i
j++
}
}
l.mtx.RUnlock()
return l.sliceCallbacks
}