-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdispatcher.go
58 lines (48 loc) · 1.23 KB
/
dispatcher.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
package godispatcher
import "sync"
// Event is used to transfer event-related data.
// It is passed to listeners when Emit() is called
type Event struct {
Name string
Data interface{}
}
// Listener is a type for listeners
type Listener func(event *Event) error
// Dispatcher (a.k.a event emitter, dispatcher) stores listener and notifies them when an event emitted
type Dispatcher struct {
mutex *sync.RWMutex
listeners map[string][]Listener
}
// New returns new Dispatcher
func New() Dispatcher {
return Dispatcher{
mutex: &sync.RWMutex{},
listeners: make(map[string][]Listener),
}
}
// On adds new listener.
// listener is a callback function that will be called when event emits
func (o Dispatcher) On(event string, listener Listener) {
o.mutex.Lock()
defer o.mutex.Unlock()
o.listeners[event] = append(o.listeners[event], listener)
}
// Emit notifies listeners about the event
func (o Dispatcher) Emit(eventName string, data interface{}) error {
o.mutex.RLock()
defer o.mutex.RUnlock()
listeners, ok := o.listeners[eventName]
if !ok {
return nil
}
for i := range listeners {
event := &Event{
Name: eventName,
Data: data,
}
if err := listeners[i](event); err != nil {
return err
}
}
return nil
}