diff options
Diffstat (limited to 'lib/pure/events.nim')
-rw-r--r-- | lib/pure/events.nim | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/lib/pure/events.nim b/lib/pure/events.nim index 00d6797d2..8ba637f9e 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -16,43 +16,42 @@ 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)]] - PEventHandler* = ref TEventHandler ## An eventhandler for an event. + TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] + #PEventHandler* = ref TEventHandler ## An eventhandler for an event. type - TEventEmitter = object {.pure, final.} - s: seq[PEventHandler] - PEventEmitter* = ref TEventEmitter ## An object that fires events and - ## holds event handlers for an object. + TEventEmitter* = object {.pure, final.} + s: seq[TEventHandler] + #PEventEmitter* = ref TEventEmitter ## An object that fires events and holds event handlers for an object. EInvalidEvent* = object of EInvalidValue -proc newEventHandler*(name: string): PEventHandler = +proc newEventHandler*(name: string): TEventHandler = ## Initializes an EventHandler with the specified name and returns it. - new(result) + #new(result) result.handlers = @[] result.name = name -proc addHandler*(handler: PEventHandler, func: proc(e: TEventArgs)) = +proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) = ## Adds the callback to the specified event handler. handler.handlers.add(func) -proc removeHandler*(handler: PEventHandler, func: proc(e: TEventArgs)) = +proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) = ## Removes the callback from the specified event handler. for i in countup(0, len(handler.handlers) -1): if func == handler.handlers[i]: handler.handlers.del(i) break -proc clearHandlers*(handler: PEventHandler) = +proc clearHandlers*(handler: var TEventHandler) = ## Clears all of the callbacks from the event handler. setLen(handler.handlers, 0) -proc getEventhandler(emitter: PEventEmitter, event: string): int = +proc getEventhandler(emitter: var TEventEmitter, event: string): int = for k in 0..high(emitter.s): if emitter.s[k].name == event: return k return -1 -proc on*(emitter: PEventEmitter, event: string, func: proc(e: TEventArgs)) = +proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) = ## Assigns a event handler with the specified callback. If the event ## doesn't exist, it will be created. var i = getEventHandler(emitter, event) @@ -63,12 +62,12 @@ proc on*(emitter: PEventEmitter, event: string, func: proc(e: TEventArgs)) = else: addHandler(emitter.s[i], func) -proc emit*(emitter: PEventEmitter, eventhandler: PEventHandler, +proc emit*(emitter: var TEventEmitter, eventhandler: var TEventHandler, args: TEventArgs) = ## Fires an event handler with specified event arguments. for func in items(eventhandler.handlers): func(args) -proc emit*(emitter: PEventEmitter, event: string, args: TEventArgs) = +proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) = ## Fires an event handler with specified event arguments. var i = getEventHandler(emitter, event) if i >= 0: @@ -76,8 +75,9 @@ proc emit*(emitter: PEventEmitter, event: string, args: TEventArgs) = else: raise newException(EInvalidEvent, "invalid event: " & event) -proc newEventEmitter*(): PEventEmitter = +proc newEventEmitter*(): TEventEmitter = ## Creates and returns a new EventEmitter. - new(result) + #new(result) + var result : TEventEmitter result.s = @[] - + return result \ No newline at end of file |