Browse Source

added bboard removal tracking

Darren Ranalli 19 years ago
parent
commit
6c57da8086
2 changed files with 27 additions and 17 deletions
  1. 4 0
      direct/src/showbase/BulletinBoard.py
  2. 23 17
      direct/src/showbase/BulletinBoardWatcher.py

+ 4 - 0
direct/src/showbase/BulletinBoard.py

@@ -19,6 +19,9 @@ class BulletinBoard:
     def getEvent(self, postName):
     def getEvent(self, postName):
         return 'bboard-%s' % postName
         return 'bboard-%s' % postName
 
 
+    def getRemoveEvent(self, postName):
+        return 'bboard-remove-%s' % postName
+
     def post(self, postName, value=None):
     def post(self, postName, value=None):
         if postName in self._dict:
         if postName in self._dict:
             BulletinBoard.notify.warning('changing %s from %s to %s' % (
             BulletinBoard.notify.warning('changing %s from %s to %s' % (
@@ -35,6 +38,7 @@ class BulletinBoard:
     def remove(self, postName):
     def remove(self, postName):
         if postName in self._dict:
         if postName in self._dict:
             del self._dict[postName]
             del self._dict[postName]
+            messenger.send(self.getRemoveEvent(postName))
 
 
     def removeIfEqual(self, postName, value):
     def removeIfEqual(self, postName, value):
         # only remove the post if its value is a particular value
         # only remove the post if its value is a particular value

+ 23 - 17
direct/src/showbase/BulletinBoardWatcher.py

@@ -1,27 +1,33 @@
 from direct.directnotify import DirectNotifyGlobal
 from direct.directnotify import DirectNotifyGlobal
-from direct.showbase.PythonUtil import Functor
+from direct.showbase.PythonUtil import Functor, makeList
 from direct.showbase import DirectObject
 from direct.showbase import DirectObject
 
 
 class BulletinBoardWatcher(DirectObject.DirectObject):
 class BulletinBoardWatcher(DirectObject.DirectObject):
-    """This class allows you to wait for a set of posts to be made to the
-    bulletin board, and give you a notification when all posts have been
-    made. Values of posts are not examined."""
+    """ This class allows you to wait for a set of posts to be made to (or
+    removed from) the bulletin board, and gives you a notification when all
+    posts have been made. Values of posts are not examined."""
     notify = DirectNotifyGlobal.directNotify.newCategory('BulletinBoardWatcher')
     notify = DirectNotifyGlobal.directNotify.newCategory('BulletinBoardWatcher')
 
 
-    def __init__(self, name, postNames, callback):
+    def __init__(self, name, postNames, callback, removeNames=None):
         self.notify.debug('__init__: %s, %s, %s' % (name, postNames, callback))
         self.notify.debug('__init__: %s, %s, %s' % (name, postNames, callback))
+        if removeNames is None:
+            removeNames = []
         self.name = name
         self.name = name
-        if type(postNames) == type(''):
-            postNames = [postNames]
-        self.postNames = postNames
+        self.postNames = makeList(postNames)
+        self.removeNames = makeList(removeNames)
         self.callback = callback
         self.callback = callback
-        self.waitingOn = {}
-        for name in postNames:
+        self.waitingOn = set()
+        for name in self.postNames:
             if not bboard.has(name):
             if not bboard.has(name):
                 eventName = bboard.getEvent(name)
                 eventName = bboard.getEvent(name)
-                self.acceptOnce(eventName, Functor(self.handlePost, eventName))
-                self.waitingOn[eventName] = None
-        self.checkDone()
+                self.waitingOn.add(eventName)
+                self.acceptOnce(eventName, Functor(self._handleEvent, eventName))
+        for name in self.removeNames:
+            if bboard.has(name):
+                eventName = bboard.getRemoveEvent(name)
+                self.waitingOn.add(eventName)
+                self.acceptOnce(eventName, Functor(self._handleEvent, eventName))
+        self._checkDone()
 
 
     def destroy(self):
     def destroy(self):
         self.ignoreAll()
         self.ignoreAll()
@@ -31,13 +37,13 @@ class BulletinBoardWatcher(DirectObject.DirectObject):
     def isDone(self):
     def isDone(self):
         return len(self.waitingOn) == 0
         return len(self.waitingOn) == 0
 
 
-    def checkDone(self):
+    def _checkDone(self):
         if self.isDone():
         if self.isDone():
             self.notify.debug('%s: done' % self.name)
             self.notify.debug('%s: done' % self.name)
             self.callback()
             self.callback()
 
 
-    def handlePost(self, eventName):
+    def _handleEvent(self, eventName):
         self.notify.debug('%s: handlePost(%s)' % (self.name, eventName))
         self.notify.debug('%s: handlePost(%s)' % (self.name, eventName))
         assert eventName in self.waitingOn
         assert eventName in self.waitingOn
-        del self.waitingOn[eventName]
-        self.checkDone()
+        self.waitingOn.remove(eventName)
+        self._checkDone()