Created
August 22, 2018 06:59
-
-
Save copygirl/e73e6b07d7aef8238c39eedc52428d9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import macros | |
type | |
EventHandler*[T] = proc(event: T) | |
EventStore[T] = seq[EventHandler[T]] | |
proc getEventStore[T](): var EventStore[T] = | |
static var store = newSeq[T]() | |
result = store | |
proc subscribe*[T](self: typedesc[T], handler: EventHandler) = | |
var store = getEventStore[T]() | |
store.add(handler) | |
proc unsubscribe*[T](self: typedesc[T], handler: EventHandler) = | |
var store = getEventStore[T]() | |
store.del(handler) | |
proc fire*[T](event: T) = | |
var store = getEventStore[T]() | |
for handler in store: handler(event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
signature should be
proc subscribe*[T](self: typedesc[T], handler: EventHandler[T]) =
I think, you might have cannot instantiate T otherwise.