Wraith. Widgets. Lua
(Wraith v0.1.0)
View Source
Scripted widgets: a Lua 5.3 chunk (run by luerl, pure Erlang — no
native code, no OS access) defines
function init(props) return { count = 0 } end
function render(state, props, meta) return "<b>" .. state.count .. "</b>" end
function handle(state, event, payload, meta) state.count = state.count + 1; return state endThe chunk is compiled once per call into a fresh state (nothing leaks
between instances or readers), the dangerous libraries are removed
(os, io, package, require, load*, dofile), and each call is
cut off after @timeout_ms.
A wraith table helps with HTML:
wraith.escape(s)— HTML-escape a stringwraith.action(event, payload)— the attributes a clickable element needs to sendeventtohandle(phx-clickplumbing, instance included); e.g."<button " .. wraith.action("vote", {option="a"}) .. ">A</button>"wraith.json(t)/wraith.len(t)/wraith.keys(t)— small table helperswraith.split(s, sep)—"a|b"→{"a", "b"}(luerl has nogmatch)wraith.format(n)—1234→"1,234"wraith.fetch(url)— GET a URL (JSON decoded when the answer is JSON, else the text); only hosts the owner allowlisted in Settings (widget_fetch_hosts) and cached for 60 s; nil + an error string otherwisewraith.now()— unix seconds
Summary
Functions
The phx-* attributes that send an event for an instance.
Does the chunk compile, and does it define the three functions? :ok | {:error, why}.
A decoded Luerl table (proplist) → map or list.
Elixir → what Luerl encodes (maps with string keys, lists become 1-based tables).
Types
Functions
The phx-* attributes that send an event for an instance.
Does the chunk compile, and does it define the three functions? :ok | {:error, why}.
A decoded Luerl table (proplist) → map or list.
Elixir → what Luerl encodes (maps with string keys, lists become 1-based tables).