2014-11-21 10:00:32 +08:00
|
|
|
package gobot
|
|
|
|
|
2016-08-30 19:27:50 +08:00
|
|
|
type eventChannel chan *Event
|
|
|
|
|
2014-11-21 10:00:32 +08:00
|
|
|
type eventer struct {
|
2016-08-30 19:27:50 +08:00
|
|
|
// 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
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:27:50 +08:00
|
|
|
// Eventer is the interface which describes how a Driver or Adaptor
|
|
|
|
// handles events.
|
2014-11-21 10:00:32 +08:00
|
|
|
type Eventer interface {
|
2016-08-30 19:27:50 +08:00
|
|
|
// Events returns the map of valid Event names.
|
|
|
|
Events() (eventnames map[string]string)
|
|
|
|
// Event returns the map of valid Event names.
|
|
|
|
Event(name string) string
|
|
|
|
// AddEvent registers a new Event name.
|
2014-11-21 10:00:32 +08:00
|
|
|
AddEvent(name string)
|
2016-08-30 19:27:50 +08:00
|
|
|
// Publish new events to anyone listening
|
|
|
|
Publish(name string, data interface{})
|
|
|
|
// Subscribe to any events from this eventer
|
|
|
|
Subscribe() (events eventChannel)
|
2016-08-30 20:22:09 +08:00
|
|
|
// 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)
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// NewEventer returns a new Eventer.
|
2014-11-21 10:00:32 +08:00
|
|
|
func NewEventer() Eventer {
|
2016-08-30 19:27:50 +08:00
|
|
|
evtr := &eventer{
|
|
|
|
eventnames: make(map[string]string),
|
|
|
|
in: make(eventChannel, 1),
|
|
|
|
outs: make([]eventChannel, 1),
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
2016-08-30 19:27:50 +08:00
|
|
|
|
|
|
|
// 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
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:27:50 +08:00
|
|
|
func (e *eventer) Events() map[string]string {
|
|
|
|
return e.eventnames
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:27:50 +08:00
|
|
|
func (e *eventer) Event(name string) string {
|
|
|
|
return e.eventnames[name]
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *eventer) AddEvent(name string) {
|
2016-08-30 19:27:50 +08:00
|
|
|
e.eventnames[name] = 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
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|