Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created August 22, 2018 06:59
Show Gist options
  • Save copygirl/e73e6b07d7aef8238c39eedc52428d9a to your computer and use it in GitHub Desktop.
Save copygirl/e73e6b07d7aef8238c39eedc52428d9a to your computer and use it in GitHub Desktop.
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)
@mratsim
Copy link

mratsim commented Aug 22, 2018

signature should be proc subscribe*[T](self: typedesc[T], handler: EventHandler[T]) = I think, you might have cannot instantiate T otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment