Ash.Notifier behaviour (ash v3.21.1)

Copy Markdown View Source

A notifier is an extension that receives various events

Summary

Callbacks

A load statement to be applied before this notifier receives the notification.

Functions

Extracts pre-loaded notifier dependency data from a record's calculations and builds the notifier_data map used by enrich_notification/3.

Builds a query with NotifierDependencies calculations for all notifiers that implement load/2 with non-empty load statements.

Sends any notifications that can be sent, and returns the rest.

Callbacks

load(t, action)

(optional)
@callback load(Ash.Resource.t(), Ash.Resource.Actions.action()) ::
  atom() | [atom()] | Keyword.t()

A load statement to be applied before this notifier receives the notification.

The loaded fields are merged onto notification.data before notify/1 is called. If multiple notifiers request the same fields with the same arguments, the calculation dependency resolver ensures they are only loaded once.

The return value can be anything accepted by Ash.Query.load/2, including %Ash.Query.Calculation{} structs for multi-level dependency resolution (e.g. a PubSub notifier building one inner load per publication).

notify(t)

@callback notify(Ash.Notifier.Notification.t()) :: :ok

requires_original_data?(t, action)

@callback requires_original_data?(Ash.Resource.t(), Ash.Resource.Actions.action()) ::
  boolean()

Functions

extract_notifier_data(record, notifiers, resource, action)

@spec extract_notifier_data(
  Ash.Resource.record(),
  [module()],
  Ash.Resource.t(),
  Ash.Resource.Actions.action()
) :: {map(), map()}

Extracts pre-loaded notifier dependency data from a record's calculations and builds the notifier_data map used by enrich_notification/3.

Returns {notifier_statements, notifier_data} where notifier_data maps each notifier to {statement, extra}.

notifier_calculation_query(resource, action, source_context \\ %{})

@spec notifier_calculation_query(
  Ash.Resource.t(),
  Ash.Resource.Actions.action(),
  map()
) ::
  Ash.Query.t() | nil

Builds a query with NotifierDependencies calculations for all notifiers that implement load/2 with non-empty load statements.

This is meant to be merged into the load that already happens in the action pipeline, so that notifier dependencies are loaded in bulk (once per batch) rather than per-notification at dispatch time.

notify(resource_notifications)

Sends any notifications that can be sent, and returns the rest.

A notification can only be sent if you are not currently in a transaction for the resource in question.