Macros for optimistic actions.
The optimistic_action macro captures source code at compile time for
transpilation to JavaScript, enabling client-side optimistic updates.
Note: The calculate macro has been deprecated in favor of the Spark DSL:
calculate :name, rx(expr) - see Lavash.Dsl for documentation.
Summary
Functions
Defines an optimistic action that runs on both server and client.
Functions
Defines an optimistic action that runs on both server and client.
This macro captures the source code of the run and validate functions
for JavaScript compilation, then stores the action in a module attribute.
Options
:run- Required. Functionfn current, value -> new_value endthat transforms the field:validate- Optional. Functionfn current, value -> boolean endfor validation:max- Optional. Field name containing max length for array fields
Example
optimistic_action :add, :tags,
run: fn tags, tag -> tags ++ [tag] end,
validate: fn tags, tag -> tag not in tags end,
max: :max_tags
optimistic_action :remove, :tags,
run: fn tags, tag -> Enum.reject(tags, &(&1 == tag)) end