Package sparked :: Module events
[hide private]
[frames] | no frames]

Source Code for Module sparked.events

 1  # Copyright (c) 2010 Arjan Scherpenisse 
 2  # See LICENSE for details. 
 3  # -*- test-case-name: sparked.test.test_events -*- 
 4   
 5  """ 
 6  Classes which define a generic event system. 
 7  """ 
 8   
 9  from twisted.words.xish import utility 
10   
11 -class EventDispatcher(utility.EventDispatcher):
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 """
21 - def __init__(self, eventprefix=""):
22 utility.EventDispatcher.__init__(self, eventprefix)
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 # If this is a dispatch within a dispatch, don't 54 # do anything with the updateQueue -- it needs to 55 # wait until we've back all the way out of the stack 56 if self._dispatchDepth == 0: 57 # Deal with pending update operations 58 for f in self._updateQueue: 59 f() 60 self._updateQueue = [] 61 62 return foundTarget
63