Permalink
Browse files

Merge pull request #83 from Amrykid/76c5a26510e7d5d735475635c8023589c…

…fb1296c

events.nim updates
  • Loading branch information...
2 parents 0ec0e86 + 76c5a26 commit 0332f0c8acbb4fb2a9213225f69204e976bd4909 @Araq Araq committed Dec 29, 2011
Showing with 23 additions and 16 deletions.
  1. +23 −16 lib/pure/events.nim
View
@@ -12,25 +12,27 @@
## This module implements an event system that is not dependant 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.
+##
## .. code-block:: Nimrod
-## var ee = initEventEmitter()
-## var genericargs: TEventArgs
-## proc handleevent(e: TEventArgs) =
-## echo("Handled!")
+## var ee = initEventEmitter()
+## var genericargs: TEventArgs
+## proc handleevent(e: TEventArgs) =
+## echo("Handled!")
##
-## # Python way
-## ee.on("EventName", handleevent)
-## ee.emit("EventName", genericargs)
+## # 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)
+## # 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.
TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] ## An eventhandler for an event.
@@ -57,6 +59,11 @@ proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) =
handler.handlers.del(i)
break
+proc containsHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)): bool =
+ ## Checks if a callback is registered to this event handler.
+ return handler.handlers.contains(func)
+
+
proc clearHandlers*(handler: var TEventHandler) =
## Clears all of the callbacks from the event handler.
setLen(handler.handlers, 0)

0 comments on commit 0332f0c

Please sign in to comment.