feat: Expose TokenManager with lifecycle APIs for session management
TI
16d ago+397+397+012345678901234567890123456789-3-3-0123456789
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- New feature (non-breaking change which adds functionality)
New Feature Submission:
- Does your submission pass the tests?
- Have you linted your code locally prior to submission?
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
Description
Exposes rx.get_token_manager() with new lifecycle APIs for handling client/session lifecycle within background events, as requested in #5669.
New APIs on TokenManager:
| Method | Description |
|---|---|
session_is_connected(sid) | Async iterator yielding client token until the session disconnects |
token_is_connected(client_token) | Async iterator yielding session ID until the token disconnects |
when_session_disconnects(sid) | Returns asyncio.Event set on disconnect |
when_token_disconnects(client_token) | Returns asyncio.Event set on disconnect |
Public accessor:
rx.get_token_manager()— returns the activeTokenManageror raisesRuntimeError
Files changed (3):
reflex/utils/token_manager.py— Added lifecycle methods, disconnect notification fromdisconnect_token,get_token_manager(), and_TokenNotConnectedErrorreflex/__init__.py— Exposedget_token_managervia lazy loader mappingtests/units/utils/test_token_manager.py— Added 19 new tests covering all lifecycle APIs
Example usage:
@rx.event(background=True)
async def thing_checker(self):
for _task_active in rx.get_token_manager().session_is_connected(
sid=self.router_data.session.session_id
):
async with self:
if await self._check_thing():
await self._update_thing()
await asyncio.sleep(CHECK_INTERVAL)
Test results:
- 3567 tests passed (3548 existing + 19 new), 0 failures
closes #5669