Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created August 22, 2018 07:44
Show Gist options
  • Save copygirl/1015e0c636e961cace19622ebf0f2188 to your computer and use it in GitHub Desktop.
Save copygirl/1015e0c636e961cace19622ebf0f2188 to your computer and use it in GitHub Desktop.
import macros
type
EventHandler*[T] = proc(event: T)
Event*[T] = distinct seq[EventHandler[T]]
proc newEvent*[T](): Event[T] =
result = newSeq[EventHandler[T]]()
proc subscribe*[T](ev: Event[T], handler: EventHandler[T]) =
ev.add(handler)
proc unsubscribe*[T](ev: Event[T], handler: EventHandler[T]) =
ev.del(handler)
proc fire*[T](ev: Event[T], event: T) =
for handler in ev: handler(event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment