This commit is contained in:
程广 2024-11-16 16:12:20 +08:00
parent 533c3d1be0
commit f4a43b64be
4 changed files with 66 additions and 18 deletions

View File

@ -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)
}

Binary file not shown.

33
main.go
View File

@ -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
}

View File

@ -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")
}