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...) fmt.Println(data...)
} }
type A struct {
Name string
}
func (a *A) Exec(data ...interface{}) {
fmt.Println(a.Name, data)
}
func main() { func main() {
s := &St{Name: "testeer"} s := &St{Name: "testeer"}
em := emitter.NewEmitter() 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...) fmt.Println(data...)
}) })
em.On("test", s.Print) em.On("test", s.Print)
@ -31,8 +41,19 @@ func main() {
s := fmt.Sprint(data...) s := fmt.Sprint(data...)
fmt.Println(interface{}("Once"), s) 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") em.EmitSync("test", "hello", "world")
emh.Remove()
em.Emit("test", "hello", "world2") em.Emit("test", "hello", "world2")
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Println("Recovered in main:", r) log.Println("Recovered in main:", r)
@ -40,5 +61,5 @@ func main() {
log.Println(string(debug.Stack())) 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 { type EventEmitter struct {
callbacks map[string][]func(args ...interface{}) callbacks map[string]map[int]func(args ...interface{})
onceCallbacks map[string][]func(args ...interface{}) onceCallbacks map[string][]func(args ...interface{})
lock sync.Mutex 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() e.lock.Lock()
defer e.lock.Unlock() defer e.lock.Unlock()
e.counter++
if _, ok := e.callbacks[event]; !ok { if _, ok := e.callbacks[event]; !ok {
e.callbacks[event] = []func(args ...interface{}){callback} e.callbacks[event] = make(map[int]func(args ...interface{}))
} else { }
e.callbacks[event] = append(e.callbacks[event], callback) 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 { func NewEmitter() *EventEmitter {
ret := &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{})) ret.onceCallbacks = make(map[string][]func(args ...interface{}))
return ret return ret
} }

View File

@ -6,18 +6,15 @@ import (
) )
func TestEventEmitter_Emit(t *testing.T) { func TestEventEmitter_Emit(t *testing.T) {
emitter := &EventEmitter{ emitter := NewEmitter()
callbacks: make(map[string][]func(args ...interface{})),
onceCallbacks: make(map[string][]func(args ...interface{})),
}
// 注册一个普通回调函数 // 注册一个普通回调函数
emitter.callbacks["testEvent"] = append(emitter.callbacks["testEvent"], func(data ...interface{}) { emitter.On("testEvent", func(data ...interface{}) {
t.Log("普通回调函数被调用") t.Log("普通回调函数被调用")
}) })
// 注册一个一次性回调函数 // 注册一个一次性回调函数
emitter.onceCallbacks["testEvent"] = append(emitter.onceCallbacks["testEvent"], func(data ...interface{}) { emitter.Once("testEvent", func(data ...interface{}) {
t.Log("一次性回调函数被调用") t.Log("一次性回调函数被调用")
}) })
@ -32,9 +29,7 @@ func TestEventEmitter_Emit(t *testing.T) {
} }
func TestEventEmitter_Once(t *testing.T) { func TestEventEmitter_Once(t *testing.T) {
emitter := &EventEmitter{ emitter := NewEmitter()
onceCallbacks: make(map[string][]func(args ...interface{})),
}
// 测试添加新事件和回调函数 // 测试添加新事件和回调函数
emitter.Once("event1", func(args ...interface{}) {}) emitter.Once("event1", func(args ...interface{}) {})
@ -58,3 +53,16 @@ func TestEventEmitter_Once(t *testing.T) {
t.Error("一次性回调函数没有被清除") 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")
}