# `AshBackendDrivenOptimisticUi.Resource`
[🔗](https://github.com/Undeadlol1/ash_backend_driven_optimistic_ui/blob/v0.1.0/lib/ash_backend_driven_optimistic_ui/resource.ex#L1)

Declare, next to an action, what the client may show the moment the user
calls it, before the server answers.

    optimistic_ui do
      optimistic :award_xp do
        push :xp_awarded, xp_amount: expr(arg(:xp_amount))

        where expr(arg(:xp_amount) >= xp_to_next_level) do
          set :current_level, expr(current_level + 1)
          push :level_up, message: expr(^context(:level_up_message))
        end
      end

      on :level_up do
        set :balance, expr(balance + 1)
      end
    end

An action that should have no rule says so with
`not_optimistic :create_from_image, because: :ai_result`. The reason must be
one of `AshBackendDrivenOptimisticUi.NotOptimistic.reasons/0`; the client never sees it.

Readable changes on the action become `set` effects by themselves, ahead of
the declared ones: `atomic_update(attr, expr)` and `set_attribute(attr, v)`
where `v` is a literal or `arg(...)`. Opaque change modules and conditional
changes yield nothing. A declared `set` for the same attribute, anywhere in
the `optimistic`, replaces the derived one.

An expression may read the record's own attributes, calculations and aggregates, the action's
arguments (`arg/1`, not inside `on`), the context (`^context/1`) and, inside
`on` only, the pushed event's payload (`event/1`). Never
relationships or the actor: the client does not hold them.
`AshBackendDrivenOptimisticUi.Compiler` lists the functions the client can run.

# `optimistic_ui`
*macro* 

---

*Consult [api-reference.md](api-reference.md) for complete listing*
