1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
2014-06-11 11:37:20 -07:00

35 lines
464 B
Go

package gobot
type Event struct {
Chan chan interface{}
Callbacks []func(interface{})
}
func NewEvent() *Event {
e := &Event{
Chan: make(chan interface{}, 1),
Callbacks: []func(interface{}){},
}
go func() {
for {
e.Read()
}
}()
return e
}
func (e *Event) Write(data interface{}) {
select {
case e.Chan <- data:
default:
}
}
func (e *Event) Read() {
for s := range e.Chan {
for _, f := range e.Callbacks {
go f(s)
}
}
}