This module implements an event system that is not dependent on external graphical toolkits. It was originally called NimEE
because it was inspired by Python's PyEE module. There are two ways you can use events: one is a python-inspired way; the other is more of a C-style way.
var ee = initEventEmitter() var genericargs: EventArgs proc handleevent(e: EventArgs) = echo("Handled!") # Python way ee.on("EventName", handleevent) ee.emit("EventName", genericargs) # C/Java way # Declare a type type SomeObject = object of RootObj SomeEvent: EventHandler var myobj: SomeObject myobj.SomeEvent = initEventHandler("SomeEvent") myobj.SomeEvent.addHandler(handleevent) ee.emit(myobj.SomeEvent, genericargs)
EventArgs = object of RootObj
EventHandler = tuple[name: string, handlers: seq[proc (e: EventArgs) {.closure.}]]
EventEmitter = object s: seq[EventHandler]
EventError = object of ValueError
proc initEventHandler(name: string): EventHandler {.raises: [], tags: [].}
proc addHandler(handler: var EventHandler; fn: proc (e: EventArgs) {.closure.}) {. raises: [], tags: [].}
proc removeHandler(handler: var EventHandler; fn: proc (e: EventArgs) {.closure.}) {. raises: [Exception], tags: [RootEffect].}
proc containsHandler(handler: var EventHandler; fn: proc (e: EventArgs) {.closure.}): bool {. raises: [Exception], tags: [RootEffect].}
proc clearHandlers(handler: var EventHandler) {.raises: [], tags: [].}
proc on(emitter: var EventEmitter; event: string; fn: proc (e: EventArgs) {.closure.}) {. raises: [], tags: [].}
proc emit(emitter: var EventEmitter; eventhandler: var EventHandler; args: EventArgs) {. raises: [Exception], tags: [RootEffect].}
proc emit(emitter: var EventEmitter; event: string; args: EventArgs) {. raises: [Exception], tags: [RootEffect].}
proc initEventEmitter(): EventEmitter {.raises: [], tags: [].}
© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/events.html