TermUI.ComponentRegistry (TermUI v0.2.0)

View Source

ETS-based registry for component lookup.

The registry enables fast lookup of component processes by id, which is essential for event routing and focus management. Components register on mount and unregister on unmount.

Usage

# Register a component
ComponentRegistry.register(:my_button, pid, Button)

# Lookup by id
{:ok, pid} = ComponentRegistry.lookup(:my_button)

# Lookup by pid
{:ok, id} = ComponentRegistry.lookup_id(pid)

# List all
components = ComponentRegistry.list_all()

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears all registrations.

Returns the count of registered components.

Gets all children of a component.

Gets full component info by id.

Gets the parent of a component.

Lists all registered components.

Looks up a component by id.

Looks up a component id by pid.

Registers a component in the registry.

Checks if a component is registered.

Sets the parent of a component for propagation.

Starts the component registry.

Unregisters a component from the registry.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Clears all registrations.

Mainly useful for testing.

count()

@spec count() :: non_neg_integer()

Returns the count of registered components.

get_children(parent_id)

@spec get_children(term()) :: [term()]

Gets all children of a component.

Returns

List of child component ids.

get_info(id)

@spec get_info(term()) :: {:ok, map()} | {:error, :not_found}

Gets full component info by id.

Returns

  • {:ok, %{id: term(), pid: pid(), module: module()}} - Component found
  • {:error, :not_found} - Component not registered

get_parent(id)

@spec get_parent(term()) :: {:ok, term() | nil} | {:error, :not_found}

Gets the parent of a component.

Returns

  • {:ok, parent_id} - Parent found (nil if root)
  • {:error, :not_found} - Component not in parent table

list_all()

@spec list_all() :: [map()]

Lists all registered components.

Returns

List of %{id: term(), pid: pid(), module: module()}

lookup(id)

@spec lookup(term()) :: {:ok, pid()} | {:error, :not_found}

Looks up a component by id.

Returns

  • {:ok, pid} - Component found
  • {:error, :not_found} - Component not registered

lookup_id(pid)

@spec lookup_id(pid()) :: {:ok, term()} | {:error, :not_found}

Looks up a component id by pid.

Returns

  • {:ok, id} - Component found
  • {:error, :not_found} - Component not registered

register(id, pid, module)

@spec register(term(), pid(), module()) :: :ok | {:error, :already_registered}

Registers a component in the registry.

Parameters

  • id - Unique identifier for the component
  • pid - Process pid of the component
  • module - Component module

Returns

  • :ok - Successfully registered
  • {:error, :already_registered} - Id already taken

registered?(id)

@spec registered?(term()) :: boolean()

Checks if a component is registered.

set_parent(id, parent_id)

@spec set_parent(term(), term() | nil) :: :ok

Sets the parent of a component for propagation.

Parameters

  • id - Component id
  • parent_id - Parent component id (or nil for root)

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the component registry.

unregister(id)

@spec unregister(term()) :: :ok

Unregisters a component from the registry.

Parameters

  • id - Component identifier to unregister

Returns

  • :ok - Successfully unregistered (or wasn't registered)