-
Notifications
You must be signed in to change notification settings - Fork 3
/
responder_queue.go
89 lines (76 loc) · 1.78 KB
/
responder_queue.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
package bot
import (
"regexp"
"sync"
)
func newListener(capacity int, cbs ...func(*Responder)) listener {
var cb func(*Responder)
var ch chan Responder
if len(cbs) > 0 {
cb = cbs[0]
} else {
ch = make(chan Responder, capacity)
}
return listener{ch: ch, callback: cb}
}
// Listener holds the listening channel or callback
type listener struct {
ch chan Responder
callback func(*Responder)
IsClosed bool
Once bool
}
func (l *listener) Close() {
l.IsClosed = true
close(l.ch)
}
func (l *listener) Dispatch(e *Responder) {
if l.callback != nil {
l.callback(e)
return
}
if !l.IsClosed {
l.ch <- *e
}
}
// responderQueue is responsible for holding listeners and
// dispatching events to them asynchronously
type responderQueue struct {
Capacity int
mu sync.RWMutex
listeners map[messageType][]listener
}
func newResponderQueue(capacity int) *responderQueue {
return &responderQueue{
Capacity: capacity,
listeners: make(map[messageType][]listener),
}
}
func (e *responderQueue) Forward(r *Robot, ch <-chan Message) {
for msg := range ch {
rs := newResponder(r, msg)
exp := regexp.MustCompile("^@?" + r.Username() + "\\p{Zs}")
if msg.Type == DefaultMessage && exp.MatchString(msg.Text) {
e.Emit(Response, rs)
}
e.Emit(msg.Type, rs)
}
}
// On binds a listener to the responderQueue
func (e *responderQueue) On(on messageType, cbs ...func(*Responder)) <-chan Responder {
e.mu.Lock()
defer e.mu.Unlock()
l := newListener(e.Capacity, cbs...)
e.listeners[on] = append(e.listeners[on], l)
return l.ch
}
// Emit dispatches an event to corresponding listeners
func (e *responderQueue) Emit(on messageType, rs *Responder) {
e.mu.RLock()
defer e.mu.RUnlock()
if ls, ok := e.listeners[on]; ok {
for _, l := range ls {
l.Dispatch(rs)
}
}
}