Turns a form's two free-form fields - an event name and a payload text -
into a Statifier.Event.t(), the ordinary recordable input
(Statifier.Event.external/2) rather than a side door.
The three-way payload distinction
Statifier.Event's own moduledoc is explicit that data has three
states that must not collapse into each other: no data (:undefined),
data, present, and null (nil), and data, present, and empty
(%{}). One text field has to spell all three, and build/2 resolves
them this way:
- blank (
nil, or a string that is empty after trimming) meansdata: :undefined- "no data" - the same defaultEvent.external/2itself uses when the caller passes no:dataoption at all. "null"decodes throughJSON.decode/1tonil, whichStatifierUI.Value.decode/1passes through unchanged - "data, present, and null"."{}"decodes to%{}the same way - "data, present, and empty".
Any other payload text is decoded as ADR-0005 JSON via JSON.decode/1
and then StatifierUI.Value.decode/1, so a $-tagged shape
($undefined, $date, $datetime, $duration) resolves to the
predicator value it encodes.
Summary
Functions
Builds a Statifier.Event.t() from a form's name and payload text.
Types
@type reason() :: :blank_event_name | {:invalid_event_name, term()} | {:invalid_json, term()} | {:invalid_payload, term()}
Why build/2 refused to produce an event.
:blank_event_name- the name was empty, or all whitespace.{:invalid_event_name, name}-namewas not a binary, or contained whitespace or a control character (SCXML event names are dot-delimited tokens; one with a space in it can never match a transition'seventattribute, so it is a typo rather than a debugging choice).{:invalid_json, reason}- the payload text was not valid JSON;reasonisJSON.decode/1's own error value.{:invalid_payload, reason}- the payload decoded as JSON but not as an ADR-0005 value (an unknown$-prefixed tag, a malformed$date);reasonisStatifierUI.Value.decode/1's own error value.
Functions
@spec build(String.t(), String.t() | nil) :: {:ok, Statifier.Event.t()} | {:error, reason()}
Builds a Statifier.Event.t() from a form's name and payload text.
name is trimmed of leading/trailing whitespace before validation. An
unmatched but well-formed name is legal and is not rejected here - only
syntax is checked; whether the name matches any transition is the
chart's business, not this function's.
payload_text defaults to nil. nil, or a string that is blank after
trimming, means no data (data: :undefined); see the moduledoc for the
full three-way spelling.
No option other than :data is passed to Statifier.Event.external/2:
invokeid, origin, origintype, and sendid all belong to delivery
paths this function is not.
Examples
iex> {:ok, event} = StatifierUI.EventInjection.Draft.build("payment.success", ~s({"amount":1999}))
iex> event.data
%{"amount" => 1999}
iex> {:ok, event} = StatifierUI.EventInjection.Draft.build("payment.success", "")
iex> event.data
:undefined
iex> StatifierUI.EventInjection.Draft.build("")
{:error, :blank_event_name}