Skip to content

Commit

Permalink
Ensure no events are dispatched before the websocket is open (#6528)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Mar 19, 2024
1 parent 56e3103 commit 18c0ac1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
10 changes: 5 additions & 5 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.""")

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
"""
Expand Down
2 changes: 1 addition & 1 deletion panel/io/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions panel/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 18c0ac1

Please sign in to comment.