Commits

Virgil Dupras committed d8560bb

notify: Added Listener.bind_messages()

Comments (0)

Files changed (2)

 # which should be included with this package. The terms are also available at 
 # http://www.hardcoded.net/licenses/bsd_license
 
+from collections import defaultdict
+
 class Broadcaster(object):
     def __init__(self):
         self.listeners = set()
 class Listener(object):
     def __init__(self, broadcaster):
         self.broadcaster = broadcaster
+        self._bound_notifications = defaultdict(list)
+    
+    def bind_messages(self, messages, func):
+        """Binds multiple message to the same function.
+        
+        Often, we perform the same thing on multiple messages. Instead of having the same function
+        repeated again and agin in our class, we can use this method to bind multiple messages to
+        the same function.
+        """
+        for message in messages:
+            self._bound_notifications[message].append(func)
     
     def connect(self):
         self.broadcaster.add_listener(self)
         self.broadcaster.remove_listener(self)
     
     def dispatch(self, msg):
-        method = getattr(self, msg)
-        method()
+        if msg in self._bound_notifications:
+            for func in self._bound_notifications[msg]:
+                func()
+        if hasattr(self, msg):
+            method = getattr(self, msg)
+            method()
     
 
 class Repeater(Broadcaster, Listener):
             self.notify(msg)
     
     def dispatch(self, msg):
-        if hasattr(self, msg):
-            Listener.dispatch(self, msg)
+        Listener.dispatch(self, msg)
         self._repeat_message(msg)
     

tests/notify_test.py

     r.connect()
     l.connect()
     b.notify('hello') # no crash
-    eq_(l.hello_count, 1)
+    eq_(l.hello_count, 1)
+
+def test_bind_messages():
+    b, l = create_pair()
+    l.bind_messages({'foo', 'bar'}, l.hello)
+    l.connect()
+    b.notify('foo')
+    b.notify('bar')
+    b.notify('hello') # Normal dispatching still work
+    eq_(l.hello_count, 3)