|
| 1 | +""" |
| 2 | +Signals are a simple pub-sub mechanism. |
| 3 | +
|
| 4 | +DOMNodes can subscribe to a signal, which will invoke a callback when the signal is published. |
| 5 | +
|
| 6 | +This is experimental for now, for internal use. It may be part of the public API in a future release. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import TYPE_CHECKING |
| 13 | +from weakref import WeakKeyDictionary |
| 14 | + |
| 15 | +import rich.repr |
| 16 | + |
| 17 | +from textual import log |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from ._types import IgnoreReturnCallbackType |
| 21 | + from .dom import DOMNode |
| 22 | + |
| 23 | + |
| 24 | +class SignalError(Exception): |
| 25 | + """Base class for a signal.""" |
| 26 | + |
| 27 | + |
| 28 | +@rich.repr.auto(angular=True) |
| 29 | +class Signal: |
| 30 | + """A signal that a widget may subscribe to, in order to invoke callbacks when an associated event occurs.""" |
| 31 | + |
| 32 | + def __init__(self, owner: DOMNode, name: str) -> None: |
| 33 | + """Initialize a signal. |
| 34 | +
|
| 35 | + Args: |
| 36 | + owner: The owner of this signal. |
| 37 | + name: An identifier for debugging purposes. |
| 38 | + """ |
| 39 | + self._owner = owner |
| 40 | + self._name = name |
| 41 | + self._subscriptions: WeakKeyDictionary[ |
| 42 | + DOMNode, list[IgnoreReturnCallbackType] |
| 43 | + ] = WeakKeyDictionary() |
| 44 | + |
| 45 | + def __rich_repr__(self) -> rich.repr.Result: |
| 46 | + yield "owner", self._owner |
| 47 | + yield "name", self._name |
| 48 | + yield "subscriptions", list(self._subscriptions.keys()) |
| 49 | + |
| 50 | + def subscribe(self, node: DOMNode, callback: IgnoreReturnCallbackType) -> None: |
| 51 | + """Subscribe a node to this signal. |
| 52 | +
|
| 53 | + When the signal is published, the callback will be invoked. |
| 54 | +
|
| 55 | + Args: |
| 56 | + node: Node to subscribe. |
| 57 | + callback: A callback function which takes no arguments, and returns anything (return type ignored). |
| 58 | + """ |
| 59 | + if not node.is_running: |
| 60 | + raise SignalError( |
| 61 | + f"Node must be running to subscribe to a signal (has {node} been mounted)?" |
| 62 | + ) |
| 63 | + callbacks = self._subscriptions.setdefault(node, []) |
| 64 | + if callback not in callbacks: |
| 65 | + callbacks.append(callback) |
| 66 | + |
| 67 | + def unsubscribe(self, node: DOMNode) -> None: |
| 68 | + """Unsubscribe a node from this signal. |
| 69 | +
|
| 70 | + Args: |
| 71 | + node: Node to unsubscribe, |
| 72 | + """ |
| 73 | + self._subscriptions.pop(node, None) |
| 74 | + |
| 75 | + def publish(self) -> None: |
| 76 | + """Publish the signal (invoke subscribed callbacks).""" |
| 77 | + |
| 78 | + for node, callbacks in list(self._subscriptions.items()): |
| 79 | + if not node.is_running: |
| 80 | + # Removed nodes that are no longer running |
| 81 | + self._subscriptions.pop(node) |
| 82 | + else: |
| 83 | + # Call callbacks |
| 84 | + for callback in callbacks: |
| 85 | + try: |
| 86 | + callback() |
| 87 | + except Exception as error: |
| 88 | + log.error(f"error publishing signal to {node} ignored; {error}") |
0 commit comments