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

View Source

GenServer implementation for event management in Raxol applications.

This server provides a pure functional approach to event management using a PubSub pattern, eliminating Process dictionary usage and implementing proper OTP supervision patterns.

Features

  • Event handler registration with module/function callbacks
  • PubSub-style subscriptions with filters
  • Broadcast and targeted event dispatching
  • Supervised state management with fault tolerance
  • Support for priority handlers
  • Event history tracking (optional)

State Structure

The server maintains state with the following structure:

%{
  handlers: %{event_type => [{module, function, priority}]},
  subscriptions: %{ref => %{pid: pid, event_types: [], filters: [], monitor_ref: ref}},
  monitors: %{monitor_ref => subscription_ref},
  event_history: [], # Optional, configurable
  config: %{
    history_limit: 100,
    enable_history: false
  }
}

Event Dispatching

Events are dispatched in priority order (lower numbers = higher priority). Handlers with the same priority are executed in registration order.

Summary

Functions

Broadcasts an event to all subscribers regardless of filters.

Returns a specification to start this module under a supervisor.

Clears all event handlers.

Clears event history.

Clears all subscriptions.

Dispatches an event to all registered handlers and subscribers.

Dispatches an event synchronously, waiting for all handlers to complete.

Gets all registered event handlers.

Gets the current state (for debugging/testing).

Gets all active subscriptions.

Initializes the event manager (for backward compatibility).

Subscribes to events with optional filters.

Triggers an event with type and payload (compatibility alias).

Unsubscribes from events using the subscription reference.

Functions

broadcast(server \\ __MODULE__, event)

Broadcasts an event to all subscribers regardless of filters.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_handlers(server \\ __MODULE__)

Clears all event handlers.

clear_history(server \\ __MODULE__)

Clears event history.

clear_subscriptions(server \\ __MODULE__)

Clears all subscriptions.

dispatch(server \\ __MODULE__, event)

Dispatches an event to all registered handlers and subscribers.

This is an asynchronous operation - use dispatch_sync for synchronous dispatch.

dispatch_sync(server \\ __MODULE__, event)

Dispatches an event synchronously, waiting for all handlers to complete.

get_event_history(server \\ __MODULE__, limit \\ nil)

Gets event history (if enabled).

get_handlers(server \\ __MODULE__)

Gets all registered event handlers.

get_state(server \\ __MODULE__)

Gets the current state (for debugging/testing).

get_subscriptions(server \\ __MODULE__)

Gets all active subscriptions.

register_handler(server \\ __MODULE__, event_type, module_or_id, function_or_struct, opts \\ [])

Registers an event handler with optional priority.

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
  • opts - Options including:
    • :priority - Handler priority (default: 50, lower = higher priority)

reset_manager(server \\ __MODULE__)

Initializes the event manager (for backward compatibility).

start_link(init_opts \\ [])

subscribe(server \\ __MODULE__, event_types, opts \\ [])

Subscribes to events with optional filters.

Parameters

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

Returns

  • {:ok, ref} - Subscription reference for later unsubscribe

subscribe_pid(server \\ __MODULE__, pid, event_types, opts \\ [])

Subscribes a specific process to events.

trigger(server \\ __MODULE__, event_type, payload)

Triggers an event with type and payload (compatibility alias).

unregister_handler(server \\ __MODULE__, event_type, module, function)

Unregisters an event handler.

unsubscribe(server \\ __MODULE__, ref)

Unsubscribes from events using the subscription reference.