Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only delete page content if the number of connections is zero #4285

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions nicegui/air.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ def _handle_handshake(data: Dict[str, Any]) -> bool:
return True

@self.relay.on('client_disconnect')
def _handle_client_disconnect(data: Dict[str, Any]) -> None:
async def _handle_client_disconnect(data: Dict[str, Any]) -> None:
self.log.debug('client disconnected.')
client_id = data['client_id']
if client_id not in Client.instances:
return
Client.instances[client_id].handle_disconnect()
await Client.instances[client_id].handle_disconnect()

@self.relay.on('connect')
async def _handle_connect() -> None:
Expand Down
25 changes: 11 additions & 14 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ 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._disconnect_task: Optional[asyncio.Task] = None
self._num_connections = 0
self._deleted = False
self.tab_id: Optional[str] = None

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

def handle_handshake(self, next_message_id: Optional[int]) -> None:
"""Cancel pending disconnect task and invoke connect handlers."""
self._num_connections += 1
if next_message_id is not None:
self.outbox.try_rewind(next_message_id)
if self._disconnect_task:
self._disconnect_task.cancel()
self._disconnect_task = None
storage.request_contextvar.set(self.request)
for t in self.connect_handlers:
self.safe_invoke(t)
for t in core.app._connect_handlers: # pylint: disable=protected-access
self.safe_invoke(t)

def handle_disconnect(self) -> None:
async def handle_disconnect(self) -> None:
"""Wait for the browser to reconnect; invoke disconnect handlers if it doesn't."""
async def handle_disconnect() -> None:
await asyncio.sleep(self.page.resolve_reconnect_timeout())
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:
self.delete()
self._disconnect_task = background_tasks.create(handle_disconnect())
self._num_connections -= 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)
await asyncio.sleep(self.page.resolve_reconnect_timeout())
falkoschindler marked this conversation as resolved.
Show resolved Hide resolved
if self._num_connections == 0 and not self.shared:
self.delete()

def handle_event(self, msg: Dict) -> None:
"""Forward an event to the corresponding element."""
Expand Down
4 changes: 2 additions & 2 deletions nicegui/nicegui.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ async def _on_handshake(sid: str, data: Dict[str, Any]) -> bool:


@sio.on('disconnect')
def _on_disconnect(sid: str) -> None:
async def _on_disconnect(sid: str) -> None:
query_bytes: bytearray = sio.get_environ(sid)['asgi.scope']['query_string']
query = urllib.parse.parse_qs(query_bytes.decode())
client_id = query['client_id'][0]
client = Client.instances.get(client_id)
if client:
client.handle_disconnect()
await client.handle_disconnect()


@sio.on('event')
Expand Down
Loading