12345678910111213141516171819202122232425262728293031323334353637383940 |
- package event
- // Should be called manager?
- // Bus can be implemente in other sturcts
- type Bus interface {
- Emitter
- Receiver
- }
- // EventBus implementation
- type eventBus struct {
- handlers map[string]*[]HandlerFunc
- }
- // NewBus Creates and Initializes an eventBus
- func NewBus() Bus {
- return &eventBus{handlers: map[string]*[]HandlerFunc{}}
- }
- func (e *eventBus) Emit(evt Event) {
- handlerList, ok := e.handlers[evt.Name()]
- if !ok {
- return
- }
- for _, handler := range *handlerList {
- handler(evt)
- }
- }
- func (e *eventBus) On(name string, handler HandlerFunc) {
- handlerList, ok := e.handlers[name]
- if !ok {
- handlerList = &[]HandlerFunc{}
- e.handlers[name] = handlerList
- }
- *handlerList = append(*handlerList, handler)
- }
|