gookit / event

📢 Lightweight event manager and dispatcher implements by Go. Go实现的轻量级的事件管理、调度程序库, 支持设置监听器的优先级, 支持使用通配符来进行一组事件的监听
https://pgk.go.dev/github.com/gookit/event
MIT License
508 stars 59 forks source link

RemoveListener not support closure function #9

Closed RelicOfTesla closed 1 year ago

RelicOfTesla commented 3 years ago

func makeFn(a int) event.ListenerFunc {
    return func(e event.Event) error {
        fmt.Println(a)
        return nil
    }
}
func main() {
    evBus := event.NewManager("")
    f1 := makeFn(11)
    evBus.On("evt1", f1)
    f2 := makeFn(22)
    evBus.On("evt1", f2)
    evBus.RemoveListener("evt1", f1) // DON'T REMOVE ALL !!!
    evBus.MustFire("evt1", event.M{"arg0": "val0", "arg1": "val1"})
}
intrntbrn commented 2 years ago

The issue is that "%p" is equal for f1 and f2.

You can work around this issue by using new:

f1 := makeFn(11)
l := new(event.ListenerFunc)
*l = f1
evBus.On("evt1", l)
f2 := makeFn(22)
evBus.On("evt1", f2)