forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventer.go
123 lines (105 loc) · 2.62 KB
/
eventer.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gobot
type eventChannel chan *Event
type eventer struct {
// map of valid Event names
eventnames map[string]string
// new events get put in to the event channel
in eventChannel
// slice of out channels used by subscribers
outs []eventChannel
}
// Eventer is the interface which describes how a Driver or Adaptor
// handles events.
type Eventer interface {
// Events returns the map of valid Event names.
Events() (eventnames map[string]string)
// Event returns an Event string from map of valid Event names.
// Mostly used to validate that an Event name is valid.
Event(name string) string
// AddEvent registers a new Event name.
AddEvent(name string)
// DeleteEvent removes a previously registered Event name.
DeleteEvent(name string)
// Publish new events to anyone listening
Publish(name string, data interface{})
// Subscribe to any events from this eventer
Subscribe() (events eventChannel)
// Event handler
On(name string, f func(s interface{})) (err error)
// Event handler, only exectues one time
Once(name string, f func(s interface{})) (err error)
}
// NewEventer returns a new Eventer.
func NewEventer() Eventer {
evtr := &eventer{
eventnames: make(map[string]string),
in: make(eventChannel, 1),
outs: make([]eventChannel, 1),
}
// goroutine to cascade in events to all out event channels
go func() {
for {
select {
case evt := <-evtr.in:
for _, out := range evtr.outs[1:] {
out <- evt
}
}
}
}()
return evtr
}
func (e *eventer) Events() map[string]string {
return e.eventnames
}
func (e *eventer) Event(name string) string {
return e.eventnames[name]
}
func (e *eventer) AddEvent(name string) {
e.eventnames[name] = name
}
func (e *eventer) DeleteEvent(name string) {
delete(e.eventnames, name)
}
func (e *eventer) Publish(name string, data interface{}) {
evt := NewEvent(name, data)
e.in <- evt
}
func (e *eventer) Subscribe() eventChannel {
out := make(eventChannel)
e.outs = append(e.outs, out)
return out
}
// On executes f when e is Published to. Returns ErrUnknownEvent if Event
// does not exist.
func (e *eventer) On(n string, f func(s interface{})) (err error) {
out := e.Subscribe()
go func() {
for {
select {
case evt := <-out:
if evt.Name == n {
f(evt.Data)
}
}
}
}()
return
}
// Once is similar to On except that it only executes f one time. Returns
//ErrUnknownEvent if Event does not exist.
func (e *eventer) Once(n string, f func(s interface{})) (err error) {
out := e.Subscribe()
go func() {
for {
select {
case evt := <-out:
if evt.Name == n {
f(evt.Data)
break
}
}
}
}()
return
}