Raxol.Protocols.EventHandler protocol (Raxol v2.0.1)
View SourceProtocol 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
Functions
Determines if the handler can handle a specific event.
Gets the list of event types this handler listens to.
Handles an event.
Subscribes to specific event types.
Unsubscribes from specific event types.
Types
Functions
Determines if the handler can handle a specific event.
Parameters
handler- The handler to checkevent- The event to check
Returns
true if the handler can handle the event, false otherwise.
Gets the list of event types this handler listens to.
Returns
A list of event type atoms.
@spec handle_event(t(), event(), any()) :: handler_result()
Handles an event.
Parameters
handler- The handler receiving the eventevent- The event to handlestate- 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
Subscribes to specific event types.
Parameters
handler- The handlerevent_types- List of event types to subscribe to
Returns
The updated handler.
Unsubscribes from specific event types.
Parameters
handler- The handlerevent_types- List of event types to unsubscribe from
Returns
The updated handler.