|
97 | 97 | State, |
98 | 98 | StateManager, |
99 | 99 | StateUpdate, |
| 100 | + _split_substate_key, |
100 | 101 | _substate_key, |
101 | 102 | all_base_state_classes, |
102 | 103 | code_uses_state_contexts, |
@@ -1559,7 +1560,7 @@ async def modify_state(self, token: str) -> AsyncIterator[BaseState]: |
1559 | 1560 | state._clean() |
1560 | 1561 | await self.event_namespace.emit_update( |
1561 | 1562 | update=StateUpdate(delta=delta), |
1562 | | - sid=state.router.session.session_id, |
| 1563 | + token=token, |
1563 | 1564 | ) |
1564 | 1565 |
|
1565 | 1566 | def _process_background( |
@@ -1599,7 +1600,7 @@ async def _coro(): |
1599 | 1600 | # Send the update to the client. |
1600 | 1601 | await self.event_namespace.emit_update( |
1601 | 1602 | update=update, |
1602 | | - sid=state.router.session.session_id, |
| 1603 | + token=event.token, |
1603 | 1604 | ) |
1604 | 1605 |
|
1605 | 1606 | task = asyncio.create_task( |
@@ -2061,20 +2062,19 @@ def on_disconnect(self, sid: str): |
2061 | 2062 | and console.error(f"Token cleanup error: {t.exception()}") |
2062 | 2063 | ) |
2063 | 2064 |
|
2064 | | - async def emit_update(self, update: StateUpdate, sid: str) -> None: |
| 2065 | + async def emit_update(self, update: StateUpdate, token: str) -> None: |
2065 | 2066 | """Emit an update to the client. |
2066 | 2067 |
|
2067 | 2068 | Args: |
2068 | 2069 | update: The state update to send. |
2069 | | - sid: The Socket.IO session id. |
| 2070 | + token: The client token (tab) associated with the event. |
2070 | 2071 | """ |
2071 | | - if not sid: |
| 2072 | + client_token, _ = _split_substate_key(token) |
| 2073 | + sid = self.token_to_sid.get(client_token) |
| 2074 | + if sid is None: |
2072 | 2075 | # If the sid is None, we are not connected to a client. Prevent sending |
2073 | 2076 | # updates to all clients. |
2074 | | - return |
2075 | | - token = self.sid_to_token.get(sid) |
2076 | | - if token is None: |
2077 | | - console.warn(f"Attempting to send delta to disconnected websocket {sid}") |
| 2077 | + console.warn(f"Attempting to send delta to disconnected client {token!r}") |
2078 | 2078 | return |
2079 | 2079 | # Creating a task prevents the update from being blocked behind other coroutines. |
2080 | 2080 | await asyncio.create_task( |
@@ -2165,7 +2165,7 @@ async def on_event(self, sid: str, data: Any): |
2165 | 2165 | # Process the events. |
2166 | 2166 | async for update in updates_gen: |
2167 | 2167 | # Emit the update from processing the event. |
2168 | | - await self.emit_update(update=update, sid=sid) |
| 2168 | + await self.emit_update(update=update, token=event.token) |
2169 | 2169 |
|
2170 | 2170 | async def on_ping(self, sid: str): |
2171 | 2171 | """Event for testing the API endpoint. |
|
0 commit comments