Skip to content

fix: keep the client's overlay containers across a reconnect (dialogs, snack bars and sheets work again) - #145

Open
AdamMusa wants to merge 4 commits into
mainfrom
audit/reconnect-overlay-containers
Open

AdamMusa wants to merge 4 commits into
mainfrom
audit/reconnect-overlay-containers

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #142

A client keeps its control tree when it reconnects, and its View holds the page's _overlay and _dialogs containers it looked up once. Both servers used to hand a reconnecting client containers that replaced those instances, so after a reconnect no dialog, snack bar or bottom sheet showed again:

  • ruflet_rails (ConnectionProtocol) re-sent the containers as replace ops.
  • ruflet run (Ruflet::Server) merges its page_patch by id, but the container ids moved with the content.

What changed

  • Page (ruflet_core):
    • The containers have fixed wire ids: _overlay 3, _services 4, _dialogs 5 (Page::OVERLAY_WIRE_ID, SERVICES_WIRE_ID, DIALOGS_WIRE_ID). This follows the page (1), window (2) and implicit view (20). The allocator still starts at 100, so nothing collides.
    • New Page#register_containers_patch returns only the three containers as a property map, marks them mounted, and records what they hold. It does not publish the page shell, so the next page.update still sends the views.
    • On main, the first view patch carried the containers whole. That is how a container change the app block made without sending it reached the client. A view patch now leaves mounted containers out. So the first view patch after register_containers_patch checks each container and every control inside it. If anything changed and was not sent, that container's own patch goes out right after the view patch. Examples: page.overlay << control, a dialog given to set_view_props, or sb.props["open"] = true after page.snack_bar = sb. Later view patches do not run this check: they send what they sent on main.
    • show_dialog decided whether to send the page first by "the container has a wire id". It now checks "the container is mounted", because the id is always set. A page whose containers are not mounted still sends the page first, as before.
  • Ruflet::ConnectionProtocol (ruflet_server, run by ruflet_rails' LocalServer): the register_client reply's page_patch carries those containers, for a fresh Page and for a resumed one, before the app block runs. The client merges that map by id, so a reconnecting client keeps its instances. The rest of the page still streams as op patches while the app block renders it.
    • The whole app block is not buffered the way Ruflet::Server does it. That would hide a spinner the block shows before slow work, and it would delay the first message until the block ends.
  • Ruflet::Server needs no change: with fixed ids, its page_patch merges into the client's containers on any route.

No API, CLI or Rails host API change. Nothing is configured differently, and apps need no change.

Behavior changes

All four follow from the #142 fix. In each case in the table below, the client ends with the same UI as on main, except the last row, which fixes a lost update.

  1. Wire ids. The internal containers are 3, 4 and 5 on every Page. Before, they took allocator ids of 100 and up, after the root controls. So controls registered after the containers now get ids 3 lower than before. Ids are internal to the wire: the server assigns them and the client echoes them. The H1 page goldens for ruflet run normalize ids by first appearance, and the order is unchanged, so they show no diff.
  2. ruflet_rails register reply. It now carries the containers instead of {}, and the page patches no longer replace them. On a first connect the client builds the same containers, from the reply instead of from the first patch. After a reconnect it keeps its containers, which fixes the bug.
    • H1 golden diff: the 5 --rails-endpoint page goldens. Their register reply carries _overlay, _services and _dialogs, and their page patch no longer does. Each has the same number of messages as on main.
  3. Rails app block, before its first view patch: the setters now send at once. page.dialog=, snack_bar=, bottom_sheet=, overlay=, services=, add_service and service(...) now send a patch_control to container 3, 4 or 5 right away, ahead of the view patch. The client has that container from the register reply. This is what these setters already do once the containers are mounted, in event handlers and under ruflet run. On main, in this window, the dialog setters sent nothing. overlay= and the services setters sent a patch to an allocator id the client did not have yet. The content then arrived inside the first view patch.
  4. Rails app block: what was not sent before the first view patch now goes out as its container's patch after that view patch (see What changed). On main it went out inside that view patch. This keeps that content reaching the client, at the cost of one more message.

Messages each app block sends after the register reply. Measured with the real ConnectionProtocol and a client that merges by id:

Rails app block main this PR client ends with
page.snack_bar = Ruflet.snack_bar(..., open: true); page.add (same with dialog= and page.update) 1 5, 1 the same
page.overlay = [t]; page.add 100 (not on the client), 1 3, 1 the same
page.service(:clipboard); page.add (same with add_service, services=) 101 (not on the client), 1 4, 1 the same
page.snack_bar = sb; sb.props["open"] = true; page.add (same with dialog= and page.update, and with bottom_sheet=) 1 5, 1, 5 the same: open
page.overlay = [t]; change t; page.add 100 (not on the client), 1 3, 1, 3 the same: the changed t
page.overlay << t; page.add 1 1, 3 the same
page.show_dialog(d); page.add 1, 102, 1 5, 1 the same
page.show_dialog(d); change the dialog's title; page.add 1, 102, 1 5, 1, 5 main keeps the old title (the change is recorded as sent and never sent); this PR shows the new one

How verified

All on head 0d29af4 (main d3f7f12 merged in).

  • New tests. Each fails on main and passes here, except test_rails_first_paint_stays_progressive, which guards what main already does. Each part of the fix was planted back on its own, and at least one test catches each plant (comments below).

    • ruflet_core/test/page_internal_containers_test.rb (11 tests): fixed ids on any content, no collision with the allocator, the containers-only patch, a resumed page's open dialog, content set before the first view patch, a change inside a container after its setter (snack bar, dialog, bottom sheet, overlay control), nothing sent twice, only the first view patch flushes, and show_dialog on an unmounted page.
    • ruflet_server/test/reconnect_containers_test.rb (5 tests): uses a small model of the engine's two apply paths (Control.update merges by _i, a replace op builds new controls). It checks that the View's captured _dialogs and _overlay instances survive a Rails fresh reconnect, a Rails resumed reconnect, and a ruflet run reconnect on a route with more content, and that the dialog shown afterwards lands in them. It also checks the register reply shape, and that Rails first paint stays progressive.
    • ruflet_rails/test/local_server_reconnect_test.rb (3 tests): the real LocalServer with a SessionRegistry. A fresh Page after the old socket closed names the same containers; a dialog shown afterwards patches container 5; a snack bar opened after page.snack_bar= in the app block reaches the client open.
    • ruflet_rails/test/session_registry_test.rb#test_local_server_reattaches_existing_page_when_websocket_reconnects asserted the replace op this fixes. It now asserts that the container, with the open dialog, is in the register reply, and that the reattach patch carries views but no _dialogs.
    • page_api_coverage_test.rb lists the new public method.
  • Real client replay. A widget test replayed the wire of 5 scenarios through the real PageControl, Navigator and ViewControl, with a real disconnect and reconnect, a client that keeps its tree, and then a dialog. It ran with Flutter 3.41.2 on engine codex/engine-initialization 00d1df3c and on the vendored engine in template main 8b08569, the client published gems build. The test itself is not committed. The wire of those 5 scenarios, regenerated on this head, is identical to the replayed one except for the random service uid.

    Scenario main this PR
    Rails, no reconnect shown shown
    Rails, fresh Page after reconnect not shown shown
    Rails, resumed Page not shown shown
    ruflet run, same route shown shown
    ruflet run, route renders more rows not shown shown
  • Suites, one process per file: ruflet_core (212 files), ruflet_server (19), ruflet_rails (22, under bundle exec) and ruflet (28), all green. H1 goldens: ruby test/compat/compat.rb --jobs 4 checked 359 goldens, rc 0, and test/compat_goldens_test.rb passes. No golden changed in this round; the 5 above are the only diffs against main.

  • page.rb and connection_protocol.rb compile with the embedded VM's mrbc. Both are compiled into the --self runtime.

Notes for reviewers

A reconnecting client keeps its control tree, and its View looks up the
page's _overlay and _dialogs containers once. ruflet_rails
(ConnectionProtocol) re-sent them as replace ops, and ruflet run merged
them by ids that moved with the content, so the client built new
containers and no dialog, snack bar or bottom sheet showed again.

- Page: _overlay, _services and _dialogs keep fixed wire ids 3, 4 and 5,
  below the allocator's 100, like the page (1), window (2) and view (20).
- Page#register_containers_patch: only those containers, for a register
  reply that op patches follow; marks them mounted. Until the next view
  patch, container content set without a push (overlay <<, a dialog given
  to set_view_props) still goes out, as its own patch.
- ConnectionProtocol sends that patch in the register_client reply, for a
  fresh and a resumed Page, before the app block. The rest of the page
  still streams as op patches: first paint stays progressive.
- show_dialog checks whether the container is mounted, not whether it has
  a wire id (it always has one now).

Behavior changes: the containers' wire ids (app controls after them get
ids 3 lower); the Rails register reply carries the containers and the
page patches no longer replace them (the 5 --rails-endpoint H1 page
goldens, regenerated; same message counts).
@AdamMusa

