Drafter.EventResult (drafter v0.3.2)

Copy Markdown View Source

Normalises what a widget's handle_event/2 returned into the triple the router uses.

parse/2 maps every accepted return shape onto {new_state, actions, mode}, or the bare atom :not_handled. The modes are:

  • :stop — handled; the event does not travel to the parent
  • :bubble — handled, and the event still travels to the parent
  • :not_handled — the widget did not act on the event

Examples

iex> Drafter.EventResult.parse({:ok, %{count: 1}}, %{count: 0})
{%{count: 1}, [], :stop}

Summary

Functions

Normalise a widget handler's return value.

Types

mode()

@type mode() :: :stop | :bubble | :not_handled

t()

@type t() :: {term(), list(), mode()} | :not_handled

Functions

parse(action, fallback)

@spec parse(term(), term()) :: t()

Normalise a widget handler's return value.

Accepted shapes, where state is the widget's new state and actions a list:

  • {:ok, state, actions} and {:ok, state}:stop
  • {:bubble, state, actions} and {:bubble, state}:bubble
  • {:noreply, _}:not_handled
  • a bare action {:pop, _}, {:push, _, _}, {:replace, _, _} or {:app_callback, _, _}{fallback, [action], :stop}

fallback is the state to keep when the handler returned an action instead of a state — normally the state the widget had before the call. Anything else returns :not_handled and fallback is unused.

Examples

iex> Drafter.EventResult.parse({:ok, :new, [:act]}, :old)
{:new, [:act], :stop}

iex> Drafter.EventResult.parse({:ok, :new}, :old)
{:new, [], :stop}

iex> Drafter.EventResult.parse({:bubble, :new}, :old)
{:new, [], :bubble}

iex> Drafter.EventResult.parse({:noreply, :new}, :old)
:not_handled

iex> Drafter.EventResult.parse({:pop, :result}, :old)
{:old, [{:pop, :result}], :stop}

iex> Drafter.EventResult.parse({:app_callback, :saved, 1}, :old)
{:old, [{:app_callback, :saved, 1}], :stop}

iex> Drafter.EventResult.parse(:something_else, :old)
:not_handled