Raxol.Core.Events.EventManager (Raxol v2.0.1)

View Source

Event management system that wraps :telemetry for backward compatibility.

This module provides a compatibility layer while migrating from a custom event system to the standard :telemetry library. New code should use :telemetry directly.

Migration Status

This module now delegates to :telemetry internally. The GenServer functionality is maintained for backward compatibility but will be deprecated in a future version.

Summary

Functions

Returns a specification to start this module under a supervisor.

Cleans up the event manager and all resources.

Clears all registered handlers.

Dispatches an event using :telemetry.

Gets all registered handlers. Returns a list of handler entries.

Initializes the event manager state.

Notifies all registered handlers of an event.

Registers a handler for specific event types.

Subscribes to event streams with optional filtering.

Unregisters a handler for specific event types.

Unsubscribes from event streams.

Types

event_data()

@type event_data() :: map()

event_type()

@type event_type() :: atom()

filter_opts()

@type filter_opts() :: keyword()

handler_fun()

@type handler_fun() :: atom() | {module(), atom()} | function()

subscription_ref()

@type subscription_ref() :: reference()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

cleanup()

@spec cleanup() :: :ok

Cleans up the event manager and all resources.

clear_handlers()

@spec clear_handlers() :: :ok

Clears all registered handlers.

dispatch(event_type)

@spec dispatch(
  {event_type(), event_data()}
  | {event_type(), term(), term()}
  | event_type()
) :: :ok

dispatch(event_type, event_data)

Dispatches an event using :telemetry.

This method now delegates to telemetry for event dispatching while maintaining backward compatibility with the old API.

get_handlers()

@spec get_handlers() :: list()

Gets all registered handlers. Returns a list of handler entries.

init()

@spec init() :: :ok

Initializes the event manager state.

notify(manager_pid \\ __MODULE__, event_type, event_data)

@spec notify(GenServer.server(), event_type(), event_data()) :: :ok

Notifies all registered handlers of an event.

register_handler(event_types, target, handler)

@spec register_handler(
  event_type() | [event_type()],
  pid() | module(),
  handler_fun()
) :: :ok

Registers a handler for specific event types.

Examples

register_handler(:keyboard, MyModule, :handle_keyboard)
register_handler([:mouse, :touch], self(), :handle_input)

start_link(init_opts \\ [])

subscribe(event_types, opts \\ [])

@spec subscribe([event_type()], filter_opts()) ::
  {:ok, subscription_ref()} | {:error, term()}

Subscribes to event streams with optional filtering.

Examples

{:ok, ref} = subscribe([:keyboard, :mouse])
{:ok, ref} = subscribe([:focus], filter: [component_id: "main"])

unregister_handler(event_types, target, handler)

@spec unregister_handler(
  event_type() | [event_type()],
  pid() | module(),
  handler_fun()
) :: :ok

Unregisters a handler for specific event types.

unsubscribe(ref)

@spec unsubscribe(subscription_ref()) :: :ok

Unsubscribes from event streams.