Copy link
Copy Markdown
Owner Author

Plants: each part of the fix put back on its own in a scratch copy, with the new and changed tests run against it. Every plant is caught by at least one test.

container ids back on the allocator (the three wire_id = lines removed)

  • ruflet_core/test/page_internal_containers_test.rb: 6 of 8 fail: test_container_content_set_before_the_first_view_patch_still_reaches_the_client, test_containers_already_sent_are_not_sent_again_with_the_first_view_patch, test_show_dialog_on_a_page_whose_containers_are_not_mounted_sends_the_page_first, test_register_containers_patch_carries_only_the_containers_and_leaves_the_views_to_the_next_patch, test_container_ids_do_not_depend_on_the_content_registered_before_them, test_a_resumed_page_sends_its_open_dialog_in_the_containers_patch
  • ruflet_server/test/reconnect_containers_test.rb: 2 of 5 fail: test_rails_register_reply_carries_the_containers_and_no_patch_replaces_them, test_ruflet_run_reconnect_on_a_route_with_more_content_keeps_the_clients_containers
  • ruflet_rails/test/local_server_reconnect_test.rb: 2 of 2 fail: test_a_fresh_page_after_the_old_socket_closed_names_the_same_containers, test_a_dialog_shown_after_a_fresh_reconnect_patches_the_same_container
  • ruflet_rails/test/session_registry_test.rb: 1 of 7 fail: test_local_server_reattaches_existing_page_when_websocket_reconnects

