gookit / event

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

Custom events failed to execute #68

Open arduanov opened 2 months ago

arduanov commented 2 months ago

System (please complete the following information):

Describe the bug

panic: interface conversion: event.Event is event.BasicEvent, not main.MmyEvent

To Reproduce

package main

import (
    "fmt"

    "github.com/gookit/event"
)

type MyEvent struct {
    event.BasicEvent
    customData string
}

func (e *MyEvent) CustomData() string {
    return e.customData
}

func main() {
    e := &MyEvent{customData: "hello"}
    e.SetName("e1")
    event.AddEvent(e)

    // add listener
    event.On("e1", event.ListenerFunc(func(e event.Event) error {
        fmt.Printf("custom Data: %s\n", e.(*MyEvent).CustomData())
        return nil
    }))

    // trigger
    event.Fire("e1", nil)
}
panic: interface conversion: event.Event is *event.BasicEvent, not *main.MyEvent

goroutine 1 [running]:
main.main.func1({0x102873790?, 0x140001102d0?})
        /Users/m.arduanov/go/src/tt/main.go:25 +0xa4
github.com/gookit/event.ListenerFunc.Handle(0x1400012e048?, {0x102873790?, 0x140001102d0?})
        /Users/m.arduanov/go/pkg/mod/github.com/gookit/event@v1.1.2/listener.go:18 +0x34
github.com/gookit/event.(*Manager).fireSimpleMode(0x14000136000, {0x102828f15, 0x2}, {0x102873790, 0x140001102d0})
        /Users/m.arduanov/go/pkg/mod/github.com/gookit/event@v1.1.2/manager.go:269 +0xb4
github.com/gookit/event.(*Manager).FireEvent(0x14000136000, {0x102873790, 0x140001102d0})
        /Users/m.arduanov/go/pkg/mod/github.com/gookit/event@v1.1.2/manager.go:243 +0x12c
github.com/gookit/event.(*Manager).fireByName(0x14000136000, {0x102828f15?, 0x2?}, 0x0, 0x0)
        /Users/m.arduanov/go/pkg/mod/github.com/gookit/event@v1.1.2/manager.go:221 +0x1d0
github.com/gookit/event.(*Manager).Fire(...)
        /Users/m.arduanov/go/pkg/mod/github.com/gookit/event@v1.1.2/manager.go:173
github.com/gookit/event.Fire(...)
        /Users/m.arduanov/go/pkg/mod/github.com/gookit/event@v1.1.2/std.go:67
main.main()
        /Users/m.arduanov/go/src/tt/main.go:30 +0xd0
exit status 2

Expected behavior

Listener get MyEvent with CustomData()

arduanov commented 2 months ago

but FireEvent works perfect

event.FireEvent(e)