Ash.Signals.Delivery behaviour (ash v3.36.1)

Copy Markdown View Source

A behaviour for delivering signals to their listeners.

Each listener declares how signals reach it, with its delivery option. The default, Ash.Signals.Delivery.Inline, runs the listener immediately, during the phase in which the signal is emitted. Other deliveries can, for example, enqueue a job, so that the listener runs after the emitting action's transaction commits.

signals_in do
  from MyApp.Shop.Signals do
    on :order_placed, :send_receipt, delivery: {MyApp.JobDelivery, queue: :mailers}
  end
end

When a signal is emitted, during the signal's phase and after every payload has been cast, deliver/4 is called once for each delivery and options its listeners use, with those listeners. To run a listener, immediately or later, use Ash.Signals.run_listener/3, which does exactly what the inline delivery does. A delivery that runs listeners later can store signals with Ash.Signals.dump/3, load them with Ash.Signals.load/3, and find the listener again with Ash.Resource.Info.signal_listener/2.

Summary

Callbacks

Delivers signals to the given listeners.

Verifies a listener that uses this delivery, when the listening resource is compiled. Receives the signal it listens to, and the options given to the delivery.

Callbacks

deliver(signals, listeners, opts, context)

@callback deliver(
  signals :: [struct()],
  listeners :: [Ash.Resource.SignalListener.t()],
  opts :: Keyword.t(),
  context :: Ash.Signals.Delivery.Context.t()
) :: :ok | {:error, term()}

Delivers signals to the given listeners.

signals are the signal structs emitted together, listeners are the listeners of the signal that use this delivery with these options, and opts are the options given to the delivery in their delivery option. Returning an error fails the emit.

verify_listener(listener, signal, opts, dsl_state)

(optional)
@callback verify_listener(
  listener :: Ash.Resource.SignalListener.t(),
  signal :: Ash.Signals.Signal.t(),
  opts :: Keyword.t(),
  dsl_state :: Spark.Dsl.t()
) :: :ok | {:error, String.t()}

Verifies a listener that uses this delivery, when the listening resource is compiled. Receives the signal it listens to, and the options given to the delivery.