-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevents_hub.go
50 lines (42 loc) · 971 Bytes
/
events_hub.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
package web
import "fmt"
type idEventFunc struct {
id string
ef EventFunc
}
type EventsHub struct {
eventFuncs []*idEventFunc
}
func (p *EventsHub) String() string {
var rs []string
for _, ne := range p.eventFuncs {
rs = append(rs, ne.id)
}
return fmt.Sprintf("%#+v", rs)
}
func (p *EventsHub) RegisterEventFunc(eventFuncId string, ef EventFunc) (key string) {
key = eventFuncId
if p.eventFuncById(eventFuncId) != nil {
return
}
p.eventFuncs = append(p.eventFuncs, &idEventFunc{eventFuncId, ef})
return
}
func (p *EventsHub) addMultipleEventFuncs(vs ...interface{}) (key string) {
if len(vs)%2 != 0 {
panic("id and func not paired")
}
for i := 0; i < len(vs); i = i + 2 {
p.RegisterEventFunc(vs[i].(string), vs[i+1].(func(ctx *EventContext) (r EventResponse, err error)))
}
return
}
func (p *EventsHub) eventFuncById(id string) (r EventFunc) {
for _, ne := range p.eventFuncs {
if ne.id == id {
r = ne.ef
return
}
}
return
}