Macaw.Component behaviour (macaw_component v0.13.0)

Copy Markdown View Source

The component contract (spec/01): the behavior every Macaw component implements, and the dispatch functions a host reaches a component through.

defmodule MyApp.Cards do
  use Macaw.Component, name: "my_app.cards", kinds: [:content]

  @impl true
  def declarations(_settings), do: {:ok, reading: :scan}

  @impl true
  def render(:content, _assigns), do: {:safe, ~s(<ul class="cards"></ul>)}
end

Static declarations are use's options (name:, kinds:, hooks:, assets:, inline:) and parameters/0; configured declarations are declarations/1's answer, built by Macaw.Component.Declarations.new/2. The dispatch functions answer values: a component's bad answer is an error, never a raise. A component's own raise in render/2 or a transition is its host's process's to take (spec/02 §6).

The contract is unstable until two implementers pass its kit (spec/00 §3).

Summary

Types

A contained value's drawing (spec/02 §7.6): its token and its segments in order.

A fault: the path to the value, and why.

A kind a component renders as (spec/01 §2).

A row order as a component asks for it (spec/01 §1.4): each key's field a name of the schema's or {:role, name}, a role of the slot, the first key deciding first.

How a slot's rows may be reduced (spec/01 §1.4), the request's reduce:: a reducer by name and its own count, :none for never, or nil for the host's default.

What a hook asks its contained value (spec/02 §7.3): the name of one of the component's declared requests, and the payload the hook sent.

One segment of a contained value's drawing (spec/02 §7.6): {key, token, bytes}, the segment's key, the token of the drawing it last changed in, and its bytes, raw.

A contained component's value (spec/02 §7).

Functions

A cell's settings as a document writes them (spec/01 §1.1): each value a literal or a binding to a state source, {:state, name}, checked against the component's parameters and its qualified parameters (variables/2), and each binding against states, the declared state sources by name, each %{type: type} with an optional values list. {:ok, bound}, each bound parameter's state source by parameter, or every fault at once, each at [param]

A contained component's bound/3 (spec/02 §7).

A slot's row budget in the context the instance renders in, at the context's size (spec/01 §1.4): the component's budget/4 when it has one, handed the size and the context, whose pixel ratio it reads, else its budget/3, handed the size alone, at ratio 1. {:ok, nil} for a component without either, else {:ok, rows} for a positive integer or nil, and {:error, {:bad_budget, answer}} otherwise.

A contained value given the easing clock (spec/02 §7.4): ms, the host's monotonic milliseconds, which a host that ticks gives before each batch's inputs. A component without clock/2, or without both tick callbacks, takes no clock: the value as given.

Whether a module is loaded and uses Macaw.Component.

Whether a module exports every contained transition.

The component's configured declarations for given settings: the settings validated, then declarations/1 called with every bindable parameter at its default and no qualified parameter (spec/01 §1.1), and its answer built by Macaw.Component.Declarations.new/2.

A contained component's delivery/3 (spec/02 §7).

A fault as a line a person reads.

A component's static declarations, %{name, kinds, hooks, assets, inline}, or :error for a module that is not a component. inline is whether it renders phrasing content (spec/01 §1, 02 §5.1).

A contained component's init/2 (spec/02 §7).

A contained component initialized again, given the value it replaces (spec/02 §7.1): its init/3 when it has one, so it carries what still holds (a warm layout, a cache), else its init/2, starting cold.

The kinds a component may render as, in order.

The closed observation vocabulary, in order (spec/01 §1.6).

The row order a slot asks its source for (spec/01 §1.4): {:ok, []} for a component without order/2, else {:ok, order}, and {:error, {:bad_order, answer}} otherwise. An order's field keys are of Macaw.Data.Source.Order's shape ({field, :asc | :desc}, each field once); a role key, {{:role, name}, direction}, names a role the slot declares for these settings, once, of one field and of types that have an order.

A component's parameters, each checked by Macaw.Data.Parameter.check/1, named by a non-empty string, and, when bindable, with a default (spec/01 §1.1). Every fault at once, its path from the parameter's name.

The data a placement prepares for its renders (spec/01 §1, 02 §1): the component's prepare/2, handed the validated settings and the context, run by the host once per placement at validation and again when the settings or a stable field of the context (Macaw.Component.Context.stable_fields/0) change; its data reaches every render as the prepared assign. {:ok, nil} for a component without prepare/2; else {:ok, data} for {:ok, data}, the component's own {:error, faults} as given, and {:error, [{[], {:bad_prepare, answer}}]} for an answer of neither shape, or faults that are no {path, reason} pairs with a list for a path.

