summary refs log tree commit diff stats
path: root/lib/pure/events.nim
blob: 413256657ec094029b957781b717c26cef55e9e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
#
#            Nimrod's Runtime Library
#        (c) Copyright 2011 Alex Mitchell
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## :Author: Alex Mitchell
##
## 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!")
##
##   # 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.
  TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] ## An eventhandler for an event.

type
  TEventEmitter* = object {.pure, final.} ## An object that fires events and holds event handlers for an object.
    s: seq[TEventHandler]
  EInvalidEvent* = object of EInvalidValue
    
proc initEventHandler*(name: string): TEventHandler =
  ## Initializes an EventHandler with the specified name and returns it.
  #new(result)
  result.handlers = @[]
  result.name = name

proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) =
  ## Adds the callback to the specified event handler.
  handler.handlers.add(func)

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: var TEventHandler) =
  ## Clears all of the callbacks from the event handler.
  setLen(handler.handlers, 0)

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: 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)
  if i < 0:
    var eh = newEventHandler(event)
    addHandler(eh, func)
    emitter.s.add(eh)
  else:
    addHandler(emitter.s[i], func)
  
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: var TEventEmitter, event: string, args: TEventArgs) =
  ## Fires an event handler with specified event arguments.
  var i = getEventHandler(emitter, event)
  if i >= 0:
    emit(emitter, emitter.s[i], args)
  else:
    raise newException(EInvalidEvent, "invalid event: " & event)

proc initEventEmitter*(): TEventEmitter =
  ## Creates and returns a new EventEmitter.
  #new(result)
  result.s = @[]