-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventhandlers.go
More file actions
98 lines (87 loc) · 2.85 KB
/
eventhandlers.go
File metadata and controls
98 lines (87 loc) · 2.85 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
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
90
91
92
93
94
95
96
97
98
package event
type EventHandlerMethod func(e *Event) error
type EventHandler struct {
handlerDefault EventHandlerMethod
handlers map[string]EventHandlerMethod
oneshotDefault EventHandlerMethod
handlersOnce map[string]EventHandlerMethod
}
func NewEventHandler() (eh *EventHandler) {
eh = new(EventHandler)
eh.handlers = make(map[string]EventHandlerMethod)
return
}
// Process executes a matching handler for an event.
func (eh *EventHandler) Process(e *Event) error {
if handler := eh.GetHandler(e.GetID()); handler != nil {
return handler(e)
}
if eh.handlerDefault != nil {
return eh.handlerDefault(e)
}
return ErrorEventHandlerNoMatch(e.GetID())
}
// Handle assigns a Go function to receive events of the specified IDs.
// The method receives one event at a time, but can be in any order and must be safe for multiple concurrent calls.
// Specifying no event IDs will assign the method to be the default event handler.
func (eh *EventHandler) Handle(method EventHandlerMethod, eventID ...string) *EventHandler {
if len(eventID) > 0 {
for i := 0; i < len(eventID); i++ {
id := eventID[i]
if id == "" {
panic("EventHandler: Handle: eventID must never be empty string")
}
eh.handlers[id] = method
}
} else {
eh.handlerDefault = method
}
return eh
}
// Oneshot returns a wrapper over the given method that will unregister itself after being processed once.
func (eh *EventHandler) Oneshot(method EventHandlerMethod) EventHandlerMethod {
return func(e *Event) error {
err := method(e)
eh.Unhandle(e.GetID())
return err
}
}
// Unhandle removes one or more handlers. Specifying no event IDs will remove the default event handler.
func (eh *EventHandler) Unhandle(eventID ...string) *EventHandler {
if len(eventID) > 0 {
for i := 0; i < len(eventID); i++ {
id := eventID[i]
if id == "" {
panic("EventHandler: Unhandle: eventID must never be empty string")
}
delete(eh.handlers, id)
}
} else {
eh.handlerDefault = nil
}
return eh
}
// GetEventIDs returns the list of handled event IDs.
func (eh *EventHandler) GetEventIDs() []string {
eventIDs := make([]string, 0)
for eventID := range eh.handlers {
eventIDs = append(eventIDs, eventID)
}
return eventIDs
}
// GetHandler returns a handler for an event ID.
func (eh *EventHandler) GetHandler(eventID string) EventHandlerMethod {
handler, exists := eh.handlers[eventID]
if exists {
return handler
}
return nil
}
// GetHandlersMapClone creates a clone of the handler mapping and returns the clone. It is up to the caller to dereference the clone.
func (eh *EventHandler) GetHandlersMapClone() map[string]EventHandlerMethod {
handlers := make(map[string]EventHandlerMethod)
for eventID, handler := range eh.handlers {
handlers[eventID] = handler
}
return handlers
}