Lavash.Dsl.CommonEntities (Lavash v0.3.0-rc.0)

Copy Markdown View Source

Shared DSL entity definitions used by both Lavash.Dsl (LiveView) and Lavash.Component.Dsl.

This module provides common entity definitions to reduce duplication between the two DSLs. Each DSL imports the entities it needs and may extend schemas with runtime-specific options.

Summary

Functions

Base schema for action entities.

Base schema for calculate entities.

Base schema for form entities.

Base schema fields shared by all state entities.

Effect entity for actions - runs a side effect function.

MapBy entity for actions — key-based array mutations.

Argument entity for read and derive blocks.

Run entity for actions - executes a function that transforms assigns.

Set entity for actions - assigns a value to a state field.

Submit entity for actions - submits a form.

Functions

base_action_schema()

Base schema for action entities.

base_calculate_schema()

Base schema for calculate entities.

base_form_schema()

Base schema for form entities.

base_state_schema()

Base schema fields shared by all state entities.

LiveView extends this with: :url from option, setter, encode, decode, required Component uses this with: :socket/:ephemeral from option only

effect_entity()

Effect entity for actions - runs a side effect function.

map_by_entity()

MapBy entity for actions — key-based array mutations.

Finds items in an array by a key field and applies a transformation. The @item variable references the matched item in the rx() expression. Return :remove to filter the item out.

read_argument_entity()

Argument entity for read and derive blocks.

run_entity()

Run entity for actions - executes a function that transforms assigns.

Server-only (plain function)

Use this for complex multi-field updates where individual set calls would be cumbersome:

action :checkout do
  run fn assigns ->
    assigns
    |> assign(:status, :processing)
    |> assign(:submitted_at, DateTime.utc_now())
  end
end

Transpilable (with reads)

Add reads to declare state dependencies, enabling JavaScript transpilation for optimistic client-side updates:

action :apply_discount do
  run [:subtotal, :discount_rate], fn assigns ->
    discount = assigns.subtotal * assigns.discount_rate
    final = assigns.subtotal - discount

    assigns
    |> assign(:discount_amount, discount)
    |> assign(:total, final)
  end
end

The reads list declares which state fields the function depends on. This enables the transpiler to generate equivalent JavaScript.

The function receives an assigns map (state + params merged) and should use assign/3 to update fields. This ensures proper change tracking.

For simple single-field updates, prefer set with rx():

action :increment do
  set :count, rx(@count + 1)
end

set_entity()

Set entity for actions - assigns a value to a state field.

The value can use @field syntax to reference state fields and params, aligned with template syntax:

action :increment do
  set :count, @count + 1
end

action :add_item do
  params [:name]
  set :items, @items ++ [@name]
end

The expression is captured at compile time and can be transpiled to JavaScript for optimistic client-side updates.

submit_entity()

Submit entity for actions - submits a form.