feat(event): add @rx.event(detached=True) and EventContext.enqueue_detached (#7239)
SH
1d ago+167+167+012345678901234567890123456789-0-0-0123456789
Fixes #7239
Summary
Adds a first-class way to enqueue events that outlive a supersedes=True parent chain (such as on_load_internal after yield rx.redirect(...)) without being cancelled by the parent chain's cancellation cascade.
Changes
packages/reflex-base/src/reflex_base/event/__init__.py(DETACHED_MARKER,EventHandler.detached,EventDecorator):- Added
detached: bool | None = Noneto@rx.event/EventDecoratorandEventHandler.detached.
- Added
packages/reflex-base/src/reflex_base/event/processor/event_processor.py(EventProcessor.enqueue):- Clears
parent_txid=Noneonev_ctxwhen the target handler hasdetached=True, starting a fresh root chain with its ownroot_gen.
- Clears
packages/reflex-base/src/reflex_base/event/context.py(EventContext.enqueue_detached):- Added
await EventContext.get().enqueue_detached(*events)to enqueue events as independent root chains at the call site without relying on privateEventContextfields.
- Added
tests/units/reflex_base/event/processor/test_event_processor.py:- Added
test_detached_event_survives_superseding_parentandtest_enqueue_detached_survives_superseding_parent.
- Added
packages/reflex-base/news/+detached-events.feature.md:- Added news fragment.
Testing
pytest tests/units/reflex_base/event/processor/test_event_processor.py(72 passed, 0 failed)ruff checkandruff format --checkclean.
Note: This PR was developed with AI assistance and locally verified against the unit test suite and linter.