diff options
-rw-r--r-- | lib/pure/events.nim | 33 | ||||
-rw-r--r-- | tests/accept/run/tevents.nim | 4 |
2 files changed, 26 insertions, 11 deletions
diff --git a/lib/pure/events.nim b/lib/pure/events.nim index 30ab324d6..d054dd217 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -11,11 +11,28 @@ ## ## This module implements an event system that is not dependant on external ## graphical toolkits. It was originally called ``NimEE`` because -## it was inspired by Ptyhon's PyEE module. - +## 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. +## .. code-block:: Nimrod +## var ee = initEventEmitter() +## var genericargs : TEventArgs +## proc handleevent(e : TEventArgs) = +## echo("Handled!") +## +## # Python way +## ee.on("EventName", handleevent) +## ee.emit("EventName", genericargs) +## +## # C/Java way +## # Declare a type +## type +## TSomeObject = object of TObject +## SomeEvent : TEventHandler +## var myobj : TSomeObject +## myobj.SomeEvent = initEventHandler("SomeEvent") +## myobj.SomeEvent.addHandler(handleevent) +## ee.emit(myobj.SomeEvent, genericargs) type - TEventArgs* = object of TObject ## Base object for event arguments - ## that are passed to callback functions. + TEventArgs* = object of TObject ## Base object for event arguments that are passed to callback functions. TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] ## An eventhandler for an event. type @@ -23,7 +40,7 @@ type s: seq[TEventHandler] EInvalidEvent* = object of EInvalidValue -proc newEventHandler*(name: string): TEventHandler = +proc initEventHandler*(name: string): TEventHandler = ## Initializes an EventHandler with the specified name and returns it. #new(result) result.handlers = @[] @@ -73,9 +90,7 @@ proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) = else: raise newException(EInvalidEvent, "invalid event: " & event) -proc newEventEmitter*(): TEventEmitter = +proc initEventEmitter*(): TEventEmitter = ## Creates and returns a new EventEmitter. #new(result) - var result : TEventEmitter - result.s = @[] - return result \ No newline at end of file + result.s = @[] \ No newline at end of file diff --git a/tests/accept/run/tevents.nim b/tests/accept/run/tevents.nim index a53ae7ac8..fb94b1f79 100644 --- a/tests/accept/run/tevents.nim +++ b/tests/accept/run/tevents.nim @@ -18,7 +18,7 @@ proc handleprintevent2*(e: TEventArgs) = var args: TPrintEventArgs = TPrintEventArgs(e) write(stdout, "HandlePrintEvent2: Output -> printing for " & args.user) -var ee = newEventEmitter() +var ee = initEventEmitter() var eventargs: TPrintEventArgs eventargs.user = "ME\n" @@ -37,7 +37,7 @@ type PrintEvent: TEventHandler var obj: TSomeObject -obj.PrintEvent = newEventHandler("print") +obj.PrintEvent = initEventHandler("print") obj.PrintEvent.addHandler(handleprintevent2) ee.emit(obj.PrintEvent, eventargs) |