Running expensive operations inside Custom Change

What is the recommended way to run expensive operations inside Custom Change? I have something like this in my app:

actions do
    update :save_step_3 do
      accept [
        :delivery_address,
        :fulfillment_amount,
        :distance,
        :position,
        # ...
      ]

      change {ProcessDeliveryAddress, []}
      require_atomic? false
    end
end
defmodule ProcessDeliveryAddress do
  use Ash.Resource.Change

  @impl true
  def init(opts) do
    {:ok, opts}
  end

  @impl true
  def change(changeset, opts, context) do
    # Call multiple external APIs to verify delivery address and update :fulfillment_amount, :distance, etc.
    changeset
  end
end

I understand that I can add a debounce or throttle on the form, but I would prefer the expensive operation to only run when the user clicks the form submit button, rather than every time the form is validated. What is the best way to accomplish this? Should I pass in a flag via context?

Thanks! I’m loving Ash :yellow_heart:

You can use use_when_valid?: Ash.Resource — ash v3.5.8?

You can wrap the body of the change function inside a hook, like before_action, to only run it once before submit.