feat(event): add @rx.event(detached=True) and EventContext.enqueue_detached (#7239)

SH
1d ago
+167-0

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 = None to @rx.event / EventDecorator and EventHandler.detached.
  • packages/reflex-base/src/reflex_base/event/processor/event_processor.py (EventProcessor.enqueue):
    • Clears parent_txid=None on ev_ctx when the target handler has detached=True, starting a fresh root chain with its own root_gen.
  • 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 private EventContext fields.
  • tests/units/reflex_base/event/processor/test_event_processor.py:
    • Added test_detached_event_survives_superseding_parent and test_enqueue_detached_survives_superseding_parent.
  • 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 check and ruff format --check clean.

Note: This PR was developed with AI assistance and locally verified against the unit test suite and linter.