Skip to content

fix: give select events their value, run zero-parameter handlers, add Flet's confirm, drawer and focus methods; Flashlight#on(event) registers a handler - #150

Open
AdamMusa wants to merge 4 commits into
mainfrom
audit/event-value-handler-arity
Open

AdamMusa wants to merge 4 commits into
mainfrom
audit/event-value-handler-arity

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #149

Only ruflet_core changes. There are no engine, template, wire-format or CLI changes.

What changed

1. A select event's value is its payload (event.rb)

  • Event#value now returns the payload when typed_data is an AutoCompleteSelectEvent and the payload is not a Map. The rule is the same one GenericEvent uses.
  • The scalar payloads are a Dropdown's key, a Chip's bool and a PopupMenuButton's item id, on both the Material and Cupertino renderers.
  • AutoComplete and ContextMenu send Maps, and nothing changes for them.
  • typed_data is left alone on purpose. Page#apply_event_value_to_control writes a select's typed value into props. Changing AutoCompleteSelectEvent.from_data would add a "selected" prop to PopupMenuButton, which later patches would send. It would also turn an Integer-key Dropdown's synced props["value"] "2" into 2.

2. Handlers with no parameters run (control.rb, page.rb, platform_control_contracts.rb)

  • New Ruflet::Control.call_handler(handler, event). It calls a lambda or Method of arity 0 without the event, as Flet does.
  • Control#emit, Page#dispatch_page_event and the Dropdown Material 2 on_change wrapper use it.
  • Every other handler still gets call(event): lambdas with parameters, optional or splat parameters, procs, blocks, Methods with parameters and callable objects.

3. Flashlight#on (current_service_methods.rb)

  • With an event name, it is Control#on: flashlight.on(:error) { |e| ... }. Without a block it does what Control#on does on every control: it sets on_error, sends one patch and registers no handler.
  • Without an event name, it turns the light on as before. This covers flashlight.on, flashlight.on(timeout:, on_result:) and flashlight.on { }, where the block is still ignored.
  • Passing an event name together with timeout: or on_result: raises ArgumentError. That call raised before too.

4. Flet's methods for View, Dismissible, Pagelet and KeyboardListener

  • view.confirm_pop(true) or view.confirm_pop(should_pop: true) sends {"should_pop" => bool}. The default is true.
  • dismissible.confirm_dismiss(true) or dismissible.confirm_dismiss(dismiss: true) sends {"dismiss" => bool}. The default is true.
  • The keywords are Flet's names for these arguments. If both forms are given, the keyword wins.
  • show_drawer, close_drawer, show_end_drawer and close_end_drawer on View and Pagelet come from a new DrawerMethods module. Like page.show_drawer, they take no positional argument.
  • keyboard_listener.focus comes from FocusableMethods.
  • Each one takes timeout: and on_result: and invokes the client method that view.dart, dismissible.dart, pagelet.dart and keyboard_listener.dart already handle, in both the engine and the template.
  • On an unmounted control, each returns nil and sends nothing.
  • There are specs for every new method in test/support/method_contracts, so MethodContractCoverageTest covers them.

Behavior changes (D6: all are bug fixes)

Call Before After
e.value in on_select of a Dropdown, dropdown_m2(on_change:), Chip or PopupMenuButton nil the picked key, bool or item id, the same value as e.data
on_click: -> { ... }, method(:no_args), page.on_route_change = -> { ... }, flashlight(on_error: -> { ... }) ArgumentError on every event runs without the event
flashlight.on(:error) { ... } ArgumentError registers the handler
flashlight.on(:error) with no block ArgumentError what Control#on does on every control: returns the flashlight, sets on_error and sends one patch; no handler runs
view.confirm_pop, dismissible.confirm_dismiss, view.show_drawer, pagelet.show_drawer (and the close and end variants), keyboard_listener.focus NoMethodError invokes the client method
the same calls with arguments the new method takes: view.confirm_pop(false), view.confirm_pop(should_pop: false), dismissible.confirm_dismiss(false), dismissible.confirm_dismiss(dismiss: false), keyboard_listener.focus(timeout: 5) sent nothing; Kernel#method_missing built an unused generic control (type "confirm_pop", ...), so an on_confirm_pop or on_confirm_dismiss answered this way waited 5 minutes and then kept the view or item invokes the client method
the same calls with arguments the new method does not take: view.show_drawer(drawer), pagelet.show_drawer(drawer), keyboard_listener.focus(true), view.confirm_pop(true, false), an unknown keyword such as view.confirm_pop(pop: true) sent nothing; built the same unused generic control ArgumentError naming the argument; still sends nothing. page.show_drawer(drawer) already raises this way. The call never reached the client before, so it now fails loudly instead of doing nothing

