Jido.Signal.Ext.Registry (Jido Signal v1.2.0)

View Source

Compile-time registry for Signal extensions.

This module provides a centralized registry for all Signal extensions, enabling runtime lookup and validation. Extensions are automatically registered during compilation through the @after_compile hook in the Jido.Signal.Ext behavior.

Overview

The registry maintains a mapping between extension namespaces and their implementing modules. This allows the Signal system to:

  • Validate extension data at runtime
  • Look up extension modules by namespace
  • Enumerate all available extensions
  • Handle extension serialization/deserialization

Storage Mechanism

The registry uses an Agent for thread-safe storage during development and testing. In production, this could be backed by ETS for better performance with many concurrent readers.

Registration Process

Extensions are automatically registered when they are compiled:

defmodule MyApp.Auth do
  use Jido.Signal.Ext,
    namespace: "auth",
    schema: [user_id: [type: :string, required: true]]
end

# After compilation, the extension is automatically available:
{:ok, MyApp.Auth} = Jido.Signal.Ext.Registry.get("auth")

Thread Safety

All registry operations are thread-safe and can be called concurrently from multiple processes without coordination.

Error Handling

The registry uses standard Elixir conventions:

  • Returns {:ok, result} for successful operations
  • Returns {:error, reason} for failures
  • Provides bang versions that raise on errors

Examples

# Look up an extension by namespace
case Jido.Signal.Ext.Registry.get("auth") do
  {:ok, module} -> module.validate_data(auth_data)
  {:error, :not_found} -> handle_unknown_extension()
end

# Get all registered extensions
extensions = Jido.Signal.Ext.Registry.all()
IO.puts("Found #{length(extensions)} extensions")

# Check if an extension is registered
if Jido.Signal.Ext.Registry.get!("tracking") do
  apply_tracking_extension()
end

Summary

Functions

Returns all registered extensions.

Returns a specification to start this module under a supervisor.

Returns the count of registered extensions.

Looks up an extension module by namespace.

Looks up an extension module by namespace, raising if not found.

Registers an extension module with its namespace.

Checks if an extension is registered for the given namespace.

Starts the extension registry.

Functions

all()

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

Returns all registered extensions.

Returns

A list of {namespace, module} tuples for all registered extensions

Examples

extensions = Jido.Signal.Ext.Registry.all()

Enum.each(extensions, fn {namespace, module} ->
  IO.puts("Extension #{namespace}: #{module}")
end)

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count()

@spec count() :: non_neg_integer()

Returns the count of registered extensions.

Examples

count = Jido.Signal.Ext.Registry.count()
IO.puts("#{count} extensions registered")

get(namespace)

@spec get(String.t()) :: {:ok, module()} | {:error, :not_found}

Looks up an extension module by namespace.

Parameters

  • namespace - The extension namespace string

Returns

{:ok, module} if found, {:error, :not_found} otherwise

Examples

case Jido.Signal.Ext.Registry.get("auth") do
  {:ok, AuthExt} ->
    {:ok, data} = AuthExt.validate_data(%{user_id: "123"})
  {:error, :not_found} ->
    {:error, "Unknown extension: auth"}
end

get!(namespace)

@spec get!(String.t()) :: module() | no_return()

Looks up an extension module by namespace, raising if not found.

Parameters

  • namespace - The extension namespace string

Returns

The extension module

Raises

ArgumentError if the extension is not found

Examples

module = Jido.Signal.Ext.Registry.get!("auth")
{:ok, data} = module.validate_data(%{user_id: "123"})

register(module)

@spec register(module()) :: :ok

Registers an extension module with its namespace.

This function is typically called automatically by the @after_compile hook in extension modules and doesn't need to be called manually.

Parameters

  • module - The extension module to register

Returns

:ok if registration succeeds, :ok if registry is not available

Examples

# Automatic registration (preferred)
defmodule MyExt do
  use Jido.Signal.Ext, namespace: "my_ext"
end

# Manual registration (not typically needed)
Jido.Signal.Ext.Registry.register(MyExt)

registered?(namespace)

@spec registered?(String.t()) :: boolean()

Checks if an extension is registered for the given namespace.

Parameters

  • namespace - The extension namespace string

Returns

true if registered, false otherwise

Examples

if Jido.Signal.Ext.Registry.registered?("auth") do
  # Use auth extension
else
  # Handle missing extension
end

start_link(opts \\ [])

Starts the extension registry.

This is typically called by the application supervision tree and doesn't need to be called manually.