Set Reflex contexts needed for state and event processing. Pushes RegistrationContext and EventContext into the current contextvars scope, but only if they are not already set. Can be used as a context manager:: with app.set_contexts(): async wi
(self)
| 634 | return tokens |
| 635 | |
| 636 | def set_contexts(self) -> contextlib.AbstractContextManager: |
| 637 | """Set Reflex contexts needed for state and event processing. |
| 638 | |
| 639 | Pushes RegistrationContext and EventContext into the current |
| 640 | contextvars scope, but only if they are not already set. |
| 641 | |
| 642 | Can be used as a context manager:: |
| 643 | |
| 644 | with app.set_contexts(): |
| 645 | async with app.modify_state(token) as state: |
| 646 | ... |
| 647 | |
| 648 | Returns: |
| 649 | A context manager that resets any contexts that were set on exit. |
| 650 | """ |
| 651 | tokens = self._set_contexts_internal() |
| 652 | if not tokens: |
| 653 | return contextlib.nullcontext() |
| 654 | stack = contextlib.ExitStack() |
| 655 | for ctx_cls, tok in tokens.items(): |
| 656 | stack.callback(ctx_cls.reset, tok) |
| 657 | return stack |
| 658 | |
| 659 | def _context_middleware(self, app: ASGIApp) -> ASGIApp: |
| 660 | """Ensure Reflex contexts are attached for each ASGI request. |