Skip to content

Commit

Permalink
restore previous behavior: call disconnect handlers only once
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Jan 31, 2025
1 parent 31219a1 commit d0f52ae
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def __init__(self, page: page, *, request: Optional[Request]) -> None:
self.environ: Optional[Dict[str, Any]] = None
self.shared = request is None
self.on_air = False
self._num_connections = defaultdict(int)
self._delete_task: Optional[asyncio.Task] = None
self._num_connections: defaultdict[str, int] = defaultdict(int)
self._delete_tasks: Dict[str, asyncio.Task] = {}
self._deleted = False
self.tab_id: Optional[str] = None

Expand Down Expand Up @@ -238,7 +238,7 @@ def on_disconnect(self, handler: Union[Callable[..., Any], Awaitable]) -> None:

def handle_handshake(self, socket_id: str, next_message_id: Optional[int]) -> None:
"""Cancel pending disconnect task and invoke connect handlers."""
self._cancel_delete_task()
self._cancel_delete_task(socket_id)
self._num_connections[socket_id] += 1
if next_message_id is not None:
self.outbox.try_rewind(next_message_id)
Expand All @@ -249,25 +249,30 @@ def handle_handshake(self, socket_id: str, next_message_id: Optional[int]) -> No
self.safe_invoke(t)

def handle_disconnect(self, socket_id: str) -> None:
"""Wait for the browser to reconnect; invoke disconnect handlers if it doesn't."""
self._cancel_delete_task()
"""Wait for the browser to reconnect; invoke disconnect handlers if it doesn't.
NOTE:
In contrast to connect handlers, disconnect handlers are not called during a reconnect.
This behavior should be fixed in version 3.0.
"""
self._cancel_delete_task(socket_id)
self._num_connections[socket_id] -= 1
for t in self.disconnect_handlers:
self.safe_invoke(t)
for t in core.app._disconnect_handlers: # pylint: disable=protected-access
self.safe_invoke(t)
if not self.shared:
async def delete_content() -> None:
await asyncio.sleep(self.page.resolve_reconnect_timeout())
if self._num_connections[socket_id] == 0:
self._num_connections.pop(socket_id)

async def delete_content() -> None:
await asyncio.sleep(self.page.resolve_reconnect_timeout())
if self._num_connections[socket_id] == 0:
for t in self.disconnect_handlers:
self.safe_invoke(t)
for t in core.app._disconnect_handlers: # pylint: disable=protected-access
self.safe_invoke(t)
self._num_connections.pop(socket_id)
if not self.shared:
self.delete()
self._delete_task = background_tasks.create(delete_content())
self._delete_task = background_tasks.create(delete_content())

def _cancel_delete_task(self) -> None:
if self._delete_task:
self._delete_task.cancel()
self._delete_task = None
def _cancel_delete_task(self, socket_id: str) -> None:
if socket_id in self._delete_tasks:
self._delete_tasks.pop(socket_id).cancel()

def handle_event(self, msg: Dict) -> None:
"""Forward an event to the corresponding element."""
Expand Down

0 comments on commit d0f52ae

Please sign in to comment.