How a slot's rows may be reduced in the context the instance renders in (spec/01 §1.4), the reduce: a host puts in the slot's request: the component's reducer/4 when it has one, handed the context's size and the context, else its reducer/3, handed the size alone, at ratio 1. {:ok, nil} for a component without either, else {:ok, answer} for {name, count} (a non-empty name and a positive integer), :none or nil, and {:error, {:bad_reducer, answer}} otherwise. Whether the name is a registered reducer is the host's to judge, where its registry is.

A hook's request answered by its contained value (spec/02 §7.3): {:ok, {answer, value}}, {:error, :no_request} for a component without request/2, and {:error, {:bad_request, answer}} for an answer that is no {answer, value} pair.

The order a slot's request takes (spec/01 §1.4): order/3's answer in fields, for bindings, the slot's resolved binding (Slot.resolve/3). A role key takes its role's field, so an unbound window role reads the window field; an optional role left unbound drops its key; a key on a field an earlier key names is dropped, since it can decide nothing. A required role absent from bindings, or one holding anything but a field's name, is {:error, {:bad_order, answer}}, as order/3's refusals are.

A contained value's drawing as raw keyed segments (spec/02 §7.6), the bytes a runtime streams in place of the base64 its render carries: {:ok, {token, segments}}, :none for a component without segments/1, and {:error, {:segments, faults}} for an answer that is no drawing, every fault at once, ordered by path: {[:token], _}, or {[:segments, i, field], _} with field :key, :token or :bytes, for a name that is no string ({:type, :string}) or longer than 255 bytes ({:too_long, 255}) and bytes that are no iodata ({:type, :iodata}); {[:segments, i], {:duplicate_key, key}} for a key a segment before it has; {[:segments, i], {:type, :segment}} for an entry that is no triple; and {[], {:type, :drawing}} for an answer of another shape.

Given settings checked against the component's parameters, defaults filled in (Macaw.Data.Parameter.validate/2). For a component with variables/1, a qualified name given ("<qualifier>.<variable>", no parameter's name) is checked against variables/2's parameter of that name as a literal, its type and values, and kept; one it doesn't answer is :unknown_param. Qualified names aren't filled in: one not given holds the document's own value. They are checked once the parameters' settings pass, since the variables follow from those.

A contained component's size/2 (spec/02 §7).

A contained value stepped once at the host's monotonic ms (spec/02 §7.2). A component without both tick callbacks steps nothing: the value as given.

The milliseconds a contained value asks to wait for its next tick (spec/02 §7.2): {:ok, nil} for a component without both tick_interval/1 and tick/2, else {:ok, ms} for a positive integer or nil, and {:error, {:bad_tick_interval, answer}} otherwise.

A contained component's four transitions, as {name, arity}.

