diff options
author | Amrykid <amrykid@gmail.com> | 2011-07-27 18:04:13 -0500 |
---|---|---|
committer | Amrykid <amrykid@gmail.com> | 2011-07-27 18:04:13 -0500 |
commit | 79dafe08e33b82fb1955c981840ff7b3790c224e (patch) | |
tree | 96e9c68b2392bbdb5ddccc8dd6e3c1a19e5f272f /lib/pure | |
parent | ecd62b955b0a2417506e69bfd898af1b135b8f49 (diff) | |
download | Nim-79dafe08e33b82fb1955c981840ff7b3790c224e.tar.gz |
Renamed new* to init*.
Added example code for documentation. Fixed a small documentation issue. Rewrote newEventEmitter... Updated test.
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/events.nim | 33 |
1 files changed, 24 insertions, 9 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 |