Modify the state out of band. Args: token: The token to modify the state for. Yields: The state to modify. Raises: RuntimeError: If the app has not been initialized yet.
(self, token: str)
| 805 | compiler_utils.write_page(output_path, code) |
| 806 | @contextlib.asynccontextmanager |
| 807 | async def modify_state(self, token: str) -> AsyncIterator[BaseState]: |
| 808 | """Modify the state out of band. |
| 809 | |
| 810 | Args: |
| 811 | token: The token to modify the state for. |
| 812 | |
| 813 | Yields: |
| 814 | The state to modify. |
| 815 | |
| 816 | Raises: |
| 817 | RuntimeError: If the app has not been initialized yet. |
| 818 | """ |
| 819 | if self.event_namespace is None: |
| 820 | raise RuntimeError("App has not been initialized yet.") |
| 821 | |
| 822 | # Get exclusive access to the state. |
| 823 | async with self.state_manager.modify_state(token) as state: |
| 824 | # No other event handler can modify the state while in this context. |
| 825 | yield state |
| 826 | delta = state.get_delta() |
| 827 | if delta: |
| 828 | # When the state is modified reset dirty status and emit the delta to the frontend. |
| 829 | state._clean() |
| 830 | await self.event_namespace.emit_update( |
| 831 | update=StateUpdate(delta=delta), |
| 832 | sid=state.router.session.session_id, |
| 833 | ) |
| 834 | |
| 835 | def _process_background( |
| 836 | self, state: BaseState, event: Event |