@@ -279,7 +279,7 @@ def __init__(
279279
280280 # Keep track of effects so we can destroy them when the chat is destroyed
281281 self ._effects : list [reactive .Effect_ ] = []
282- self ._bookmark_callbacks : list [ CancelCallback ] = []
282+ self ._cancel_bookmarking_callbacks : CancelCallback | None
283283
284284 # Initialize chat state and user input effect
285285 with session_context (self ._session ):
@@ -1376,17 +1376,19 @@ def destroy(self):
13761376 Destroy the chat instance.
13771377 """
13781378 self ._destroy_effects ()
1379- self ._destroy_bookmarks ()
1379+ self ._destroy_bookmarking ()
13801380
13811381 def _destroy_effects (self ):
13821382 for x in self ._effects :
13831383 x .destroy ()
13841384 self ._effects .clear ()
13851385
1386- def _destroy_bookmarks (self ):
1387- for x in self ._bookmark_callbacks :
1388- x ()
1389- self ._bookmark_callbacks .clear ()
1386+ def _destroy_bookmarking (self ):
1387+ if not self ._cancel_bookmarking_callbacks :
1388+ return
1389+
1390+ self ._cancel_bookmarking_callbacks ()
1391+ self ._cancel_bookmarking_callbacks = None
13901392
13911393 async def _remove_loading_message (self ):
13921394 await self ._send_custom_message ("shiny-chat-remove-loading-message" , None )
@@ -1488,7 +1490,7 @@ async def set_chatlas_state(value: Jsonifiable) -> None:
14881490 )
14891491
14901492 # Reset prior bookmarking hooks
1491- self ._destroy_bookmarks ()
1493+ self ._destroy_bookmarking ()
14921494
14931495 # Must use `root_session` as the id is already resolved. :-/
14941496 # Using a proxy session would double-encode the proxy-prefix
@@ -1519,7 +1521,7 @@ async def _():
15191521 await session .bookmark ()
15201522
15211523 @root_session .bookmark .on_bookmark
1522- async def cancel_on_bookmark_client (state : BookmarkState ):
1524+ async def _on_bookmark_client (state : BookmarkState ):
15231525 if resolved_bookmark_id_str in state .values :
15241526 raise ValueError (
15251527 f'Bookmark value with id (`"{ resolved_bookmark_id_str } "`) already exists.'
@@ -1529,7 +1531,7 @@ async def cancel_on_bookmark_client(state: BookmarkState):
15291531 state .values [resolved_bookmark_id_str ] = await get_state ()
15301532
15311533 @root_session .bookmark .on_bookmark
1532- def cancel_on_bookmark_ui (state : BookmarkState ):
1534+ def _on_bookmark_ui (state : BookmarkState ):
15331535 if resolved_bookmark_id_msgs_str in state .values :
15341536 raise ValueError (
15351537 f'Bookmark value with id (`"{ resolved_bookmark_id_msgs_str } "`) already exists.'
@@ -1551,7 +1553,7 @@ def cancel_on_bookmark_ui(state: BookmarkState):
15511553 self ._init_chat .destroy ()
15521554
15531555 @root_session .bookmark .on_restore
1554- async def cancel_on_restore_client (state : RestoreState ):
1556+ async def _on_restore_client (state : RestoreState ):
15551557 if resolved_bookmark_id_str not in state .values :
15561558 return
15571559
@@ -1560,7 +1562,7 @@ async def cancel_on_restore_client(state: RestoreState):
15601562 await set_state (info )
15611563
15621564 @root_session .bookmark .on_restore
1563- async def cancel_on_restore_ui (state : RestoreState ):
1565+ async def _on_restore_ui (state : RestoreState ):
15641566 print ("set chat ui" )
15651567 # Do not call `self.clear_messages()` as it will clear the
15661568 # `chat.ui(messages=)` in addition to the `self.messages()`
@@ -1582,16 +1584,16 @@ async def cancel_on_restore_ui(state: RestoreState):
15821584 await self .append_message (message_dict )
15831585 print ("done-restore ui" )
15841586
1585- def cancel_bookmark ():
1586- cancel_on_bookmark_client ()
1587- cancel_on_bookmark_ui ()
1588- cancel_on_restore_client ()
1589- cancel_on_restore_ui ()
1587+ def _cancel_bookmarking ():
1588+ _on_bookmark_client ()
1589+ _on_bookmark_ui ()
1590+ _on_restore_client ()
1591+ _on_restore_ui ()
15901592
15911593 # Store the callbacks to be able to destroy them later
1592- self ._bookmark_callbacks . append ( cancel_bookmark )
1594+ self ._cancel_bookmarking_callbacks = _cancel_bookmarking
15931595
1594- return BookmarkCancelCallback (cancel_bookmark )
1596+ return BookmarkCancelCallback (_cancel_bookmarking )
15951597
15961598
15971599@add_example ("app-express.py" )
0 commit comments