ConnectionProtocol back to Protocol.register_response(session_id:) (empty page_patch)

  • ruflet_core/test/page_internal_containers_test.rb: 8 runs, all pass
  • ruflet_server/test/reconnect_containers_test.rb: 3 of 5 fail: test_rails_reconnect_that_resumes_the_page_keeps_the_clients_containers, test_rails_register_reply_carries_the_containers_and_no_patch_replaces_them, test_rails_reconnect_with_a_fresh_page_keeps_the_clients_containers
  • ruflet_rails/test/local_server_reconnect_test.rb: 1 of 2 fail: test_a_fresh_page_after_the_old_socket_closed_names_the_same_containers
  • ruflet_rails/test/session_registry_test.rb: 1 of 7 fail: test_local_server_reattaches_existing_page_when_websocket_reconnects

no flush of container content set before the first view patch (unsent_container_change? always false)

  • ruflet_core/test/page_internal_containers_test.rb: 1 of 8 fail: test_container_content_set_before_the_first_view_patch_still_reaches_the_client
  • ruflet_server/test/reconnect_containers_test.rb: 5 runs, all pass
  • ruflet_rails/test/local_server_reconnect_test.rb: 2 runs, all pass
  • ruflet_rails/test/session_registry_test.rb: 7 runs, all pass

show_dialog back to send_view_patch unless @dialogs_container.wire_id

  • ruflet_core/test/page_internal_containers_test.rb: 1 of 8 fail: test_show_dialog_on_a_page_whose_containers_are_not_mounted_sends_the_page_first
  • ruflet_server/test/reconnect_containers_test.rb: 5 runs, all pass
  • ruflet_rails/test/local_server_reconnect_test.rb: 2 runs, all pass
  • ruflet_rails/test/session_registry_test.rb: 7 runs, all pass

