MishkaGervaz.Table.Web.Events.HookRunner behaviour (MishkaGervaz v0.0.1-alpha.2)

Copy Markdown View Source

Handles hook execution for Events module.

This module provides functions for running hooks and applying hook results. Hooks allow customization of event handling at specific points.

Customization

You can create a custom HookRunner:

defmodule MyApp.CustomHookRunner do
  use MishkaGervaz.Table.Web.Events.HookRunner

  # Custom hook runner with error logging
  def run_hook(hooks, hook_name, args) do
    result = super(hooks, hook_name, args)
    case result do
      {:error, reason} -> Logger.error("Hook failed", hook: hook_name, reason: reason)
      _ -> :ok
    end
    result
  end
end

Then configure it in your resource's DSL:

mishka_gervaz do
  table do
    events do
      hooks MyApp.CustomHookRunner
    end
  end
end

See MishkaGervaz.Table.Web.Events, MishkaGervaz.Table.Dsl.Hooks (where hooks are declared), MishkaGervaz.Table.Entities.ActionHook (the per-action hook entity), MishkaGervaz.Table.Web.DataLoader.HookRunner (data-loading counterpart), and the sibling handlers SanitizationHandler, RecordHandler, SelectionHandler, BulkActionHandler, RelationFilterHandler.

Summary

Callbacks

Applies a hook result to the socket.

Runs a hook with the given arguments.

Types

hook_name()

@type hook_name() :: atom() | {:on_event, String.t()} | {:on_bulk_action, String.t()}

hooks()

@type hooks() :: map() | nil

socket()

@type socket() :: Phoenix.LiveView.Socket.t()

Callbacks

apply_hook_result(hooks, hook_name, args, default_socket)

@callback apply_hook_result(
  hooks :: hooks(),
  hook_name :: hook_name(),
  args :: list(),
  default_socket :: socket()
) :: socket() | {:halt, socket()}

Applies a hook result to the socket.

Handles various hook return formats:

  • {:halt, socket} - Stops processing and returns {:halt, socket}
  • {:cont, socket} - Continues with the returned socket
  • socket - Continues with the returned socket
  • nil or other - Uses the default socket

run_hook(hooks, hook_name, args)

@callback run_hook(hooks :: hooks(), hook_name :: hook_name(), args :: list()) :: any()

Runs a hook with the given arguments.

Returns the hook's return value or nil if the hook doesn't exist.