Skip to content

Commit

Permalink
feat: remove multiple listeners on event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdulsametileri committed Nov 2, 2023
1 parent 13d10c4 commit 320f2c9
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions helpers/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ type Bus interface {
}

type bus struct {
listeners map[string][]Listener
listeners map[string]Listener
}

func (i *bus) Emit(eventName string, event interface{}) {
if listeners, ok := i.listeners[eventName]; ok {
for _, listener := range listeners {
listener(event)
}
if listener, ok := i.listeners[eventName]; ok {
listener(event)
}
}

func (i *bus) Subscribe(eventName string, listener Listener) {
if listeners, ok := i.listeners[eventName]; ok {
i.listeners[eventName] = append(listeners, listener)
} else {
i.listeners[eventName] = []Listener{listener}
}
i.listeners[eventName] = listener
}

func NewBus() Bus {
return &bus{
listeners: map[string][]Listener{},
listeners: map[string]Listener{},
}
}

0 comments on commit 320f2c9

Please sign in to comment.