1from frozenlist import FrozenList
2
3__version__ = "1.3.2"
4
5__all__ = ("Signal",)
6
7
8class Signal(FrozenList):
9 """Coroutine-based signal implementation.
10
11 To connect a callback to a signal, use any list method.
12
13 Signals are fired using the send() coroutine, which takes named
14 arguments.
15 """
16
17 __slots__ = ("_owner",)
18
19 def __init__(self, owner):
20 super().__init__()
21 self._owner = owner
22
23 def __repr__(self):
24 return "<Signal owner={}, frozen={}, {!r}>".format(
25 self._owner, self.frozen, list(self)
26 )
27
28 async def send(self, *args, **kwargs):
29 """
30 Sends data to all registered receivers.
31 """
32 if not self.frozen:
33 raise RuntimeError("Cannot send non-frozen signal.")
34
35 for receiver in self:
36 await receiver(*args, **kwargs) # type: ignore