All of main (a668af6), no fix

  • ruflet_core/test/page_internal_containers_test.rb: 8 of 8 fail.
  • ruflet_server/test/reconnect_containers_test.rb: 4 of 5 fail. test_rails_first_paint_stays_progressive passes on main on purpose: it pins that the first paint stays progressive.
  • ruflet_rails/test/local_server_reconnect_test.rb: 2 of 2 fail.
  • ruflet_rails/test/session_registry_test.rb: 1 of 7 fails (the updated reattach test).

page.snack_bar=, dialog=, bottom_sheet= and overlay= push their container
as soon as it is mounted, which the register reply now does. A prop set on
the control afterwards (snack_bar.props["open"] = true), before the app
block's first view patch, was compared only against the container's own
state, so it was never sent and then recorded as sent. The check now walks
the container's subtree. On main the first view patch carried the whole
container, so the client got that change.

Tests: the snack bar, dialog and bottom sheet opened after their setter,
an overlay control changed after overlay=, the real Rails LocalServer
case, and that only the first view patch flushes.
@AdamMusa

Copy link
Copy Markdown
Owner Author

Plants on head 0d29af4: each part of the fix put back on its own in a scratch copy, with the new and changed tests run against it (ruflet_core/test/page_internal_containers_test.rb, ruflet_server/test/reconnect_containers_test.rb, and under bundle exec ruflet_rails/test/local_server_reconnect_test.rb and session_registry_test.rb).

Plant Failing tests
Q1 container-local check only (previous head) 3: test_an_overlay_control_changed_before_the_first_view_patch_is_sent_as_changed, test_a_change_inside_a_container_before_the_first_view_patch_still_reaches_the_client, test_a_snack_bar_opened_after_snack_bar_assignment_in_the_app_block_reaches_the_client
Q2 flush window never closes (review P5) 1: test_only_the_first_view_patch_flushes_unsent_container_changes
Q3 no pre-first-view flush 4: test_a_change_inside_a_container_before_the_first_view_patch_still_reaches_the_client, test_an_overlay_control_changed_before_the_first_view_patch_is_sent_as_changed, test_container_content_set_before_the_first_view_patch_still_reaches_the_client, test_a_snack_bar_opened_after_snack_bar_assignment_in_the_app_block_reaches_the_client
P1 fixed ids removed 14 tests
P2 register reply without containers 6 tests
P3 no pre-first-view flush 4: test_an_overlay_control_changed_before_the_first_view_patch_is_sent_as_changed, test_container_content_set_before_the_first_view_patch_still_reaches_the_client, test_a_change_inside_a_container_before_the_first_view_patch_still_reaches_the_client, test_a_snack_bar_opened_after_snack_bar_assignment_in_the_app_block_reaches_the_client
P4 show_dialog checks wire_id 1: test_show_dialog_on_a_page_whose_containers_are_not_mounted_sends_the_page_first
P5 flush window never closes 1: test_only_the_first_view_patch_flushes_unsent_container_changes
P6 containers not marked mounted by register_containers_patch 12 tests
P7 containers patch carries the whole page 2: test_register_containers_patch_carries_only_the_containers_and_leaves_the_views_to_the_next_patch, test_rails_register_reply_carries_the_containers_and_no_patch_replaces_them
P8 register reply after app block (buffering order) 4: test_rails_first_paint_stays_progressive, test_rails_register_reply_carries_the_containers_and_no_patch_replaces_them, test_a_snack_bar_opened_after_snack_bar_assignment_in_the_app_block_reaches_the_client, test_a_fresh_page_after_the_old_socket_closed_names_the_same_containers
P9 prepare_for_reconnect keeps mounted flags none
P10 dialogs id collides with view id 20 15 tests
P11 flush order: containers before view patch none

Q1 is this round's fix put back (the check compares only the container's own state, as on 8ad4f20). Q2/P5 is the non-blocking finding: the flush window that never closes is now caught. P9 and P11 are equivalent on this head: prepare_for_reconnect! has one caller, ConnectionProtocol, which calls register_containers_patch right after it, and that sets the same flags; P11 sends the same messages in the same order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant