Raxol.Core.Events.Manager (Raxol v0.4.0)
View SourceEvent manager for handling and dispatching events in Raxol applications.
This module provides a simple event system that allows:
- Registering event handlers
- Dispatching events
- Handling events
- Managing subscriptions
Usage
# Initialize the event manager
EventManager.init()
# Register an event handler
EventManager.register_handler(:click, MyModule, :handle_click)
# Subscribe to events
{:ok, ref} = EventManager.subscribe([:click, :key_press])
# Dispatch an event
EventManager.dispatch({:click, %{x: 10, y: 20}})
# Unsubscribe
EventManager.unsubscribe(ref)
Summary
Functions
Broadcast an event to all subscribers.
Cleans up the event manager state.
Clear all event handlers.
Dispatch an event to all registered handlers and subscribers.
Get all registered event handlers.
Initialize the event manager.
Register an event handler.
Subscribe to events with optional filters.
Triggers an event with a type and payload (alias for dispatch/1). This is provided for API compatibility; use dispatch/1 for generic events.
Unregister an event handler.
Unsubscribe from events using the subscription reference.
Functions
Broadcast an event to all subscribers.
Examples
iex> EventManager.broadcast({:system_event, :shutdown})
:ok
Cleans up the event manager state.
Clear all event handlers.
Examples
iex> EventManager.clear_handlers()
:ok
Dispatch an event to all registered handlers and subscribers.
Parameters
event
- The event to dispatch
Examples
iex> EventManager.dispatch({:click, %{x: 10, y: 20}})
:ok
iex> EventManager.dispatch(:accessibility_high_contrast)
:ok
Get all registered event handlers.
Examples
iex> EventManager.get_handlers()
%{click: [{MyModule, :handle_click}]}
Initialize the event manager.
Examples
iex> EventManager.init()
:ok
Register an event handler.
Parameters
event_type
- The type of event to handlemodule
- The module containing the handler functionfunction
- The function to call when the event occurs
Examples
iex> EventManager.register_handler(:click, MyModule, :handle_click)
:ok
Subscribe to events with optional filters.
Parameters
event_types
- List of event types to subscribe toopts
- Optional filters and options
Examples
iex> EventManager.subscribe([:click, :key_press])
{:ok, ref}
iex> EventManager.subscribe([:mouse], button: :left)
{:ok, ref}
Triggers an event with a type and payload (alias for dispatch/1). This is provided for API compatibility; use dispatch/1 for generic events.
Unregister an event handler.
Parameters
event_type
- The type of eventmodule
- The module containing the handler functionfunction
- The function that was registered
Examples
iex> EventManager.unregister_handler(:click, MyModule, :handle_click)
:ok
Unsubscribe from events using the subscription reference.
Examples
iex> EventManager.unsubscribe(ref)
:ok