2014-11-21 10:00:32 +08:00
|
|
|
package gobot
|
|
|
|
|
2016-12-09 03:32:02 +08:00
|
|
|
import "sync"
|
|
|
|
|
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
|
|
|
|
|
2016-09-12 19:55:10 +08:00
|
|
|
// map of out channels used by subscribers
|
|
|
|
outs map[eventChannel]eventChannel
|
2016-12-09 03:32:02 +08:00
|
|
|
|
|
|
|
// mutex to protect the eventChannel map
|
|
|
|
eventsMutex sync.Mutex
|
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)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
2016-09-11 19:51:21 +08:00
|
|
|
// Event returns an Event string from map of valid Event names.
|
2016-09-11 19:27:43 +08:00
|
|
|
// Mostly used to validate that an Event name is valid.
|
2016-08-30 19:27:50 +08:00
|
|
|
Event(name string) string
|
2016-09-12 00:27:16 +08:00
|
|
|
|
2016-08-30 19:27:50 +08:00
|
|
|
// AddEvent registers a new Event name.
|
2014-11-21 10:00:32 +08:00
|
|
|
AddEvent(name string)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
2016-09-11 19:27:43 +08:00
|
|
|
// DeleteEvent removes a previously registered Event name.
|
|
|
|
DeleteEvent(name string)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
2016-09-12 19:55:10 +08:00
|
|
|
// Publish new events to any subscriber
|
2016-08-30 19:27:50 +08:00
|
|
|
Publish(name string, data interface{})
|
2016-09-12 00:27:16 +08:00
|
|
|
|
2016-09-12 19:55:10 +08:00
|
|
|
// Subscribe to events
|
2016-08-30 19:27:50 +08:00
|
|
|
Subscribe() (events eventChannel)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
2016-09-12 19:55:10 +08:00
|
|
|
// Unsubscribe from an event channel
|
|
|
|
Unsubscribe(events eventChannel)
|
|
|
|
|
2016-08-30 20:22:09 +08:00
|
|
|
// Event handler
|
|
|
|
On(name string, f func(s interface{})) (err error)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
|
|
|
// Event handler, only executes one time
|
2016-08-30 20:22:09 +08:00
|
|
|
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),
|
2016-09-12 19:55:10 +08:00
|
|
|
outs: make(map[eventChannel]eventChannel),
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
2016-08-30 19:27:50 +08:00
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// goroutine to cascade "in" events to all "out" event channels
|
2016-08-30 19:27:50 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case evt := <-evtr.in:
|
2016-12-09 03:32:02 +08:00
|
|
|
evtr.eventsMutex.Lock()
|
2016-09-12 19:55:10 +08:00
|
|
|
for _, out := range evtr.outs {
|
2016-08-30 19:27:50 +08:00
|
|
|
out <- evt
|
|
|
|
}
|
2016-12-09 03:32:02 +08:00
|
|
|
evtr.eventsMutex.Unlock()
|
2016-08-30 19:27:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return evtr
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// Events returns the map of valid Event names.
|
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-09-12 00:27:16 +08:00
|
|
|
// Event returns an Event string from map of valid Event names.
|
|
|
|
// Mostly used to validate that an Event name is valid.
|
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
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// AddEvent registers a new Event 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
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// DeleteEvent removes a previously registered Event name.
|
2016-09-11 19:27:43 +08:00
|
|
|
func (e *eventer) DeleteEvent(name string) {
|
|
|
|
delete(e.eventnames, name)
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// Publish new events to anyone that is subscribed
|
2016-08-30 19:27:50 +08:00
|
|
|
func (e *eventer) Publish(name string, data interface{}) {
|
|
|
|
evt := NewEvent(name, data)
|
|
|
|
e.in <- evt
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// Subscribe to any events from this eventer
|
2016-08-30 19:27:50 +08:00
|
|
|
func (e *eventer) Subscribe() eventChannel {
|
2016-12-09 03:32:02 +08:00
|
|
|
e.eventsMutex.Lock()
|
|
|
|
defer e.eventsMutex.Unlock()
|
2016-08-30 19:27:50 +08:00
|
|
|
out := make(eventChannel)
|
2016-09-12 19:55:10 +08:00
|
|
|
e.outs[out] = out
|
2016-08-30 19:27:50 +08:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-09-12 19:55:10 +08:00
|
|
|
// Unsubscribe from the event channel
|
|
|
|
func (e *eventer) Unsubscribe(events eventChannel) {
|
2016-12-09 03:32:02 +08:00
|
|
|
e.eventsMutex.Lock()
|
|
|
|
defer e.eventsMutex.Unlock()
|
2016-09-12 19:55:10 +08:00
|
|
|
delete(e.outs, events)
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// On executes the event handler f when e is Published to.
|
2016-08-30 19:27:50 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:27:16 +08:00
|
|
|
// Once is similar to On except that it only executes f one time.
|
2016-08-30 19:27:50 +08:00
|
|
|
func (e *eventer) Once(n string, f func(s interface{})) (err error) {
|
|
|
|
out := e.Subscribe()
|
|
|
|
go func() {
|
2016-09-12 00:27:16 +08:00
|
|
|
ProcessEvents:
|
2016-11-14 05:09:17 +08:00
|
|
|
for evt := range out {
|
|
|
|
if evt.Name == n {
|
|
|
|
f(evt.Data)
|
|
|
|
e.Unsubscribe(out)
|
|
|
|
break ProcessEvents
|
2016-08-30 19:27:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|