Skip to content

refactor: dispatch Page invoke methods through a handler table (WP-00 R4) - #103

Merged
AdamMusa merged 2 commits into
codex/engine-initializationfrom
refactor/page-invoke-registry
Nov 8, 2026
Merged

AdamMusa merged 2 commits into
codex/engine-initializationfrom
refactor/page-invoke-registry

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

What

WP-00 R4, engine half. The Page control (lib/src/controls/page.dart, _PageControlState) answered Ruby's invoke_control_method with one switch. It now dispatches through a name → handler map:

late final Map<String, _PageMethodHandler> _invokeHandlers = {
  "take_screenshot": _takeScreenshot,
  "push_route": _pushRoute,
  "get_device_info": _getDeviceInfo,
  "set_allowed_device_orientations": _setAllowedDeviceOrientations,
};

Each case body moved verbatim into its own handler. _invokeMethod still writes the Page.<name>(<args>) log line, looks up the handler, and throws Exception("Unknown Page method: <name>") when there is none.

Why

Wave 1 and wave 2 packages add Page methods: RT-04 take_screenshot sugar, RT-05 system UI, and the WP-18 image-cache methods. With a switch, every one of them edits the same lines. With the map, each new method is one map entry plus one function. The Ruby half (appendable page props, Page events and page methods) is AdamMusa/ruflet#74.

Proof of zero change

  • The bodies are unchanged. The diff only moves each case into a Future<dynamic> handler: break becomes return null and case becomes a method.
  • Timing is unchanged. A handler runs synchronously until its first await, as its case did. So push_route still moves the route before the reply is sent.
  • The unhandled-method fast-fail from fix: fail fast on invoke methods no handler claims #59 still holds. The Page always registers this listener, so a name with no handler is answered at once, never left waiting for a listener. The reply is still Exception: Unknown Page method: <name>. It arrives within one frame while the Ruby timeout (30 s in the test) is never advanced.
  • The new test passes on the old switch. test/widgets/page_invoke_methods_test.dart is its own first commit (935c9e87), and it passes on the switch-based code (10/10). It also passes unchanged on the refactor.
  • Full suite (Flutter 3.41.2, packages/ruflet):
    • flutter test: +2291: All tests passed! at 935c9e87 (old switch plus the new test) and +2291: All tests passed! at the head 3dc4a56c.
  • flutter analyze:
    • On the two touched files: No issues found!
    • On the whole package: identical output at both commits. There are 430 pre-existing info lints, all in vendor/flutter_math_fork and vendor/flutter_markdown_plus.
  • Mutation check. Dropping the push_route entry or throwing a different error type for unknown names fails 4 of the new tests.
  • No new Flutter API. Nothing beyond what the switch already used (RenderRepaintBoundary.toImage, SystemChrome.setPreferredOrientations).
  • Dependencies and binary size: no new dependency. The binary-size delta was not measured; the change is one map literal.
  • Ruby control surface: unchanged, so test/contract/ruflet_control_surface.json needs no update.

Behavior changes

None.

Tests

test/widgets/page_invoke_methods_test.dart (10 tests: 5 cases × Android and iOS designs). It drives a connected backend through a recording channel and asserts the exact invoke_control_method reply Ruby receives for each method:

  • Unhandled names: no_such_method, Push_Route (names match exactly), the empty name, and show_drawer (a View method, not a Page method). Each returns Exception: Unknown Page method: <name> within one frame. The log line Page.no_such_method({}) is still written.
  • push_route:
    • With nil, {} or route: nil, the reply is Invalid argument(s): Page.push_route requires a "route" argument.
    • With /next, the reply is result: null, error: null, the backend route changes, and the log line is written.
    • A non-string route is sent as its string.
  • get_device_info: returns the record and ignores extra args. A failing source gives Bad state: no record.
  • set_allowed_device_orientations:
    • Deduplicated orientations, [] and nil (all orientations) reach SystemChrome.setPreferredOrientations.
    • An unknown orientation gives Invalid argument (orientations): Page.set_allowed_device_orientations: unknown orientation: "sideways".
    • On macOS the reply is Unsupported operation: set_allowed_device_orientations is only supported on mobile platforms, and no platform call is made.
  • take_screenshot: returns null without enable_screenshots, and PNG bytes with it.

Not in this PR

Users get this only through a later ruflet-template sync (ruby tool/sync_ruflet_source.rb --source ruflet-engine --ref <sha>, X3).

Ruby reaches the Page control (wire id 1) through invoke(:page, ...),
page.get_device_info and page.set_allowed_device_orientations. Pin, on both
designs, the exact invoke_control_method reply for each method the Page
handles (push_route, get_device_info, set_allowed_device_orientations,
take_screenshot with and without enable_screenshots), the error replies for
missing or bad arguments and for desktop orientations, and the
"Exception: Unknown Page method: <name>" reply an unhandled name gets
within one frame instead of waiting for the Ruby timeout. Also pin the
"Page.<name>(<args>)" log line.

These pass on the current switch-based dispatch; the handler table that
replaces it must keep them green.
WP-00 R4. The Page control answered Ruby's invoke_control_method with one
switch, so every feature that adds a Page method (take_screenshot sugar,
image-cache methods, system UI) would edit the same lines. Replace it with
a name -> handler map: each method is its own handler and a new one is one
map entry plus its function.

Behavior is unchanged: the same "Page.<name>(<args>)" log line, the same
handlers (bodies moved verbatim), the same results and errors, and a name
with no handler still throws Exception("Unknown Page method: <name>") at
once, so Ruby gets the error reply immediately instead of waiting out its
timeout. Handlers run synchronously up to their first await, as the switch
cases did, so push_route still moves the route before the reply is sent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant