2020-05-31 05:09:30 +00:00
|
|
|
import tables, hashes, json, message
|
2020-05-30 04:59:23 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
EventType* = enum
|
|
|
|
evtReady = "READY"
|
2020-05-31 05:09:30 +00:00
|
|
|
evtMessageCreate = "MESSAGE_CREATE"
|
2020-05-30 04:59:23 +00:00
|
|
|
|
|
|
|
BaseEvent* = object of RootObj
|
|
|
|
name*: string
|
|
|
|
ReadyEvent* = object of BaseEvent
|
|
|
|
readyPayload*: JsonNode
|
2020-05-31 05:09:30 +00:00
|
|
|
MessageCreateEvent* = object of BaseEvent
|
|
|
|
message*: Message
|
2020-05-30 04:59:23 +00:00
|
|
|
|
|
|
|
# Table storing all the event listeners
|
|
|
|
let eventListeners = newTable[string, seq[proc(event: BaseEvent)]]()
|
|
|
|
|
|
|
|
proc registerEventListener*(event: EventType, listener: proc(event: BaseEvent)) =
|
|
|
|
if (eventListeners.hasKey($event)):
|
2020-05-31 02:45:11 +00:00
|
|
|
eventListeners[$event].add(cast[proc(event: BaseEvent)](listener))
|
2020-05-30 04:59:23 +00:00
|
|
|
|
|
|
|
echo "Added other event listener: ", $event
|
|
|
|
else:
|
|
|
|
let tmp = @[listener]
|
|
|
|
eventListeners.add($event, tmp)
|
|
|
|
|
|
|
|
echo "Added new event listener: ", $event
|
|
|
|
|
|
|
|
proc dispatchEvent*[T: BaseEvent](event: T) =
|
|
|
|
if (eventListeners.hasKey(event.name)):
|
|
|
|
let listeners = eventListeners[event.name]
|
2020-05-31 02:45:11 +00:00
|
|
|
echo "Dispatching event: ", event.name
|
2020-05-30 04:59:23 +00:00
|
|
|
for index, eventListener in listeners.pairs:
|
|
|
|
eventListener(event)
|
|
|
|
else:
|
|
|
|
echo "No event listeners for event: ", event.name
|