Lavash.Optimistic.Macros (Lavash v0.4.0-rc.3)

Copy Markdown View Source

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

optimistic_action(name, field, opts)

(macro)

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. Function fn current, value -> new_value end that transforms the field
  • :validate - Optional. Function fn current, value -> boolean end for 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