__init__.py 867 B

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