package event // Should be called manager? // Bus can be implemente in other structs type Bus interface { Emitter Receiver } // EventBus implementation type eventBus struct { system *System emitPrefix string handlers map[string]*[]HandlerFunc } // NewBus Creates and Initializes an eventBus /*func NewBus() Bus { return &eventBus{handlers: map[string]*[]HandlerFunc{}} }*/ func (e *eventBus) handleEvent(evt Event) { handlerList, ok := e.handlers[evt.Name()] if !ok { return } for _, handler := range *handlerList { handler(evt) } // Special case catchList, ok := e.handlers["*"] if !ok { return } for _, handler := range *catchList { handler(evt) } } // This should send to system func (e *eventBus) Emit(name string, data interface{}) { var evt Event if e.emitPrefix != "" { evt = New(e.emitPrefix+namedPrefixer+name, data) } else { evt = New(name, data) } e.system.handleEvent(evt) // Forward to system } // Register locally 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) }