From 18c0ac1bfe548fe2cf40381831c58b602b0d4124 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Tue, 19 Mar 2024 14:11:21 -0400 Subject: [PATCH] Ensure no events are dispatched before the websocket is open (#6528) --- panel/chat/feed.py | 10 +++++----- panel/io/state.py | 2 +- panel/tests/test_server.py | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/panel/chat/feed.py b/panel/chat/feed.py index 8e4a71ab24..a09a8051e6 100644 --- a/panel/chat/feed.py +++ b/panel/chat/feed.py @@ -26,7 +26,6 @@ from ..layout.card import Card from ..layout.spacer import VSpacer from ..pane.image import SVG -from ..widgets.button import Button from .message import ChatMessage if TYPE_CHECKING: @@ -181,6 +180,8 @@ class ChatFeed(ListPanel): _callback_state = param.ObjectSelector(objects=list(CallbackState), doc=""" The current state of the callback.""") + _callback_trigger = param.Event(doc="Triggers the callback to respond.") + _disabled_stack = param.List(doc=""" The previous disabled state of the feed.""") @@ -260,8 +261,7 @@ def __init__(self, *objects, **params): ) # handle async callbacks using this trick - self._callback_trigger = Button(visible=False) - self._callback_trigger.on_click(self._prepare_response) + self.param.watch(self._prepare_response, '_callback_trigger') def _get_model( self, doc: Document, root: Model | None = None, @@ -491,7 +491,7 @@ async def _handle_callback(self, message, loop: asyncio.BaseEventLoop): response = await asyncio.to_thread(self.callback, *callback_args) await self._serialize_response(response) - async def _prepare_response(self, _) -> None: + async def _prepare_response(self, *_) -> None: """ Prepares the response by scheduling the placeholder and executing the callback. @@ -656,7 +656,7 @@ def respond(self): """ Executes the callback with the latest message in the chat log. """ - self._callback_trigger.param.trigger("clicks") + self.param.trigger("_callback_trigger") def stop(self) -> bool: """ diff --git a/panel/io/state.py b/panel/io/state.py index 66d882e399..216dd133d1 100644 --- a/panel/io/state.py +++ b/panel/io/state.py @@ -268,7 +268,7 @@ def _thread_id(self, thread_id: int) -> None: self._thread_id_[self.curdoc] = thread_id def _unblocked(self, doc: Document) -> bool: - return doc is self.curdoc and self._thread_id in (self._current_thread, None) + return doc is self.curdoc and self._thread_id in (self._current_thread, None) and (not doc.session_context or self._loaded.get(doc)) @param.depends('_busy_counter', watch=True) def _update_busy_counter(self): diff --git a/panel/tests/test_server.py b/panel/tests/test_server.py index 3bf2f8aa4a..32048dacd2 100644 --- a/panel/tests/test_server.py +++ b/panel/tests/test_server.py @@ -291,6 +291,9 @@ async def cb(count=[0]): def app(): button = Button(name='Click') state.add_periodic_callback(cb, 100) + def loaded(): + state._schedule_on_load(state.curdoc, None) + state.execute(loaded, schedule=True) return button serve_and_request(app) @@ -715,6 +718,9 @@ def cb(count=[0]): def app(): button = Button(name='Click') state.add_periodic_callback(cb, 100) + def loaded(): + state._schedule_on_load(state.curdoc, None) + state.execute(loaded, schedule=True) return button serve_and_request(app)