forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
35 lines (29 loc) · 659 Bytes
/
event.go
File metadata and controls
35 lines (29 loc) · 659 Bytes
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
package gobot
import "sync"
type callback struct {
f func(interface{})
once bool
}
// Event executes the list of Callbacks when Chan is written to.
type Event struct {
sync.Mutex
Callbacks []callback
}
// NewEvent returns a new Event which is now listening for data.
func NewEvent() *Event {
return &Event{}
}
// Write writes data to the Event, it will not block and will not buffer if there
// are no active subscribers to the Event.
func (e *Event) Write(data interface{}) {
e.Lock()
defer e.Unlock()
tmp := []callback{}
for _, cb := range e.Callbacks {
go cb.f(data)
if !cb.once {
tmp = append(tmp, cb)
}
}
e.Callbacks = tmp
}