The following are unchanged:

  • e.data, typed_data, event names and the wire;
  • the props the client syncs, and every patch;
  • every handler that works today;
  • flashlight.on and flashlight.on(timeout:, on_result:);
  • page.invoke(e.control, "confirm_pop", args: ...), the documented workaround, which still works.

How it was verified

All runs used Ruby 4.0.5 on this branch (2908091), which merges main 51db9a6.

New tests (fail on main, pass here):

  • select_event_value_test.rb (7 tests) replays the client's real order and payload shapes. It also asserts that the synced props stay the same and that no "selected" prop reaches a later patch.
  • event_handler_arity_test.rb (7) covers 0-parameter lambdas and Methods on controls, page handlers, services and the Material 2 wrapper. It also checks that 1-parameter, optional and splat lambdas, procs, blocks, 1-parameter and optional Methods and callable objects still get the event.
  • flashlight_on_test.rb (6) and confirm_and_drawer_methods_test.rb (7). These include the keyword forms and flashlight.on(:error) with no block.
  • 19 new method-contract cases (control_method_contract_test 138 runs on main, 157 here).

Bug planted back: I restored each fixed file from main in a copy of this tree, one fix at a time, and ran its test:

Restored from main Result
event.rb 5 of 7 select tests fail
control.rb, page.rb and the Material 2 wrapper 6 of 7 arity tests fail
only the page dispatch line and the wrapper the page-handler test and the Material 2 test fail
current_service_methods.rb 3 of 6 flashlight tests fail
the four control files 7 of 7 method tests fail
view_control.rb and dismissible_control.rb from the previous head (positional only) the keyword test and 4 contract cases fail with unknown keyword

Suites (one process per test file):

Suite Result
ruflet_core (216 files) 2493 runs, 0 failures, 0 errors, 1 skip
ruflet_server (19 files) 232 runs, 0 failures
ruflet CLI (28 files) 902 runs, 0 failures
ruflet_rails (21 files, bundle exec) 315 runs, 0 failures, 2 skips

The core skip is the existing RUFLET_ENGINE_ROOT check in dsl_presented_controls_test. The rails skips are the existing ones in rails_executor_test.

H1 goldens: cd packages/ruflet && ruby test/compat/compat.rb --jobs 4 checked 359 goldens with no diff. compat_goldens_test.rb: 367 runs, 0 failures.

mruby:

  • I built the host VM (mruby 449627d with Ruflet's patches, host_vm.rb) from this tree. A script that drives Page through select values, 0-parameter lambdas, procs and Methods, the Material 2 wrapper, on_route_change, every form of flashlight.on and the new methods with their keyword forms prints ALL OK under it. Under a host VM built from main, the same script fails (select values are nil, then ArgumentError).
  • The embedded-harness tests (tools/embedded_vm_harness/tests) give the same output under host VMs built from main and from this branch.
  • I did not rebuild the committed VM artifacts. Self-contained builds pick this change up when the VM is rebuilt for a release, like the other ruflet_core fixes.

Docs (ruflet_site, after the next release)

  • app/content/docs/control_dropdown.md and config/property_descriptions.json (Dropdown on_select) say event.value is nil for this event.
  • control_dismissible.md documents the page.invoke reply, which event.control.confirm_dismiss(true) would replace.

… Flet's confirm, drawer and focus methods

- Event#value: a select whose payload is not a Map (a Dropdown key, a
  Chip's bool, a PopupMenuButton item id) has that payload as its value,
  as e.data already does. typed_data is unchanged, so the props the client
  synced and later patches are too.
- Ruflet::Control.call_handler calls a lambda or Method of arity 0
  without the event, as Flet does; Control#emit, Page#dispatch_page_event
  and the Dropdown Material 2 wrapper use it. Every other handler still
  gets the event.
- Flashlight#on(event_name) { } is Control#on; without an event name it
  still turns the light on.
- view.confirm_pop, dismissible.confirm_dismiss, show_drawer,
  close_drawer, show_end_drawer and close_end_drawer on View and Pagelet,
  and keyboard_listener.focus invoke the client methods the renderers
  already handle.

Fixes #149
…onfirm_dismiss

Flet calls View.confirm_pop(should_pop=...) and
Dismissible.confirm_dismiss(dismiss=...) by keyword. The positional form
keeps working; the keyword wins when both are given. Also pin that
flashlight.on(:error) without a block is Control#on.
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