Voile.Hooks (Voile v0.1.31)

Copy Markdown View Source

Action and filter hook system for Voile plugins.

Uses :persistent_term for hook storage, providing zero-cost concurrent reads. Registration and unregistration go through a GenServer to serialize writes to :persistent_term.

Action Hooks

Plugins react to events. Return values are ignored.

Voile.Hooks.run_action(:visitor_checked_in, %{visitor_id: id})

Filter Hooks

Plugins transform data. Each handler receives the accumulated value and must return a possibly-modified version.

widgets = Voile.Hooks.run_filter(:dashboard_widgets, base_widgets)

Registering Handlers

Voile.Hooks.register(:dashboard_widgets, &MyPlugin.add_widget/1,
  owner: MyPlugin, priority: 30)

Unregistering (on plugin deactivation)

Voile.Hooks.unregister_all(MyPlugin)

Summary

Functions

Returns a specification to start this module under a supervisor.

Get all handlers for a specific hook (for debugging/inspection).

List all registered hook names.

Register a handler for a named hook.

Run action hooks. All handlers are called with payload. Return values are ignored. Errors in one handler do NOT stop others.

Run filter hooks. Handlers are called in priority order, each receiving the return value of the previous. The final accumulated value is returned.

Unregister ALL hooks registered by a specific plugin module.

Types

hook_entry()

@type hook_entry() :: %{handler: function(), owner: module(), priority: integer()}

hook_name()

@type hook_name() :: atom()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

list_handlers(hook_name)

Get all handlers for a specific hook (for debugging/inspection).

list_hooks()

List all registered hook names.

register(hook_name, handler, opts \\ [])

Register a handler for a named hook.

Options:

  • owner: (module) — used to unregister all hooks for a plugin at once
  • priority: (integer) — lower numbers run first, default 50

run_action(hook_name, payload \\ nil)

@spec run_action(hook_name(), term()) :: :ok

Run action hooks. All handlers are called with payload. Return values are ignored. Errors in one handler do NOT stop others.

run_filter(hook_name, initial_value)

@spec run_filter(hook_name(), term()) :: term()

Run filter hooks. Handlers are called in priority order, each receiving the return value of the previous. The final accumulated value is returned.

If a handler raises, the error is logged and the previous value is kept.

start_link(opts \\ [])

unregister_all(owner_module)

Unregister ALL hooks registered by a specific plugin module.