forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_base.go
180 lines (157 loc) · 4.27 KB
/
event_base.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gxui
import (
"fmt"
"reflect"
)
type EventListener struct {
Id int
Function reflect.Value
}
type eventBaseSubscription struct {
event *EventBase
id int
}
func (s *eventBaseSubscription) Unlisten() {
if s.event != nil {
s.event.unlisten(s.id)
s.event = nil
}
}
type EventBase struct {
unlisten func(id int)
paramTypes []reflect.Type
isVariadic bool
listeners []EventListener
nextId int
}
func (e *EventBase) init(signature interface{}) {
e.unlisten = func(id int) {
for i, l := range e.listeners {
if l.Id == id {
copy(e.listeners[i:], e.listeners[i+1:])
e.listeners = e.listeners[:len(e.listeners)-1]
return
}
}
panic(fmt.Errorf("Listener not added to event"))
}
f := reflect.TypeOf(signature)
e.paramTypes = make([]reflect.Type, f.NumIn())
for i, _ := range e.paramTypes {
e.paramTypes[i] = f.In(i)
}
e.isVariadic = f.IsVariadic()
}
func (e *EventBase) String() string {
s := "Event<"
for i, t := range e.paramTypes {
if i > 0 {
s += ", "
}
if e.isVariadic && i == len(e.paramTypes)-1 {
s += "..."
}
s += t.String()
}
return s + ">"
}
func (e *EventBase) VerifySignature(argTys []reflect.Type, isVariadic bool) {
paramTypes := e.paramTypes
if isVariadic {
if len(argTys) < len(paramTypes)-1 {
panic(fmt.Errorf("%v.Fire(%v) Too few arguments. Must have at least %v, but got %v",
e.String(), argTys, len(paramTypes), len(argTys)))
}
for i, argTy := range argTys {
varIdx := len(paramTypes) - 1
if i >= varIdx {
paramTy := paramTypes[varIdx].Elem()
if !argTy.AssignableTo(paramTy) {
panic(fmt.Errorf("%v.Fire(%v) Variadic argument %v for was of the wrong type. Got: %v, Expected: %v",
e.String(), argTys, i-varIdx, argTy, paramTy))
}
} else {
paramTy := paramTypes[i]
if !argTy.AssignableTo(paramTy) {
panic(fmt.Errorf("%v.Fire(%v) Argument %v for was of the wrong type. Got: %v, Expected: %v",
e.String(), argTys, i, argTy, paramTy))
}
}
}
} else {
if len(paramTypes) != len(argTys) {
panic(fmt.Errorf("%v.Fire(%v) Argument count mismatch. Expected %d, got %d",
e.String(), argTys, len(paramTypes), len(argTys)))
}
for i, argTy := range argTys {
paramTy := paramTypes[i]
if !argTy.AssignableTo(paramTy) {
panic(fmt.Errorf("%v.Fire(%v) Argument %v for was of the wrong type. Got: %v, Expected: %v",
e.String(), argTys, i, argTy, paramTy))
}
}
}
}
func (e *EventBase) VerifyArguments(args []interface{}) {
argTys := make([]reflect.Type, len(args))
for i, arg := range args {
argTys[i] = reflect.TypeOf(arg)
}
e.VerifySignature(argTys, e.isVariadic)
}
func (e *EventBase) InvokeListeners(args []interface{}) {
argVals := make([]reflect.Value, len(args))
for i, arg := range args {
argVals[i] = reflect.ValueOf(arg)
}
for _, l := range e.listeners {
l.Function.Call(argVals)
}
}
// Event compliance
func (e *EventBase) Listen(listener interface{}) EventSubscription {
var paramTypes []reflect.Type
var function reflect.Value
reflectTy := reflect.TypeOf(listener)
if reflectTy.Kind() == reflect.Func {
paramTypes = make([]reflect.Type, reflectTy.NumIn())
for i, _ := range paramTypes {
paramTypes[i] = reflectTy.In(i)
}
function = reflect.ValueOf(listener)
} else {
switch ty := listener.(type) {
case Event:
paramTypes = ty.ParameterTypes()
function = reflect.ValueOf(listener).MethodByName("Fire")
default:
panic(fmt.Errorf("Listener cannot be of type %v", reflectTy.String()))
}
}
if function.IsNil() {
panic("Listener function is nil")
}
for i, listenerTy := range paramTypes {
if !listenerTy.AssignableTo(e.paramTypes[i]) {
panic(fmt.Errorf("%v.Listen(%v) Listener parameter %v for was of the wrong type. Got: %v, Expected: %v",
e.String(), listener, i, listenerTy, e.paramTypes[i]))
}
}
id := e.nextId
e.nextId++
e.listeners = append(e.listeners, EventListener{
Id: id,
Function: function,
})
return &eventBaseSubscription{e, id}
}
func (e *EventBase) Fire(args ...interface{}) {
e.VerifyArguments(args)
e.InvokeListeners(args)
}
func (e *EventBase) ParameterTypes() []reflect.Type {
return e.paramTypes
}