AshDispatch.EventRegistry (AshDispatch v0.5.1)

View Source

Auto-discovery registry for event modules.

This module eliminates the need for manual event_modules configuration by automatically discovering events from Ash domains at runtime.

How It Works

  1. Scans all configured Ash domains for the given otp_app
  2. Finds resources with AshDispatch.Resource extension
  3. Reads event definitions from the dispatch DSL section
  4. Returns event information including any associated modules

Usage

Instead of manually configuring:

# OLD - Manual config (no longer needed!)
config :ash_dispatch, :event_modules, [
  {"order.created", MyApp.Events.OrderCreated},
  {"ticket.assigned", MyApp.Events.TicketAssigned}
]

Events are now auto-discovered:

# Get all event modules (for backward compatibility)
EventRegistry.get_event_modules()

# Find a specific event
{:ok, event} = EventRegistry.find_event("order.created")

# Find module for an event
{:ok, module} = EventRegistry.find_module("order.created")

Configuration

The registry uses the :otp_app config to know which app's domains to scan:

config :ash_dispatch, :otp_app, :my_app

And the app must have domains configured:

config :my_app, :ash_domains, [MyApp.Orders, MyApp.Tickets]

Summary

Functions

Get all events from all resources in the configured domains.

Get all event modules for a specific otp_app.

Find an event by its event_id.

Find the module for an event by its event_id.

Get all event modules in the format [{event_id, module}, ...].

Functions

all_events(otp_app)

@spec all_events(atom()) :: [map()]

Get all events from all resources in the configured domains.

Returns a list of event info maps with:

  • :event_id - The event identifier (e.g., "order.created")
  • :name - The event name atom (e.g., :created)
  • :module - The event module (if any)
  • :resource - The resource module that defines this event
  • :channels - Channel configurations from DSL
  • :content - Content configuration from DSL
  • :metadata - Metadata configuration from DSL

event_modules(otp_app)

@spec event_modules(atom()) :: [{String.t(), module()}]

Get all event modules for a specific otp_app.

Returns a list of {event_id, module} tuples for events that have modules. Events without modules (pure inline DSL) are excluded.

find_event(event_id, otp_app \\ nil)

@spec find_event(String.t(), atom() | nil) :: {:ok, map()} | {:error, :not_found}

Find an event by its event_id.

Returns {:ok, event_info} or {:error, :not_found}.

find_module(event_id, otp_app \\ nil)

@spec find_module(String.t(), atom() | nil) ::
  {:ok, module()} | {:error, :not_found | :no_module}

Find the module for an event by its event_id.

Returns {:ok, module}, {:error, :no_module} if event exists but has no module, or {:error, :not_found} if event doesn't exist.

get_event_modules()

@spec get_event_modules() :: [{String.t(), module()}]

Get all event modules in the format [{event_id, module}, ...].

This provides backward compatibility with the old :event_modules config format. Only returns events that have an associated module (explicit or generated).

Uses the configured :otp_app to determine which domains to scan.