forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevtdisp.go
58 lines (41 loc) · 1.03 KB
/
evtdisp.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
package cellnet
import (
"container/list"
"reflect"
)
type EventDispatcher struct {
handler map[string]*list.List
}
func (self *EventDispatcher) Add(name string, callback func(...interface{})) {
arr := self.handler[name]
if arr == nil {
arr = list.New()
self.handler[name] = arr
}
arr.PushBack(callback)
}
func (self *EventDispatcher) Invoke(name string, args ...interface{}) {
if v, ok := self.handler[name]; ok {
for e := v.Front(); e != nil; e = e.Next() {
c := e.Value.(func(...interface{}))
c(args...)
}
}
}
func (self *EventDispatcher) Remove(name string, callback func(...interface{})) {
arr := self.handler[name]
for e := arr.Front(); e != nil; e = e.Next() {
c := e.Value.(func(...interface{}))
if reflect.ValueOf(c).Pointer() == reflect.ValueOf(callback).Pointer() {
arr.Remove(e)
}
}
}
func (self *EventDispatcher) Clear() {
self.handler = make(map[string]*list.List)
}
func NewEventDispatcher() *EventDispatcher {
return &EventDispatcher{
handler: make(map[string]*list.List),
}
}