12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 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
- 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(evt Event) {
- 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)
- }
|