This commit is contained in:
pocke 2015-01-08 09:35:15 +09:00
parent 62b2927abc
commit 7b75facf1c
1 changed files with 20 additions and 14 deletions

34
pfs.go
View File

@ -7,21 +7,26 @@ import (
) )
type PFS struct { type PFS struct {
// funcs are listener functions. // listeners are listener functions.
funcs []reflect.Value listeners []reflect.Value
mu *sync.RWMutex lmu *sync.RWMutex
filters []reflect.Value
fmu *sync.RWMutex
} }
func New() *PFS { func New() *PFS {
return &PFS{ return &PFS{
funcs: make([]reflect.Value, 0), listeners: make([]reflect.Value, 0),
mu: &sync.RWMutex{}, lmu: &sync.RWMutex{},
filters: make([]reflect.Value, 0),
fmu: &sync.RWMutex{},
} }
} }
func (p *PFS) Pub(args ...interface{}) { func (p *PFS) Pub(args ...interface{}) {
p.mu.Lock() p.lmu.Lock()
defer p.mu.Unlock() defer p.lmu.Unlock()
arguments := make([]reflect.Value, 0, len(args)) arguments := make([]reflect.Value, 0, len(args))
for _, v := range args { for _, v := range args {
@ -30,8 +35,8 @@ func (p *PFS) Pub(args ...interface{}) {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(p.funcs)) wg.Add(len(p.listeners))
for _, fn := range p.funcs { for _, fn := range p.listeners {
go func(f reflect.Value) { go func(f reflect.Value) {
defer wg.Done() defer wg.Done()
f.Call(arguments) f.Call(arguments)
@ -42,8 +47,8 @@ func (p *PFS) Pub(args ...interface{}) {
} }
func (p *PFS) Sub(f interface{}) error { func (p *PFS) Sub(f interface{}) error {
p.mu.Lock() p.lmu.Lock()
defer p.mu.Unlock() defer p.lmu.Unlock()
fn := reflect.ValueOf(f) fn := reflect.ValueOf(f)
@ -51,11 +56,11 @@ func (p *PFS) Sub(f interface{}) error {
return fmt.Errorf("Argument should be a function") return fmt.Errorf("Argument should be a function")
} }
if len(p.funcs) != 0 { if len(p.listeners) != 0 {
// TODO: check fn arguments // TODO: check fn arguments
} }
p.funcs = append(p.funcs, fn) p.listeners = append(p.listeners, fn)
return nil return nil
} }
@ -63,6 +68,7 @@ func (p *PFS) Off() {
} }
func (p *PFS) Filter(func() bool) { func (p *PFS) Filter(f interface{}) error {
return nil
} }