Added a way to check if a signal has listeners
This commit is contained in:
parent
07ffe78143
commit
3fcca0e947
|
@ -141,6 +141,9 @@ class Signal(object):
|
|||
del self.receivers[index]
|
||||
break
|
||||
|
||||
def has_listeners(self, sender=None):
|
||||
return bool(self._live_receivers(_make_id(sender)))
|
||||
|
||||
def send(self, sender, **named):
|
||||
"""
|
||||
Send signal from sender to all connected receivers.
|
||||
|
|
|
@ -126,6 +126,17 @@ class DispatcherTests(unittest.TestCase):
|
|||
a_signal.disconnect(receiver_3)
|
||||
self._testIsClean(a_signal)
|
||||
|
||||
def test_has_listeners(self):
|
||||
self.assertIs(a_signal.has_listeners(), False)
|
||||
self.assertIs(a_signal.has_listeners(sender=object()), False)
|
||||
receiver_1 = Callable()
|
||||
a_signal.connect(receiver_1)
|
||||
self.assertIs(a_signal.has_listeners(), True)
|
||||
self.assertIs(a_signal.has_listeners(sender=object()), True)
|
||||
a_signal.disconnect(receiver_1)
|
||||
self.assertIs(a_signal.has_listeners(), False)
|
||||
self.assertIs(a_signal.has_listeners(sender=object()), False)
|
||||
|
||||
|
||||
class ReceiverTestCase(unittest.TestCase):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue