pocke.goevent/goevent.go

131 lines
2.3 KiB
Go
Raw Normal View History

2015-01-11 13:24:14 +08:00
package goevent
2015-01-07 08:59:35 +08:00
import (
"fmt"
"reflect"
"sync"
)
2015-01-11 13:24:14 +08:00
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-11 13:24:14 +08:00
func New() *Event {
return &Event{}
2015-01-07 21:24:56 +08:00
}
2015-01-11 14:24:29 +08:00
func (p *Event) Trigger(args ...interface{}) error {
2015-01-08 08:35:15 +08:00
p.lmu.Lock()
defer p.lmu.Unlock()
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-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-11 14:24:29 +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-11 21:33:06 +08:00
func (p *Event) Off(f interface{}) {
fn := reflect.ValueOf(f)
p.lmu.Lock()
defer p.lmu.Unlock()
l := len(p.listeners)
for i := 0; i < l; i++ {
if fn == p.listeners[i] {
p.listeners = append(p.listeners[:i], p.listeners[i+1:]...)
l--
i--
}
}
2015-01-07 08:59:35 +08:00
}
2015-01-11 13:24:14 +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-11 13:24:14 +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
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
}