The qualified parameters of a component's document parameter (spec/01 §1.1): the settings validated by settings/2, then variables/1 called with every bindable parameter at its default and no qualified one, as declarations/1 is. {:ok, %{}} for a component without variables/1; else {:ok, parameters}, each name "<qualifier>.<variable>", each parameter checked by Macaw.Data.Parameter.check/1 and answered with bindable set: true when it has a default and a type other than :term, else false. A component with variables/1 and no {:document, type} parameter is {:error, [{[], :no_document}]}; a name that is no qualified name :not_qualified at it, one a parameter has :shadows_parameter; an answer of neither shape {:error, [{[], {:bad_variables, answer}}]}; the component's own faults (a document that doesn't validate) pass through.

Types

drawing()

@type drawing() :: {token :: String.t(), [segment()]}

A contained value's drawing (spec/02 §7.6): its token and its segments in order.

fault()

@type fault() :: {list(), term()}

A fault: the path to the value, and why.

kind()

@type kind() :: :navigation | :content | :control | :container

A kind a component renders as (spec/01 §2).

order()

@type order() :: [{Macaw.Data.Schema.name() | {:role, String.t()}, :asc | :desc}]

A row order as a component asks for it (spec/01 §1.4): each key's field a name of the schema's or {:role, name}, a role of the slot, the first key deciding first.

reducer()

@type reducer() :: {String.t(), pos_integer()} | :none | nil

How a slot's rows may be reduced (spec/01 §1.4), the request's reduce:: a reducer by name and its own count, :none for never, or nil for the host's default.

request()

@type request() :: {name :: String.t(), payload :: term()}

What a hook asks its contained value (spec/02 §7.3): the name of one of the component's declared requests, and the payload the hook sent.

segment()

@type segment() :: {key :: String.t(), token :: String.t(), iodata()}

One segment of a contained value's drawing (spec/02 §7.6): {key, token, bytes}, the segment's key, the token of the drawing it last changed in, and its bytes, raw.

value()

@type value() :: term()

A contained component's value (spec/02 §7).

Callbacks

bound(value, name, new)

(optional)
@callback bound(value(), name :: String.t(), new :: term()) :: value()

budget(slot, settings, size)

(optional)
@callback budget(slot :: String.t(), settings :: map(), size :: {number(), number()}) ::
  pos_integer() | nil

budget(slot, settings, size, t)

(optional)
@callback budget(
  slot :: String.t(),
  settings :: map(),
  size :: {number(), number()},
  Macaw.Component.Context.t()
) :: pos_integer() | nil

clock(value, ms)

(optional)
@callback clock(value(), ms :: integer()) :: value()

declarations(settings)

@callback declarations(settings :: %{required(String.t()) => term()}) ::
  {:ok, keyword() | map()} | {:error, [fault()]}

delivery(value, slot, t)

(optional)
@callback delivery(value(), slot :: String.t(), Macaw.Data.Delivery.t()) :: value()

init(settings, t)

(optional)
@callback init(settings :: map(), Macaw.Component.Context.t()) :: value()

init(settings, t, from)

(optional)
@callback init(settings :: map(), Macaw.Component.Context.t(), from :: value()) :: value()

order(slot, settings)

(optional)
@callback order(slot :: String.t(), settings :: map()) :: order()

parameters()

@callback parameters() :: %{required(String.t()) => Macaw.Data.Parameter.t()}

prepare(settings, t)

(optional)
@callback prepare(settings :: map(), Macaw.Component.Context.t()) ::
  {:ok, term()} | {:error, [fault()]}

reducer(slot, settings, size)

(optional)
@callback reducer(slot :: String.t(), settings :: map(), size :: {number(), number()}) ::
  reducer()

reducer(slot, settings, size, t)

(optional)
@callback reducer(
  slot :: String.t(),
  settings :: map(),
  size :: {number(), number()},
  Macaw.Component.Context.t()
) :: reducer()

render(kind, assigns)

@callback render(kind(), assigns :: map()) :: Phoenix.HTML.Safe.t()

request(value, request)

(optional)
@callback request(value(), request()) :: {answer :: term(), value()}

segments(value)

(optional)
@callback segments(value()) :: drawing()

size(value, tuple)

(optional)
@callback size(value(), {number(), number()}) :: value()

tick(value, ms)

(optional)
@callback tick(value(), ms :: integer()) :: value()

tick_interval(value)

(optional)
@callback tick_interval(value()) :: pos_integer() | nil

variables(settings)

(optional)
@callback variables(settings :: map()) ::
  {:ok, %{required(String.t()) => Macaw.Data.Parameter.t()}}
  | {:error, [fault()]}

Functions

bind(module, written, states)

@spec bind(module(), map(), %{required(String.t()) => map()}) ::
  {:ok, %{required(String.t()) => String.t()}} | {:error, [fault(), ...]}

A cell's settings as a document writes them (spec/01 §1.1): each value a literal or a binding to a state source, {:state, name}, checked against the component's parameters and its qualified parameters (variables/2), and each binding against states, the declared state sources by name, each %{type: type} with an optional values list. {:ok, bound}, each bound parameter's state source by parameter, or every fault at once, each at [param]:

  • the literals as settings/2 judges them, first, since the variables follow from them;
  • a parameter or qualified name the component doesn't have, :unknown_param;
  • a state source not in states, {:unknown_state, name};
  • a parameter not bindable, or a qualified parameter of type :term, {:not_bindable, param}; a qualified parameter without a default, :bindable_without_default;
  • a state of another type, {:state_type, name, state_type, param_type};
  • for a parameter with values, a state whose values are absent or not all among them, {:state_values, name}.

bound(module, value, name, new)

@spec bound(module(), value(), String.t(), term()) :: value()

A contained component's bound/3 (spec/02 §7).

budget(module, slot, settings, context)

@spec budget(module(), String.t(), map(), Macaw.Component.Context.t()) ::
  {:ok, pos_integer() | nil} | {:error, {:bad_budget, term()}}

A slot's row budget in the context the instance renders in, at the context's size (spec/01 §1.4): the component's budget/4 when it has one, handed the size and the context, whose pixel ratio it reads, else its budget/3, handed the size alone, at ratio 1. {:ok, nil} for a component without either, else {:ok, rows} for a positive integer or nil, and {:error, {:bad_budget, answer}} otherwise.

clock(module, value, ms)

@spec clock(module(), value(), integer()) :: value()

A contained value given the easing clock (spec/02 §7.4): ms, the host's monotonic milliseconds, which a host that ticks gives before each batch's inputs. A component without clock/2, or without both tick callbacks, takes no clock: the value as given.

component?(module)

@spec component?(module()) :: boolean()

Whether a module is loaded and uses Macaw.Component.

contained?(module)

@spec contained?(module()) :: boolean()

Whether a module exports every contained transition.

declarations(module, given)

@spec declarations(module(), map()) ::
  {:ok, Macaw.Component.Declarations.t()} | {:error, [fault(), ...]}

The component's configured declarations for given settings: the settings validated, then declarations/1 called with every bindable parameter at its default and no qualified parameter (spec/01 §1.1), and its answer built by Macaw.Component.Declarations.new/2.

delivery(module, value, slot, delivery)

@spec delivery(module(), value(), String.t(), Macaw.Data.Delivery.t()) :: value()

A contained component's delivery/3 (spec/02 §7).

format_fault(arg)

@spec format_fault(fault()) :: String.t()

A fault as a line a person reads.

info(module)

@spec info(module()) :: {:ok, map()} | :error

A component's static declarations, %{name, kinds, hooks, assets, inline}, or :error for a module that is not a component. inline is whether it renders phrasing content (spec/01 §1, 02 §5.1).

init(module, settings, context)

@spec init(module(), map(), Macaw.Component.Context.t()) :: value()

A contained component's init/2 (spec/02 §7).

init(module, settings, context, from)

@spec init(module(), map(), Macaw.Component.Context.t(), value()) :: value()

A contained component initialized again, given the value it replaces (spec/02 §7.1): its init/3 when it has one, so it carries what still holds (a warm layout, a cache), else its init/2, starting cold.

kinds()

@spec kinds() :: [kind(), ...]

The kinds a component may render as, in order.

observations()

@spec observations() :: [atom(), ...]

The closed observation vocabulary, in order (spec/01 §1.6).

order(module, slot, settings)

@spec order(module(), String.t(), map()) ::
  {:ok, order()} | {:error, {:bad_order, term()}}

The row order a slot asks its source for (spec/01 §1.4): {:ok, []} for a component without order/2, else {:ok, order}, and {:error, {:bad_order, answer}} otherwise. An order's field keys are of Macaw.Data.Source.Order's shape ({field, :asc | :desc}, each field once); a role key, {{:role, name}, direction}, names a role the slot declares for these settings, once, of one field and of types that have an order.

A host doesn't put this answer in a request: resolve_order/4 answers it in fields. A field-keyed order never asks for the declarations.

parameters(module)

@spec parameters(module()) :: {:ok, map()} | {:error, [fault(), ...]}

A component's parameters, each checked by Macaw.Data.Parameter.check/1, named by a non-empty string, and, when bindable, with a default (spec/01 §1.1). Every fault at once, its path from the parameter's name.

prepare(module, settings, context)

@spec prepare(module(), map(), Macaw.Component.Context.t()) ::
  {:ok, term()} | {:error, [fault(), ...]}

The data a placement prepares for its renders (spec/01 §1, 02 §1): the component's prepare/2, handed the validated settings and the context, run by the host once per placement at validation and again when the settings or a stable field of the context (Macaw.Component.Context.stable_fields/0) change; its data reaches every render as the prepared assign. {:ok, nil} for a component without prepare/2; else {:ok, data} for {:ok, data}, the component's own {:error, faults} as given, and {:error, [{[], {:bad_prepare, answer}}]} for an answer of neither shape, or faults that are no {path, reason} pairs with a list for a path.

reducer(module, slot, settings, context)

@spec reducer(module(), String.t(), map(), Macaw.Component.Context.t()) ::
  {:ok, reducer()} | {:error, {:bad_reducer, term()}}

How a slot's rows may be reduced in the context the instance renders in (spec/01 §1.4), the reduce: a host puts in the slot's request: the component's reducer/4 when it has one, handed the context's size and the context, else its reducer/3, handed the size alone, at ratio 1. {:ok, nil} for a component without either, else {:ok, answer} for {name, count} (a non-empty name and a positive integer), :none or nil, and {:error, {:bad_reducer, answer}} otherwise. Whether the name is a registered reducer is the host's to judge, where its registry is.

request(module, value, request)

@spec request(module(), value(), request()) ::
  {:ok, {term(), value()}} | {:error, :no_request | {:bad_request, term()}}

A hook's request answered by its contained value (spec/02 §7.3): {:ok, {answer, value}}, {:error, :no_request} for a component without request/2, and {:error, {:bad_request, answer}} for an answer that is no {answer, value} pair.

resolve_order(module, slot, settings, bindings)

@spec resolve_order(module(), String.t(), map(), map()) ::
  {:ok, Macaw.Data.Source.Order.t()} | {:error, {:bad_order, term()}}

The order a slot's request takes (spec/01 §1.4): order/3's answer in fields, for bindings, the slot's resolved binding (Slot.resolve/3). A role key takes its role's field, so an unbound window role reads the window field; an optional role left unbound drops its key; a key on a field an earlier key names is dropped, since it can decide nothing. A required role absent from bindings, or one holding anything but a field's name, is {:error, {:bad_order, answer}}, as order/3's refusals are.

segments(module, value)

@spec segments(module(), value()) ::
  {:ok, drawing()} | :none | {:error, {:segments, [fault(), ...]}}

A contained value's drawing as raw keyed segments (spec/02 §7.6), the bytes a runtime streams in place of the base64 its render carries: {:ok, {token, segments}}, :none for a component without segments/1, and {:error, {:segments, faults}} for an answer that is no drawing, every fault at once, ordered by path: {[:token], _}, or {[:segments, i, field], _} with field :key, :token or :bytes, for a name that is no string ({:type, :string}) or longer than 255 bytes ({:too_long, 255}) and bytes that are no iodata ({:type, :iodata}); {[:segments, i], {:duplicate_key, key}} for a key a segment before it has; {[:segments, i], {:type, :segment}} for an entry that is no triple; and {[], {:type, :drawing}} for an answer of another shape.

settings(module, given)

@spec settings(module(), map()) :: {:ok, map()} | {:error, [fault(), ...]}

Given settings checked against the component's parameters, defaults filled in (Macaw.Data.Parameter.validate/2). For a component with variables/1, a qualified name given ("<qualifier>.<variable>", no parameter's name) is checked against variables/2's parameter of that name as a literal, its type and values, and kept; one it doesn't answer is :unknown_param. Qualified names aren't filled in: one not given holds the document's own value. They are checked once the parameters' settings pass, since the variables follow from those.

