A state source a surface declares (spec/03 §1.1): %Macaw.State.Source{type, values, initial, scope, persist, snapshot}.
type, a data-model type, as a parameter's::time,:number,:category,:text,:coloror:point; required;values, the closed list of legal values, each of the type, ornilfor any value of the type;values_from, legal values from data (spec/03 §1.7, spec/07 §2.2):"source.field", the current values of a field of one of the host's sources, checked at each change, ornil; never besidevalues;multi, whether it holds several values, a list of distinct values of the type,falseby default, andmax, the most it holds, a positive integer, ornilfor no bound;initial, the value before any event: a literal of the type and amongvalues,nilfor none,{:host, key}, a value the host gives underkey, a name, or{:param, name}, the value of the page paramname(spec/06 §5);scope, where it lives (spec/03 §2, §2.1)::surface, one surface instance's, the default;:page, the page's;:region, a persistent region's content's;:session, the visit's;:viewer, the viewer's, across visits;:shared, one value for every viewer of the surface, held by its owner; or:local, the browser's only, declared but never held on the server;persist, whether a host's persistence keeps it (spec/03 §3):trueby default,falseto keep it out (a typed search, anything personal);snapshot, whether a state snapshot holds it (spec/03 §4):trueby default,falseto keep it out of snapshots even when it persists;from,nil, or{:export, surface, name}for a mirror of another surface's export (spec/03 §5.2): its value the export's, put by the page, never set by an action, never persisted or snapshotted, at:surfacescope and with noinitial.
A literal given as an atom, for a :category or :text source, is read as its
spelling, and no atom is made.
Summary
Functions
A value judged as this source's (spec/03 §1.5): {:ok, value}, nil for no value, a
:category or :text atom read as its spelling; {:error, {:type, type}} for one not
of the type, {:error, {:not_allowed, value}} for one outside values.
A state source from a map or keyword list with atom keys: {:ok, source} or every
fault at once, by path from the entry.
The entry as a document writes it (spec/03 §1.1): its map, the defaults left out.
The scopes a state source may live in (spec/03 §2.1), the seven: :local, :surface,
:page, :region, :session, :viewer and :shared.
The types a state source may have.
Types
@type t() :: %Macaw.State.Source{ from: {:export, String.t(), String.t()} | nil, initial: term() | {:host, String.t()} | {:param, String.t()} | nil, max: pos_integer() | nil, multi: boolean(), persist: boolean(), scope: :local | :surface | :page | :region | :session | :viewer | :shared, snapshot: boolean(), type: :time | :number | :category | :text | :color | :point, values: [term(), ...] | nil, values_from: String.t() | nil }
Functions
A value judged as this source's (spec/03 §1.5): {:ok, value}, nil for no value, a
:category or :text atom read as its spelling; {:error, {:type, type}} for one not
of the type, {:error, {:not_allowed, value}} for one outside values.
A multi source's value is a list of distinct values of the type, each judged as a
single one is, [] read as nil: a term that isn't a list is {:error, {:type, {:list, type}}}, as is a list holding a value not of the type; a value given twice {:error, {:duplicate, value}}; more than max values {:error, {:limit, :max, max, count}}.
Whether a value is among a values_from source's current values is the request-time
check's (spec/07 §2.2), never this one's.
iex> source = %Macaw.State.Source{type: :category, values: ["asc", "desc"]}
iex> Macaw.State.Source.judge(source, :desc)
{:ok, "desc"}
iex> Macaw.State.Source.judge(source, "up")
{:error, {:not_allowed, "up"}}
iex> Macaw.State.Source.judge(%Macaw.State.Source{type: :number}, "3")
{:error, {:type, :number}}
@spec new(map() | keyword()) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
A state source from a map or keyword list with atom keys: {:ok, source} or every
fault at once, by path from the entry.
iex> Macaw.State.Source.new(%{type: :category, values: ["asc", "desc"], initial: :asc})
{:ok, %Macaw.State.Source{type: :category, values: ["asc", "desc"], values_from: nil, multi: false, max: nil, initial: "asc", scope: :surface, persist: true, snapshot: true, from: nil}}
iex> Macaw.State.Source.new(%{type: :number, initial: "high", scope: :global})
{:error, [{[:initial], {:type, :number}}, {[:scope], {:enum, [:local, :surface, :page, :region, :session, :viewer, :shared]}}]}
The entry as a document writes it (spec/03 §1.1): its map, the defaults left out.
@spec scopes() :: [atom(), ...]
The scopes a state source may live in (spec/03 §2.1), the seven: :local, :surface,
:page, :region, :session, :viewer and :shared.
@spec types() :: [atom(), ...]
The types a state source may have.