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 end

The 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 string
  • wraith.action(event, payload) — the attributes a clickable element needs to send event to handle (phx-click plumbing, instance included); e.g. "<button " .. wraith.action("vote", {option="a"}) .. ">A</button>"
  • wraith.json(t) / wraith.len(t) / wraith.keys(t) — small table helpers
  • wraith.split(s, sep) — "a|b" → {"a", "b"} (luerl has no gmatch)
  • 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 otherwise
  • wraith.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

result()

@type result() :: {:ok, term()} | {:error, String.t()}

Functions

action_attrs(instance, event, payload)

The phx-* attributes that send an event for an instance.

call(source, function, args, meta \\ %{})

@spec call(String.t(), String.t(), [term()], map()) :: result()

check(source)

@spec check(String.t()) :: :ok | {:error, String.t()}

Does the chunk compile, and does it define the three functions? :ok | {:error, why}.

from_lua(list)

A decoded Luerl table (proplist) → map or list.

to_lua(map)

Elixir → what Luerl encodes (maps with string keys, lists become 1-based tables).