Raxol.Core.Events.Manager (Raxol v0.2.0)

View Source

Event 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.

Subscribe to events with optional filters.

Unsubscribe from events using the subscription reference.

Functions

broadcast(event)

Broadcast an event to all subscribers.

Examples

iex> EventManager.broadcast({:system_event, :shutdown})
:ok

cleanup()

Cleans up the event manager state.

clear_handlers()

Clear all event handlers.

Examples

iex> EventManager.clear_handlers()
:ok

dispatch(event)

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_handlers()

Get all registered event handlers.

Examples

iex> EventManager.get_handlers()
%{click: [{MyModule, :handle_click}]}

init()

Initialize the event manager.

Examples

iex> EventManager.init()
:ok

register_handler(event_type, module, function)

Register an event handler.

Parameters

  • event_type - The type of event to handle
  • module - The module containing the handler function
  • function - The function to call when the event occurs

Examples

iex> EventManager.register_handler(:click, MyModule, :handle_click)
:ok

subscribe(event_types, opts \\ [])

Subscribe to events with optional filters.

Parameters

  • event_types - List of event types to subscribe to
  • opts - Optional filters and options

Examples

iex> EventManager.subscribe([:click, :key_press])
{:ok, ref}

iex> EventManager.subscribe([:mouse], button: :left)
{:ok, ref}

unregister_handler(event_type, module, function)

Unregister an event handler.

Parameters

  • event_type - The type of event
  • module - The module containing the handler function
  • function - The function that was registered

Examples

iex> EventManager.unregister_handler(:click, MyModule, :handle_click)
:ok

unsubscribe(ref)

Unsubscribe from events using the subscription reference.

Examples

iex> EventManager.unsubscribe(ref)
:ok