add eachCallback

This commit is contained in:
David Rose 2003-11-20 16:16:11 +00:00
parent 4128df5a1e
commit 5d501f2f5f

View File

@ -40,21 +40,25 @@ class RelatedObjectMgr(DirectObject.DirectObject):
del self.pendingObjects del self.pendingObjects
self.ignoreAll() self.ignoreAll()
def requestObjects(self, doIdList, callback, def requestObjects(self, doIdList, allCallback = None, eachCallback = None,
timeout = None, timeoutCallback = None): timeout = None, timeoutCallback = None):
""" """
Requests the indicated callback to be called when all the Requests a callback to be called when the objects in the
objects in the doIdList are generated. If all the objects doIdList are generated. The allCallback will be called only
already exist, the callback is called immediately. In either when all the objects have been generated (and it receives a
case, the list of objects, in the order given in doIdList, is list of objects, in the order given in doIdList). The
passed to the callback. eachCallback is called as each object is generated, and
receives only the object itself.
If the objects already exist, the appropriate callback is
called immediately.
If all of the objects are not generated within the indicated If all of the objects are not generated within the indicated
timeout time, the timeoutCallback is called instead, with the timeout time, the timeoutCallback is called instead, with the
original doIdList as the parameter. If the timeoutCallback is original doIdList as the parameter. If the timeoutCallback is
None, then the original callback is called on timeout, with None, then allCallback is called on timeout, with the list of
the list of objects that have been generated so far, and None objects that have been generated so far, and None for objects
for objects that have not been generated. that have not been generated.
If any element of doIdList is None or 0, it is ignored, and If any element of doIdList is None or 0, it is ignored, and
None is passed in its place in the object list passed to the None is passed in its place in the object list passed to the
@ -69,11 +73,20 @@ class RelatedObjectMgr(DirectObject.DirectObject):
# First, see if we have all of the objects already. # First, see if we have all of the objects already.
objects, doIdsPending = self.__generateObjectList(doIdList) objects, doIdsPending = self.__generateObjectList(doIdList)
# Call the eachCallback immediately on any objects we already
# have.
if eachCallback:
for object in objects:
if object:
eachCallback(object)
if len(doIdsPending) == 0: if len(doIdsPending) == 0:
# All the objects exist, so just call the callback # All the objects exist, so just call the callback
# immediately. # immediately.
assert(self.notify.debug("All objects already exist.")) assert(self.notify.debug("All objects already exist."))
callback(objects) if allCallback:
allCallback(objects)
return return
# Some objects don't exist yet, so start listening for them, and # Some objects don't exist yet, so start listening for them, and
@ -92,7 +105,8 @@ class RelatedObjectMgr(DirectObject.DirectObject):
RelatedObjectMgr.doLaterSequence += 1 RelatedObjectMgr.doLaterSequence += 1
tuple = (callback, timeoutCallback, doIdsPending, doIdList, doLaterName) tuple = (allCallback, eachCallback, timeoutCallback,
doIdsPending, doIdList, doLaterName)
for doId in doIdsPending: for doId in doIdsPending:
pendingList = self.pendingObjects.get(doId) pendingList = self.pendingObjects.get(doId)
@ -117,7 +131,7 @@ class RelatedObjectMgr(DirectObject.DirectObject):
is removed from the queue and no further callbacks will be called. is removed from the queue and no further callbacks will be called.
""" """
if tuple: if tuple:
callback, timeoutCallback, doIdsPending, doIdList, doLaterName = tuple allCallback, eachCallback, timeoutCallback, doIdsPending, doIdList, doLaterName = tuple
assert(self.notify.debug("aborting request for %s (remaining: %s)" % (doIdList, doIdsPending))) assert(self.notify.debug("aborting request for %s (remaining: %s)" % (doIdList, doIdsPending)))
if doLaterName: if doLaterName:
@ -125,7 +139,7 @@ class RelatedObjectMgr(DirectObject.DirectObject):
self.__removePending(tuple, doIdsPending) self.__removePending(tuple, doIdsPending)
def __timeoutExpired(self, tuple): def __timeoutExpired(self, tuple):
callback, timeoutCallback, doIdsPending, doIdList, doLaterName = tuple allCallback, eachCallback, timeoutCallback, doIdsPending, doIdList, doLaterName = tuple
assert(self.notify.debug("timeout expired for %s (remaining: %s)" % (doIdList, doIdsPending))) assert(self.notify.debug("timeout expired for %s (remaining: %s)" % (doIdList, doIdsPending)))
self.__removePending(tuple, doIdsPending) self.__removePending(tuple, doIdsPending)
@ -134,7 +148,8 @@ class RelatedObjectMgr(DirectObject.DirectObject):
timeoutCallback(doIdList) timeoutCallback(doIdList)
else: else:
objects, doIdsPending = self.__generateObjectList(doIdList) objects, doIdsPending = self.__generateObjectList(doIdList)
callback(objects) if allCallback:
allCallback(objects)
def __removePending(self, tuple, doIdsPending): def __removePending(self, tuple, doIdsPending):
# Removes all the pending events for the doIdsPending list. # Removes all the pending events for the doIdsPending list.
@ -170,7 +185,7 @@ class RelatedObjectMgr(DirectObject.DirectObject):
del self.pendingObjects[doId] del self.pendingObjects[doId]
for tuple in pendingList: for tuple in pendingList:
callback, timeoutCallback, doIdsPending, doIdList, doLaterName = tuple allCallback, eachCallback, timeoutCallback, doIdsPending, doIdList, doLaterName = tuple
# Here we are depending on Python to unify this one list # Here we are depending on Python to unify this one list
# across all objects that share it. When we remove our # across all objects that share it. When we remove our
@ -178,15 +193,19 @@ class RelatedObjectMgr(DirectObject.DirectObject):
# from all the other references. # from all the other references.
doIdsPending.remove(doId) doIdsPending.remove(doId)
if eachCallback:
eachCallback(object)
if len(doIdsPending) == 0: if len(doIdsPending) == 0:
# That was the last doId on the list. Call the # That was the last doId on the list. Call the
# callback! # allCallback!
assert(self.notify.debug("All objects generated on list: %s" % (doIdList))) assert(self.notify.debug("All objects generated on list: %s" % (doIdList)))
if doLaterName: if doLaterName:
taskMgr.remove(doLaterName) taskMgr.remove(doLaterName)
objects, doIdsPending = self.__generateObjectList(doIdList) objects, doIdsPending = self.__generateObjectList(doIdList)
callback(objects) if allCallback:
allCallback(objects)
else: else:
assert(self.notify.debug("Objects still pending: %s" % (doIdsPending))) assert(self.notify.debug("Objects still pending: %s" % (doIdsPending)))
@ -205,3 +224,4 @@ class RelatedObjectMgr(DirectObject.DirectObject):
objects.append(None) objects.append(None)
return objects, doIdsPending return objects, doIdsPending