diff --git a/goemitter_test/main.go b/example/main.go similarity index 63% rename from goemitter_test/main.go rename to example/main.go index 98bd6c2..f7b94c7 100644 --- a/goemitter_test/main.go +++ b/example/main.go @@ -18,12 +18,22 @@ func (s *St) Print(data ...interface{}) { fmt.Println(data...) } +type A struct { + Name string +} + +func (a *A) Exec(data ...interface{}) { + fmt.Println(a.Name, data) +} + func main() { s := &St{Name: "testeer"} em := emitter.NewEmitter() - em.On("test", func(data ...interface{}) { + emh := em.On("test", func(data ...interface{}) { + fmt.Println("should be canceled next") fmt.Println(data...) + }) em.On("test", s.Print) @@ -31,8 +41,19 @@ func main() { s := fmt.Sprint(data...) fmt.Println(interface{}("Once"), s) }) + func() { + em.On("test", func(data ...interface{}) { + fmt.Println("函数内匿名函数被调用") + }) + }() + func() { + a := &A{Name: "ATEST"} + em.On("test", a.Exec) + }() em.EmitSync("test", "hello", "world") + emh.Remove() em.Emit("test", "hello", "world2") + defer func() { if r := recover(); r != nil { log.Println("Recovered in main:", r) @@ -40,5 +61,5 @@ func main() { log.Println(string(debug.Stack())) } }() - time.Sleep(1 * time.Second) + time.Sleep(10 * time.Second) } diff --git a/goemitter_test/main b/goemitter_test/main deleted file mode 100755 index fbf4a6b..0000000 Binary files a/goemitter_test/main and /dev/null differ diff --git a/main.go b/main.go index 21455c0..86ca5f5 100644 --- a/main.go +++ b/main.go @@ -6,19 +6,38 @@ import ( ) type EventEmitter struct { - callbacks map[string][]func(args ...interface{}) + callbacks map[string]map[int]func(args ...interface{}) onceCallbacks map[string][]func(args ...interface{}) lock sync.Mutex + counter int } -func (e *EventEmitter) On(event string, callback func(args ...interface{})) { +type EventHandle struct { + id int + event string + emitter *EventEmitter +} + +func (eh *EventHandle) Remove() { + eh.emitter.lock.Lock() + defer eh.emitter.lock.Unlock() + if _, ok := eh.emitter.callbacks[eh.event]; ok { + delete(eh.emitter.callbacks[eh.event], eh.id) + } +} + +func (e *EventEmitter) On(event string, callback func(args ...interface{})) *EventHandle { e.lock.Lock() defer e.lock.Unlock() - + e.counter++ if _, ok := e.callbacks[event]; !ok { - e.callbacks[event] = []func(args ...interface{}){callback} - } else { - e.callbacks[event] = append(e.callbacks[event], callback) + e.callbacks[event] = make(map[int]func(args ...interface{})) + } + e.callbacks[event][e.counter] = callback + return &EventHandle{ + id: e.counter, + event: event, + emitter: e, } } @@ -73,7 +92,7 @@ func (e *EventEmitter) EmitSync(event string, data ...interface{}) { func NewEmitter() *EventEmitter { ret := &EventEmitter{} - ret.callbacks = make(map[string][]func(args ...interface{})) + ret.callbacks = make(map[string]map[int]func(args ...interface{})) ret.onceCallbacks = make(map[string][]func(args ...interface{})) return ret } diff --git a/main_test.go b/main_test.go index bf45291..18f9462 100644 --- a/main_test.go +++ b/main_test.go @@ -6,18 +6,15 @@ import ( ) func TestEventEmitter_Emit(t *testing.T) { - emitter := &EventEmitter{ - callbacks: make(map[string][]func(args ...interface{})), - onceCallbacks: make(map[string][]func(args ...interface{})), - } + emitter := NewEmitter() // 注册一个普通回调函数 - emitter.callbacks["testEvent"] = append(emitter.callbacks["testEvent"], func(data ...interface{}) { + emitter.On("testEvent", func(data ...interface{}) { t.Log("普通回调函数被调用") }) // 注册一个一次性回调函数 - emitter.onceCallbacks["testEvent"] = append(emitter.onceCallbacks["testEvent"], func(data ...interface{}) { + emitter.Once("testEvent", func(data ...interface{}) { t.Log("一次性回调函数被调用") }) @@ -32,9 +29,7 @@ func TestEventEmitter_Emit(t *testing.T) { } func TestEventEmitter_Once(t *testing.T) { - emitter := &EventEmitter{ - onceCallbacks: make(map[string][]func(args ...interface{})), - } + emitter := NewEmitter() // 测试添加新事件和回调函数 emitter.Once("event1", func(args ...interface{}) {}) @@ -58,3 +53,16 @@ func TestEventEmitter_Once(t *testing.T) { t.Error("一次性回调函数没有被清除") } } + +func TestEmitterWithScope(t *testing.T) { + emitter := NewEmitter() + emitter.On("testEvent", func(data ...interface{}) { + t.Log("普通回调函数被调用") + }) + func() { + emitter.On("testEvent", func(data ...interface{}) { + t.Log("匿名函数被调用") + }) + }() + emitter.Emit("testEvent", "testData") +}