Fix having multiple listeners for the same event.

This commit is contained in:
SeanOMik 2020-05-30 21:45:11 -05:00
parent b78d6958ae
commit a1a63f93e2
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
1 changed files with 2 additions and 5 deletions

View File

@ -14,8 +14,7 @@ let eventListeners = newTable[string, seq[proc(event: BaseEvent)]]()
proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent)) = proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent)) =
if (eventListeners.hasKey($event)): if (eventListeners.hasKey($event)):
var listeners = eventListeners[$event] eventListeners[$event].add(cast[proc(event: BaseEvent)](listener))
listeners.add(cast[proc(event: BaseEvent)](listener))
echo "Added other event listener: ", $event echo "Added other event listener: ", $event
else: else:
@ -25,12 +24,10 @@ proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent))
echo "Added new event listener: ", $event echo "Added new event listener: ", $event
proc dispatchEvent*[T: BaseEvent](event: T) = proc dispatchEvent*[T: BaseEvent](event: T) =
#let base: BaseEvent = BaseEvent(event)
if (eventListeners.hasKey(event.name)): if (eventListeners.hasKey(event.name)):
let listeners = eventListeners[event.name] let listeners = eventListeners[event.name]
echo "Dispatching event: ", event.name
for index, eventListener in listeners.pairs: for index, eventListener in listeners.pairs:
echo "Dispatching event: ", event.name
eventListener(event) eventListener(event)
else: else:
echo "No event listeners for event: ", event.name echo "No event listeners for event: ", event.name