|
27 | 27 | from . import errors, events, messages |
28 | 28 | from ._callback import invoke |
29 | 29 | from ._compositor import Compositor, MapGeometry |
30 | | -from ._context import visible_screen_stack |
| 30 | +from ._context import active_message_pump, visible_screen_stack |
31 | 31 | from ._path import CSSPathType, _css_path_type_as_list, _make_path_object_relative |
32 | 32 | from ._types import CallbackType |
33 | 33 | from .binding import Binding |
|
48 | 48 |
|
49 | 49 | # Unused & ignored imports are needed for the docs to link to these objects: |
50 | 50 | from .errors import NoWidget # type: ignore # noqa: F401 |
| 51 | + from .message_pump import MessagePump |
51 | 52 |
|
52 | 53 | # Screen updates will be batched so that they don't happen more often than 60 times per second: |
53 | 54 | UPDATE_PERIOD: Final[float] = 1 / 60 |
@@ -154,7 +155,7 @@ def __init__( |
154 | 155 | self._compositor = Compositor() |
155 | 156 | self._dirty_widgets: set[Widget] = set() |
156 | 157 | self.__update_timer: Timer | None = None |
157 | | - self._callbacks: list[CallbackType] = [] |
| 158 | + self._callbacks: list[tuple[CallbackType, MessagePump]] = [] |
158 | 159 | self._result_callbacks: list[ResultCallback[ScreenResultType]] = [] |
159 | 160 |
|
160 | 161 | self._tooltip_widget: Widget | None = None |
@@ -587,17 +588,22 @@ async def _invoke_and_clear_callbacks(self) -> None: |
587 | 588 | if self._callbacks: |
588 | 589 | callbacks = self._callbacks[:] |
589 | 590 | self._callbacks.clear() |
590 | | - for callback in callbacks: |
591 | | - await invoke(callback) |
| 591 | + for callback, message_pump in callbacks: |
| 592 | + reset_token = active_message_pump.set(message_pump) |
| 593 | + try: |
| 594 | + await invoke(callback) |
| 595 | + finally: |
| 596 | + active_message_pump.reset(reset_token) |
592 | 597 |
|
593 | | - def _invoke_later(self, callback: CallbackType) -> None: |
| 598 | + def _invoke_later(self, callback: CallbackType, sender: MessagePump) -> None: |
594 | 599 | """Enqueue a callback to be invoked after the screen is repainted. |
595 | 600 |
|
596 | 601 | Args: |
597 | 602 | callback: A callback. |
| 603 | + sender: The sender (active message pump) of the callback. |
598 | 604 | """ |
599 | 605 |
|
600 | | - self._callbacks.append(callback) |
| 606 | + self._callbacks.append((callback, sender)) |
601 | 607 | self.check_idle() |
602 | 608 |
|
603 | 609 | def _push_result_callback( |
|
0 commit comments