Start on an event handler. It may work or not 🤷

This commit is contained in:
SeanOMik 2020-05-29 01:21:02 -05:00
parent 0b11d2f5e0
commit edbde752af
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
1 changed files with 27 additions and 0 deletions

27
src/event_handler.nim Normal file
View File

@ -0,0 +1,27 @@
import tables, hashes, sequtils
type
BaseEvent* = object
name*: string
proc hash[T: object](o: T): Hash =
for k, v in o.fieldPairs:
result = result !& v.hash
result = !$result
# Table storing all the event listeners
let eventListeners = newTable[BaseEvent, seq[proc()]]()
proc registerEventListener*(event: BaseEvent, listener: proc()) =
if (eventListeners.hasKey(event)):
var listeners = eventListeners[event]
listeners.add(listener)
else:
let tmp = @[listener]
eventListeners.add(event, tmp)
proc dispatchEvent(event: BaseEvent) =
if (eventListeners.hasKey(event)):
let listeners = eventListeners[event]
for index, eventListener in listeners.pairs:
eventListener()