Harlock.App behaviour (harlock v0.2.0)

Copy Markdown View Source

Behaviour for Harlock applications.

An app defines three callbacks:

  • init/1 — returns the initial model from an arbitrary init argument.
  • update/2 — given an event and the current model, returns the next model, optionally paired with a Cmd. Return :quit to exit the app.
  • view/1 — given the current model, returns an element tree.

The simplest app:

defmodule Counter do
  use Harlock.App

  def init(_), do: %{n: 0}

  def update({:key, {:char, ?+}, []}, m), do: %{m | n: m.n + 1}
  def update({:key, {:char, ?q}, []}, _), do: :quit
  def update(_event, m), do: m

  def view(m) do
    vbox(constraints: [length: 1, fill: 1], children: [
      text("Count: #{m.n}"),
      text("(+ to inc, q to quit)")
    ])
  end
end

Summary

Types

model()

@type model() :: any()

msg()

@type msg() :: any()

Callbacks

init(any)

@callback init(any()) :: model() | {model(), Harlock.Cmd.t()}

subs(model)

(optional)
@callback subs(model()) :: [Harlock.Sub.t()]

update(msg, model)

@callback update(msg(), model()) ::
  model() | {model(), Harlock.Cmd.t()} | :quit | {:quit, Harlock.Cmd.t()}

view(model)

@callback view(model()) :: Harlock.Element.t()