-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathevents.go
70 lines (55 loc) · 1.44 KB
/
events.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
package xmpp
type Listener interface {
}
type MessageListener struct {
callback func(message Message)
}
type UnknownListener struct {
callback func(raw string)
}
type AnyListener struct {
callback func(raw string)
}
type listenerList struct {
listeners []Listener
}
func (list *listenerList) Subscribe(listener Listener) {
list.listeners = append(list.listeners, listener)
}
func (list *listenerList) onAny(callback func(string)) {
list.Subscribe(AnyListener{ callback : callback })
}
func (list *listenerList) onMessage(callback func(Message)) {
list.Subscribe(MessageListener{ callback: callback })
}
func (list *listenerList) onUnknown(callback func(string)) {
list.Subscribe(UnknownListener{ callback : callback })
}
func (list *listenerList) fireOnMessage(msg *Message) {
list.eachListener(func(l Listener) {
fireIfAny(l, msg.Raw())
if msg_l, ok := l.(MessageListener); ok {
msg_l.callback(*msg)
}
})
}
func (list *listenerList) fireOnUnknown(msg string) {
list.eachListener(func(l Listener) {
fireIfAny(l, msg)
if unknown_l, ok := l.(UnknownListener); ok {
unknown_l.callback(msg)
}
})
}
func fireIfAny(l Listener, msg string) {
if any_l, ok := l.(AnyListener); ok {
any_l.callback(msg)
} else {
// not any listener, so nothing to do
}
}
func (list *listenerList) eachListener(callback func(Listener)) {
for i := range list.listeners {
callback(list.listeners[i])
}
}