Commits

pypyja committed 57f5174

improvement of ...

Comments (0)

Files changed (3)

src/r12a/events/events.py

         key = listenerObj.key
         proxy.key = key
 
-        listenerArray.append(listenerObj)
+        listenerArray[len(listenerArray.items())] = listenerObj
         r12a.events.listeners_[key] = listenerObj
 
         if not srcUid in r12a.events.sources_:
             r12a.events.sources_[srcUid] = r12a.events.pools.getArray()
-        r12a.events.sources_[srcUid].append(listenerObj)
+        r12a.events.sources_[srcUid][len(r12a.events.sources_[srcUid].items())] = listenerObj
 
         return key
 
     return r12a.events.onStringMap_[type]
 
 
+def fireListeners(obj, type, capture=None, eventObject=None):
+    u"""
+    Fires an object's listeners of a particular type and phase
+    """
+
+    map = r12a.events.listenerTree_
+
+
+def fireListeners_(map, obj, type, capture=None, eventObject=None):
+    retval = 1
+
+    objUid = r12a.getUid(obj)
+    if objUid in map:
+        map['remaining_'] = map['remaining_'] - 1
+        listenerArray = map[objUid]
+
+        # If locked_ is not set (and if already 0) initialize it to 1.
+        if not 'locked_' in listenerArray:
+            listenerArray['locked_'] = 1
+        else:
+            listenerArray['locked_'] = listenerArray['locked_'] + 1
+
+    # Events added in the dispatch phase should not be dispatched in
+    # the current dispatch phase. They will be included in the next
+    # dispatch phase though.
+
+    for key in listenerArray:
+        if key == 'locked_':
+            pass
+        else:
+            listener = listenerArray[key]
+            # We might not have a listener if the listener was removed.
+            if listener and not listener.removed:
+                retval &= not r12a.events.fireListener(listener, eventObject) == False
+
+    return bool(retval)
+
+
+def fireListener(listener, eventObject):
+    u"""
+    Fires a listener with a set of arguments
+    """
+    rv = listener.handleEvent(eventObject)
+
+    return rv
+
+
 def dispatchEvent(src, e):
     u"""
     Dispatches an event (or event like object) and calls all listeners
             ancestors.append(parent)
             parent = parent.getParentEventTarget()
 
+        targetsMap = map['True']
+        targetsMap['remaining_'] = targetsMap['count_']
+
+        # Call capture listeners
+        while True:
+            i = len(ancestors) - 1
+            if not e.propagationStopped_ and \
+                    i >= 0 and targetsMap['remaining_']:
+                e.currentTarget = ancestors[i]
+
+                rv &= r12a.events.fireListeners_(targetsMap, ancestors[i], e.type) and \
+                    not e.returnValue_ == False
+            else:
+                break
+    else:
+        pass
         # TODO: xxx
 
     hasBubble = 'False' in map

src/r12a/events/pools.py

                 'remaining_': 0}
 
     def getArray_():
-        return []
+        #return []
+        return {}
 
     # This gets set to {@code r12a.events.handleBrowserEvent_} by events.js.
     proxyCallbackFunction_ = None

src/test_eventtarget.py

 stopPropagation = False
 
 
-def func1(e):
+def func1(self, e):
     print('func1')
     if preventDefault:
         e.preventDefault()
         e.stopPropagation()
 
 
-def func2(e):
+def func2(self, e):
     print('func2')
 
 
     def __init__(self):
         pass
 
-    def DoSomething():
+    def DoSomething(self):
         print('Doing something')
         return self.dispatchEvent(DemoEventObject('Wooo'))
 
 r12a.events.listen(st, 'synth', func1, True)
 r12a.events.listen(st, 'synth', func2, False)
 r12a.events.dispatchEvent(st, 'synth')
+r12a.events.dispatchEvent(st, 'synth')