Commits

Vladimir Mihailenco committed 3838713

Add priority to handlers

  • Participants
  • Parent commits f2acc0a

Comments (0)

Files changed (1)

 ANY_ID = 0
 
 
+def default_attrgetter(attr, default=None):
+    def getter(obj):
+        if hasattr(obj, attr):
+            return getattr(obj, attr)
+        return default
+    return getter
+
+
 class Signal(object):
     """A notification emitter."""
 
                 raise
         return receiver
 
-    def connect_via(self, sender, weak=False):
+    def connect_via(self, sender=ANY, weak=False):
         """Connect the decorated function as a receiver for *sender*.
 
         :param sender: Any object or :obj:`ANY`.  The decorated function
     def receivers_for(self, sender):
         """Iterate all live receivers listening for *sender*."""
         # TODO: test receivers_for(ANY)
+        receivers = []
         if self.receivers:
             sender_id = hashable_identity(sender)
             if sender_id in self._by_sender:
                         self._disconnect(receiver_id, ANY_ID)
                         continue
                     receiver = strong
-                yield receiver
+                receivers.append(receiver)
+
+        receivers.sort(key=default_attrgetter('priority', default=100),
+                       reverse=True)
+        return iter(receivers)
 
     def disconnect(self, receiver, sender=ANY):
         """Disconnect *receiver* from this signal's events.