Raxol.Core.Runtime.ComponentManager (Raxol v2.9.2)

View Source

Refactored component manager with functional error handling patterns.

This module eliminates all try/rescue blocks and uses pure functional error handling with with statements and safe wrapper functions.

Key improvements:

  • 3 try/rescue blocks eliminated
  • Functional error composition with Task-based safety
  • Safe component operation wrappers
  • Proper error telemetry integration
  • Full backward compatibility maintained

update/2 returns

For every message the manager passes to a component's update/2 (a ComponentManager.update/2 call, a {:broadcast, message} from another component, a {:schedule, message, delay} timer, or a subscribed event), update/2 may return new_state, {:ok, new_state} or {new_state, commands}. Any other return leaves the component's state unchanged: a ComponentManager.update/2 call returns {:error, :invalid_component_return}, and for the other messages a warning names the component and the kind of message, not its data. Returned commands are processed for ComponentManager.update/2 calls and delivered events only.

Event subscriptions

A component subscribes to events dispatched through Raxol.Core.Events.EventManager (EventManager.dispatch/1,2 or EventManager.notify/3) by returning a command from mount/1, handle_event/3, or update/2 (for a ComponentManager.update/2 call or a delivered event):

  • {:command, {:subscribe, event_types}} adds the component to each listed type.
  • {:command, {:unsubscribe, event_types}} removes it from each listed type. Unmounting a component removes it from every type.

Each matching event reaches the component's update/2 as {:event, event_type, event_data}, through the same path as ComponentManager.update/2: the commands it returns are processed, and the component is queued for render if its state changed. If update/2 raises or returns anything else, the component keeps its state, a warning names the component and the event type (not the event data), and the other subscribers still get the event. A component receives an event once, however often it subscribed to the type; subscribing again changes nothing, and one unsubscribe removes it.

The manager holds one EventManager subscription per event type, shared by every component listing the type: it is started when the first component subscribes and stopped when the last one leaves. An {:event, _, _} message for a type no component lists is dropped.

If starting a type's EventManager subscription fails when its first component subscribes (EventManager not running, not answering in time, or stopping during the call), a warning is logged and the component is left unsubscribed from that type; it can subscribe again once EventManager is back. Delivery stops if EventManager restarts: the subscriptions lived in the stopped EventManager and are not re-established with the new one, and since the manager still counts them as running, subscribing again does not renew them until every component listing the type has left it.

EventManager is node-global: a subscribed component receives every event of the type dispatched anywhere on the node, whichever session or process dispatched it.

Summary

Functions

Returns a specification to start this module under a supervisor.

Retrieves all components' data.

Retrieves a specific component's data by its ID.

Retrieves the current render queue and clears it.

Directly sets the state for a component (for testing purposes).

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

dispatch_event(event)

get_all_components()

@spec get_all_components() :: map()

Retrieves all components' data.

get_component(component_id)

@spec get_component(String.t()) :: map() | nil

Retrieves a specific component's data by its ID.

get_render_queue()

@spec get_render_queue() :: [String.t()]

Retrieves the current render queue and clears it.

mount(component_module, props \\ %{})

set_component_state(component_id, new_state)

@spec set_component_state(String.t(), map()) :: :ok | {:error, :not_found}

Directly sets the state for a component (for testing purposes).

set_runtime_pid(pid)

start_link(init_opts \\ [])

unmount(component_id)

update(component_id, message)