-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventlistener.go
More file actions
48 lines (39 loc) · 1 KB
/
eventlistener.go
File metadata and controls
48 lines (39 loc) · 1 KB
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
package dispatch
import "github.com/sirupsen/logrus"
// EventListener holds all information neseccary for registering an event
type EventListener struct {
Namespace string
Action string
EventChannel chan EventMessage
}
// eventLoop is the default eventloop handler
func eventLoop(description PackageDescription, eventChannel chan EventMessage) {
hash := make(map[string]EventHandleFunc)
for _, action := range description.Actions {
hash[action.Action] = action.Callback
}
for {
select {
case msg := <-eventChannel:
callback, ok := hash[msg.Action]
if !ok {
logrus.WithFields(
logrus.Fields{
"namespace": msg.Namespace,
"action": msg.Action,
}).Error("Missing handler for event")
}
callback(msg)
}
}
}
func processEvent(e EventMessage) {
route := toRouteString(e.Namespace, e.Action)
if _, ok := listener[route]; !ok {
logrus.WithField("route", route).Error("No listener for route")
return
}
for _, c := range listener[route] {
c <- e
}
}