pocke.goevent/goevent.go

184 lines
3.4 KiB
Go
Raw Permalink Normal View History

2015-01-17 16:44:47 +08:00
/*
Package goevent is event dispatcher.
Listen for event:
e := goevent.New()
e.On(func(i int, s string){
fmt.Printf("%d: %s\n", i, s)
})
Trigger:
e.Trigger(1, "foo")
Use event table:
table := goevent.NewTable()
table.On("foo", func(i int){
fmt.Printf("foo: %d\n", i)
})
table.On("bar", func(s string){
fmt.Printf("bar: %s\n", s)
})
table.Trigger("foo", 1)
table.Trigger("bar", "hoge")
table.Trigger("bar", 38) // retrun error
*/
2015-01-11 13:24:14 +08:00
package goevent
2015-01-07 08:59:35 +08:00
import (
"fmt"
"reflect"
"sync"
)
2015-01-17 16:44:47 +08:00
// Event is an event.
2015-01-17 15:53:23 +08:00
type Event interface {
Trigger(args ...interface{}) error
2015-01-17 16:44:47 +08:00
// f is a function
2015-01-17 15:53:23 +08:00
On(f interface{}) error
Off(f interface{}) error
}
type event struct {
2015-01-08 08:35:15 +08:00
// listeners are listener functions.
listeners []reflect.Value
2015-01-11 12:08:26 +08:00
lmu sync.RWMutex
argTypes []reflect.Type
tmu sync.RWMutex
2015-01-07 08:59:35 +08:00
}
2015-01-17 16:44:47 +08:00
// New creates a new event.
2015-01-17 15:53:23 +08:00
func New() Event {
return &event{}
2015-01-07 21:24:56 +08:00
}
2015-01-17 15:53:23 +08:00
var _ Event = New()
func (p *event) Trigger(args ...interface{}) error {
2015-01-07 21:24:56 +08:00
arguments := make([]reflect.Value, 0, len(args))
2015-01-11 12:19:15 +08:00
argTypes := make([]reflect.Type, 0, len(args))
2015-01-07 21:24:56 +08:00
for _, v := range args {
arguments = append(arguments, reflect.ValueOf(v))
2015-01-11 12:19:15 +08:00
argTypes = append(argTypes, reflect.TypeOf(v))
}
err := p.validateArgs(argTypes)
if err != nil {
return err
2015-01-07 21:24:56 +08:00
}
2015-01-17 17:21:16 +08:00
p.lmu.RLock()
defer p.lmu.RUnlock()
2015-01-08 21:04:48 +08:00
wg := sync.WaitGroup{}
2015-01-08 08:35:15 +08:00
wg.Add(len(p.listeners))
for _, fn := range p.listeners {
2015-01-07 21:24:56 +08:00
go func(f reflect.Value) {
defer wg.Done()
f.Call(arguments)
}(fn)
}
2015-01-07 08:59:35 +08:00
2015-01-07 21:24:56 +08:00
wg.Wait()
2015-01-11 12:19:15 +08:00
return nil
2015-01-07 08:59:35 +08:00
}
2015-01-17 16:44:47 +08:00
// Start to listen an event.
2015-01-17 15:53:23 +08:00
func (p *event) On(f interface{}) error {
2015-01-08 21:20:49 +08:00
fn, err := p.checkFuncSignature(f)
if err != nil {
return err
2015-01-07 08:59:35 +08:00
}
2015-01-08 21:20:49 +08:00
p.lmu.Lock()
defer p.lmu.Unlock()
p.listeners = append(p.listeners, *fn)
2015-01-08 09:04:30 +08:00
2015-01-07 08:59:35 +08:00
return nil
}
2015-01-17 16:44:47 +08:00
// Stop listening an event.
2015-01-17 15:53:23 +08:00
func (p *event) Off(f interface{}) error {
2015-01-11 21:33:06 +08:00
fn := reflect.ValueOf(f)
p.lmu.Lock()
defer p.lmu.Unlock()
l := len(p.listeners)
2015-01-12 11:34:04 +08:00
m := l // for error check
2015-01-11 21:33:06 +08:00
for i := 0; i < l; i++ {
if fn == p.listeners[i] {
2015-01-12 11:34:04 +08:00
// XXX: GC Ref: http://jxck.hatenablog.com/entry/golang-slice-internals
2015-01-11 21:33:06 +08:00
p.listeners = append(p.listeners[:i], p.listeners[i+1:]...)
l--
i--
}
}
2015-01-12 11:34:04 +08:00
if l == m {
return fmt.Errorf("Listener does't exists")
}
return nil
2015-01-07 08:59:35 +08:00
}
2015-01-17 16:44:47 +08:00
// retrun function as reflect.Value
// retrun error if f isn't function, argument is invalid
2015-01-17 15:53:23 +08:00
func (p *event) checkFuncSignature(f interface{}) (*reflect.Value, error) {
2015-01-08 21:20:49 +08:00
fn := reflect.ValueOf(f)
if fn.Kind() != reflect.Func {
return nil, fmt.Errorf("Argument should be a function")
2015-01-08 09:04:30 +08:00
}
2015-01-11 12:08:26 +08:00
types := fnArgTypes(fn)
2015-01-08 09:04:30 +08:00
p.lmu.RLock()
2015-01-08 21:20:49 +08:00
defer p.lmu.RUnlock()
2015-01-11 12:08:26 +08:00
if len(p.listeners) == 0 {
p.tmu.Lock()
defer p.tmu.Unlock()
p.argTypes = types
return &fn, nil
}
2015-01-11 12:19:15 +08:00
err := p.validateArgs(types)
if err != nil {
return nil, err
}
return &fn, nil
}
2015-01-17 16:44:47 +08:00
// if argument size or type are different return error.
2015-01-17 15:53:23 +08:00
func (p *event) validateArgs(types []reflect.Type) error {
2015-01-11 12:08:26 +08:00
p.tmu.RLock()
defer p.tmu.RUnlock()
if len(types) != len(p.argTypes) {
2015-01-11 12:19:15 +08:00
return fmt.Errorf("Argument length expected %d, but got %d", len(p.argTypes), len(types))
2015-01-11 12:08:26 +08:00
}
for i, t := range types {
if t != p.argTypes[i] {
2015-01-11 12:19:15 +08:00
return fmt.Errorf("Argument Error. Args[%d] expected %s, but got %s", i, p.argTypes[i], t)
2015-01-11 12:08:26 +08:00
}
2015-01-08 09:04:30 +08:00
}
2015-01-11 12:19:15 +08:00
return nil
2015-01-07 08:59:35 +08:00
}
2015-01-11 12:08:26 +08:00
2015-01-17 16:44:47 +08:00
// return argument types.
2015-01-11 12:08:26 +08:00
func fnArgTypes(fn reflect.Value) []reflect.Type {
fnType := fn.Type()
fnNum := fnType.NumIn()
types := make([]reflect.Type, 0, fnNum)
for i := 0; i < fnNum; i++ {
types = append(types, fnType.In(i))
}
return types
}