-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
53 lines (43 loc) · 1.34 KB
/
event.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
package goevent
//Listener could be any function which accepts Payload
type Listener func(payload Payload)
//Event represents the events. the main specifier is the name field, but it's private.
//See ByName for how to generate an Event
type Event struct {
name string
aSyncListeners []Listener
syncListeners []Listener
}
//Returns name of the event
func (e Event) Name() string {
return e.name
}
//AddASyncListener adds a listener who will be run asynchronously and via goroutines
func (e *Event) AddASyncListener(listener Listener) *Event {
e.aSyncListeners = append(e.aSyncListeners, listener)
return e
}
//ASyncListeners returns a list of all the async listeners
func (e Event) ASyncListeners() []Listener {
return e.aSyncListeners
}
//AddSyncListener adds a synchronous listener who will block the main thread until done
func (e *Event) AddSyncListener(listener Listener) *Event {
e.syncListeners = append(e.syncListeners, listener)
return e
}
//SyncListeners returns a list of all the sync listeners
func (e Event) SyncListeners() []Listener {
return e.syncListeners
}
//Dispatch is the main function of the package.
//It dispatches the event and calls all the listeners
func (e Event) Dispatch(payload *Payload) {
var l Listener
for _, l = range e.SyncListeners() {
l(*payload)
}
for _, l = range e.ASyncListeners() {
go l(*payload)
}
}