Skip to content

Commit

Permalink
fix: avoid collision of subscriber priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerdweight committed Oct 13, 2023
1 parent 2c162b7 commit 26b7076
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (h *EventHub) Subscribe(subscriber EventSubscriber[Event[EventPayload]]) {
h.subscribers[subscriber.GetKey()][subscriber.GetPriority()] = make([]EventSubscriber[Event[EventPayload]], 0)
}
h.subscribers[subscriber.GetKey()][subscriber.GetPriority()] = append(h.subscribers[subscriber.GetKey()][subscriber.GetPriority()], subscriber)
// only add priority if not yet present to avoid duplicates
for _, priority := range h.priorities {
if priority == subscriber.GetPriority() {
h.lock.Unlock()
return
}
}
h.priorities = append(h.priorities, subscriber.GetPriority())
sort.Ints(h.priorities)
h.lock.Unlock()
Expand Down

0 comments on commit 26b7076

Please sign in to comment.