1
2
3
4
5 """
6 Classes which define a generic event system.
7 """
8
9 from twisted.words.xish import utility
10
12 """
13 The sparked event dispatcher is simpler than the twisted version:
14 it does not use XPath arguments and its event prefix is always
15 empty.
16
17 This class exists to simplify the implementation of event
18 dispatchers in sparked without the syntactic sugar of xish'
19 EventDispatcher class.
20 """
23
24
25 - def dispatch(self, event, *arg, **kwarg):
26 """
27 Dispatch the named event to all the callbacks.
28 """
29 foundTarget = False
30
31 self._dispatchDepth += 1
32
33 observers = self._eventObservers
34
35 priorities = observers.keys()
36 priorities.sort()
37 priorities.reverse()
38
39 emptyLists = []
40 for priority in priorities:
41 for query, callbacklist in observers[priority].iteritems():
42 if query == event:
43 callbacklist.callback(*arg, **kwarg)
44 foundTarget = True
45 if callbacklist.isEmpty():
46 emptyLists.append((priority, query))
47
48 for priority, query in emptyLists:
49 del observers[priority][query]
50
51 self._dispatchDepth -= 1
52
53
54
55
56 if self._dispatchDepth == 0:
57
58 for f in self._updateQueue:
59 f()
60 self._updateQueue = []
61
62 return foundTarget
63