W3cubDocs

/Nim

Module events

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)

Types

EventArgs = object of RootObj
Base object for event arguments that are passed to callback functions.
EventHandler = tuple[name: string, handlers: seq[proc (e: EventArgs) {.closure.}]]
An eventhandler for an event.
EventEmitter = object
  s: seq[EventHandler]
An object that fires events and holds event handlers for an object.
EventError = object of ValueError

Procs

proc initEventHandler(name: string): EventHandler {.raises: [], tags: [].}
Initializes an EventHandler with the specified name and returns it.
proc addHandler(handler: var EventHandler; fn: proc (e: EventArgs) {.closure.}) {.
    raises: [], tags: [].}
Adds the callback to the specified event handler.
proc removeHandler(handler: var EventHandler; fn: proc (e: EventArgs) {.closure.}) {.
    raises: [Exception], tags: [RootEffect].}
Removes the callback from the specified event handler.
proc containsHandler(handler: var EventHandler; fn: proc (e: EventArgs) {.closure.}): bool {.
    raises: [Exception], tags: [RootEffect].}
Checks if a callback is registered to this event handler.
proc clearHandlers(handler: var EventHandler) {.raises: [], tags: [].}
Clears all of the callbacks from the event handler.
proc on(emitter: var EventEmitter; event: string; fn: proc (e: EventArgs) {.closure.}) {.
    raises: [], tags: [].}
Assigns a event handler with the specified callback. If the event doesn't exist, it will be created.
proc emit(emitter: var EventEmitter; eventhandler: var EventHandler; args: EventArgs) {.
    raises: [Exception], tags: [RootEffect].}
Fires an event handler with specified event arguments.
proc emit(emitter: var EventEmitter; event: string; args: EventArgs) {.
    raises: [Exception], tags: [RootEffect].}
Fires an event handler with specified event arguments.
proc initEventEmitter(): EventEmitter {.raises: [], tags: [].}
Creates and returns a new EventEmitter.

© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/events.html