Release unmounted reflex_xy chart views after React clears DOM refs

Merged
FA
22d ago
+195-17

Fixes #548.

Root cause

XYChart.jsx registered each view in window.__xy_views under outerRef.current?.id || mid, then re-read that expression during effect cleanup. React detaches DOM refs (object refs nulled, callback refs called with null) before passive effect cleanup, so cleanup fell back to the mount id and the delete missed the id-keyed entry. Destroyed views (e.g. the showcase board after the small-multiples toggle) stayed in the registry.

Fix

  • Capture the registry key once per mount when the effect runs, and reuse it for both registration and removal (live and static modes).
  • New releaseViewKey(key, view) deletes the entry only while it still holds this mount's view, so a later mount reusing the same id is never evicted by an earlier mount's cleanup.

Tests

  • tests/reflex_socket_connection.cjs (run via tests/test_reflex_socket_connection.py): the stub now detaches object and callback refs before running cleanup, matching React's order. All five connection-order variants assert exact registry keys and an empty registry after the last unmount. New test covers anonymous + named mounts releasing their views while the shared app transport stays connected. Before the fix, six tests failed; after it, all seven pass.
  • scripts/reflex_ws_smoke.py: the cond/foreach step now asserts exact counts 13 → 15 → 13 across the toggle and back, that board leaves the registry, and that no registered view is destroyed. Verified it times out on the old JSX and passes with the fix against the running demo (prod, single port).
  • tests/reflex_adapter + socket gate: 252 passed. pre-commit, ruff check, ruff format --check clean.

Not covered: client-side navigation to /kinds and back — the smoke loads /kinds with a full page navigation.

Docs

  • spec/design/reflex-integration.md: new "Unmount releases the view" paragraph.
  • news/548.bugfix.md.

Generated by Claude Code