You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bog/util/event.go

42 lines
759 B
Go

package util
3 years ago
type Event struct {
eventName string
payload interface{}
3 years ago
}
func NewEvent(eventName string, payload interface{}) *Event {
3 years ago
return &Event{
eventName,
payload,
}
}
func (e *Event) EventName() string {
return e.eventName
}
func (e *Event) Payload() interface{} {
return e.payload
}
type EventHandler func(payload interface{})
3 years ago
type EventBus struct {
handlers map[string][]EventHandler
}
func NewEventBus() *EventBus {
return &EventBus{make(map[string][]EventHandler)}
3 years ago
}
func (eb *EventBus) Register(eventName string, handler EventHandler) {
eb.handlers[eventName] = append(eb.handlers[eventName], handler)
}
func (eb *EventBus) Handle(e Event) {
3 years ago
for _, handler := range eb.handlers[e.EventName()] {
handler(e.Payload())
3 years ago
}
}