A2UI.Live.EventHandler (A2UI v0.3.0)

Copy Markdown View Source

Pure functions for handling Phoenix LiveView events in A2UI.

Converts phx-click and phx-change event params into protocol-level actions and data model updates. No socket state — fully unit-testable.

Summary

Functions

Applies an a2ui_input_change event to the surfaces map.

Builds an Action struct from a2ui_action event params.

Builds an Error struct from a2ui_error event params.

Functions

apply_input_change(params, surfaces)

@spec apply_input_change(map(), A2UI.SurfaceManager.surfaces()) ::
  {:ok, A2UI.SurfaceManager.surfaces()} | {:error, atom()}

Applies an a2ui_input_change event to the surfaces map.

Extracts the input value from params, coerces it to match the existing data model type at the target path, and applies the update.

Returns {:ok, updated_surfaces} or {:error, reason}.

build_action(params, surfaces)

@spec build_action(map(), A2UI.SurfaceManager.surfaces()) ::
  {:ok, A2UI.Protocol.Messages.Action.t(), map()} | {:error, atom()}

Builds an Action struct from a2ui_action event params.

Returns {:ok, action, metadata} or {:error, reason}.

The metadata map includes:

  • :surface — the surface struct
  • :send_data_model — whether the surface wants data model echoed back
  • :data_model — the data model data (if send_data_model is true)

build_error(params)

@spec build_error(map()) ::
  {:ok, A2UI.Protocol.Messages.Error.t(), map()} | {:error, atom()}

Builds an Error struct from a2ui_error event params.

Returns {:ok, error, metadata} or {:error, reason}.