Wraith.Widgets.Widget behaviour (Wraith v0.1.0)

View Source

What a widget is, whatever language it is written in.

A widget is a tiny app embedded in a post with {{widget slug key=value}}. Every instance (a slug plus an instance id) has one shared state (a JSON-shaped map) that every reader sees live: an event from one browser runs handle/4, the new state is saved and broadcast, and every open page re-renders.

  • init/1 — the state a fresh instance starts with, from the props
  • render/3 — HTML for a state (meta has instance, visitor, presence, post); output is the widget's own markup, placed inside a wrapper that carries the instance id
  • handle/4 — a reader's event → the next state (or {:error, why} to refuse it)

Elixir widgets implement this behaviour and are registered in Wraith.Widgets.builtin_modules/0; Lua widgets implement the same three functions in a sandbox (Wraith.Widgets.Lua).

Summary

Types

meta()

@type meta() :: %{
  instance: String.t(),
  visitor: String.t() | nil,
  presence: non_neg_integer(),
  post: map() | nil
}

props()

@type props() :: %{optional(String.t()) => term()}

state()

@type state() :: %{optional(String.t()) => term()}

Callbacks

describe()

@callback describe() :: %{name: String.t(), description: String.t(), props: [map()]}

The one-line description shown in the studio.

handle(state, t, map, meta)

@callback handle(state(), String.t(), map(), meta()) :: state() | {:error, String.t()}

init(props)

@callback init(props()) :: state()

render(state, props, meta)

@callback render(state(), props(), meta()) :: String.t()