Raxol.Protocols.EventHandler protocol (Raxol v2.0.1)

View Source

Protocol for handling events in a polymorphic way.

This protocol provides a unified interface for different types of components to handle events. It supports event filtering, handling, and bubbling.

Event Structure

Events are maps with at least the following keys:

  • :type - The event type (atom)
  • :target - The target of the event
  • :timestamp - When the event occurred
  • :data - Event-specific data

Examples

defimpl Raxol.Protocols.EventHandler, for: MyComponent do
  def handle_event(component, %{type: :click} = event, state) do
    # Handle click event
    {:ok, updated_component, new_state}
  end

  def handle_event(component, _event, state) do
    # Ignore other events
    {:unhandled, component, state}
  end

  def can_handle?(component, %{type: type}) do
    type in [:click, :keypress, :focus]
  end

  def get_event_listeners(component) do
    [:click, :keypress, :focus]
  end
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Determines if the handler can handle a specific event.

Gets the list of event types this handler listens to.

Subscribes to specific event types.

Unsubscribes from specific event types.

Types

event()

@type event() :: %{type: atom(), target: any(), timestamp: integer(), data: map()}

handler_result()

@type handler_result() ::
  {:ok, t(), any()}
  | {:error, term()}
  | {:unhandled, t(), any()}
  | {:stop, t(), any()}
  | {:bubble, t(), any()}

t()

@type t() :: term()

All the types that implement this protocol.

Functions

can_handle?(handler, event)

@spec can_handle?(t(), event()) :: boolean()

Determines if the handler can handle a specific event.

Parameters

  • handler - The handler to check
  • event - The event to check

Returns

true if the handler can handle the event, false otherwise.

get_event_listeners(handler)

@spec get_event_listeners(t()) :: [atom()]

Gets the list of event types this handler listens to.

Returns

A list of event type atoms.

handle_event(handler, event, state)

@spec handle_event(t(), event(), any()) :: handler_result()

Handles an event.

Parameters

  • handler - The handler receiving the event
  • event - The event to handle
  • state - Current state

Returns

  • {:ok, updated_handler, new_state} - Event handled successfully
  • {:error, reason} - Error handling the event
  • {:unhandled, handler, state} - Event not handled
  • {:stop, handler, state} - Stop event propagation
  • {:bubble, handler, state} - Bubble event to parent

subscribe(handler, event_types)

@spec subscribe(t(), [atom()]) :: t()

Subscribes to specific event types.

Parameters

  • handler - The handler
  • event_types - List of event types to subscribe to

Returns

The updated handler.

unsubscribe(handler, event_types)

@spec unsubscribe(t(), [atom()]) :: t()

Unsubscribes from specific event types.

Parameters

  • handler - The handler
  • event_types - List of event types to unsubscribe from

Returns

The updated handler.