YouChuKoffee/event/EventManger.go

35 lines
660 B
Go
Raw Normal View History

2024-06-17 14:18:39 +08:00
package event
import "qteam/app/utils"
type EventHandler interface {
Handle(param interface{})
}
type EventManagerFactory struct {
handers map[string][]EventHandler
}
func (this *EventManagerFactory) Register(eventName string, handler EventHandler) {
if this.handers == nil {
this.handers = make(map[string][]EventHandler)
}
this.handers[eventName] = append(this.handers[eventName], handler)
}
func (this *EventManagerFactory) TrigerEvent(eventName string, param interface{}) {
defer func() {
err := recover()
if err != nil {
utils.Log(nil, "event err", err)
}
}()
for _, v := range this.handers[eventName] {
v.Handle(param)
}
}