size(module, value, size)

@spec size(module(), value(), {number(), number()}) :: value()

A contained component's size/2 (spec/02 §7).

tick(module, value, ms)

@spec tick(module(), value(), integer()) :: value()

A contained value stepped once at the host's monotonic ms (spec/02 §7.2). A component without both tick callbacks steps nothing: the value as given.

tick_interval(module, value)

@spec tick_interval(module(), value()) ::
  {:ok, pos_integer() | nil} | {:error, {:bad_tick_interval, term()}}

The milliseconds a contained value asks to wait for its next tick (spec/02 §7.2): {:ok, nil} for a component without both tick_interval/1 and tick/2, else {:ok, ms} for a positive integer or nil, and {:error, {:bad_tick_interval, answer}} otherwise.

transitions()

@spec transitions() :: keyword(non_neg_integer())

A contained component's four transitions, as {name, arity}.

variables(module, given)

@spec variables(module(), map()) ::
  {:ok, %{required(String.t()) => Macaw.Data.Parameter.t()}}
  | {:error, [fault(), ...]}

The qualified parameters of a component's document parameter (spec/01 §1.1): the settings validated by settings/2, then variables/1 called with every bindable parameter at its default and no qualified one, as declarations/1 is. {:ok, %{}} for a component without variables/1; else {:ok, parameters}, each name "<qualifier>.<variable>", each parameter checked by Macaw.Data.Parameter.check/1 and answered with bindable set: true when it has a default and a type other than :term, else false. A component with variables/1 and no {:document, type} parameter is {:error, [{[], :no_document}]}; a name that is no qualified name :not_qualified at it, one a parameter has :shadows_parameter; an answer of neither shape {:error, [{[], {:bad_variables, answer}}]}; the component's own faults (a document that doesn't validate) pass through.