forked from belak/go-seabird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux_basic.go
55 lines (46 loc) · 1.41 KB
/
mux_basic.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
package seabird
import (
"sync"
"github.com/go-irc/irc"
)
// BasicMux is a simple IRC event multiplexer. It matches the command against
// registered Handlers and calls the correct set.
//
// Handlers will be processed in the order in which they were added.
// Registering a handler with a "*" command will cause it to receive all events.
// Note that even though "*" will match all commands, glob matching is not used.
type BasicMux struct {
m map[string][]HandlerFunc
mu *sync.Mutex
}
// NewBasicMux will create an initialized BasicMux with no handlers.
func NewBasicMux() *BasicMux {
return &BasicMux{
make(map[string][]HandlerFunc),
&sync.Mutex{},
}
}
// Event will register a Handler
func (mux *BasicMux) Event(c string, h HandlerFunc) {
mux.mu.Lock()
defer mux.mu.Unlock()
mux.m[c] = append(mux.m[c], h)
}
// HandleEvent allows us to be a Handler so we can nest Handlers
//
// The BasicMux simply dispatches all the Handler commands as needed
func (mux *BasicMux) HandleEvent(b *Bot, msg *irc.Message) {
// Lock our handlers so we don't crap bricks if a
// handler is added or removed from under our feet.
mux.mu.Lock()
defer mux.mu.Unlock()
// Star means ALL THE THINGS. Really, this is only useful for logging.
for _, h := range mux.m["*"] {
h(b, msg)
}
// Now that we've done the global handlers, we can run the ones specific to
// this command.
for _, h := range mux.m[msg.Command] {
h(b, msg)
}
}