Drafter.Runtime.Reducer (drafter v0.3.1)

Copy Markdown View Source

Elm-style reducer runtime backend.

An app using this backend defines init/1 (initial state), update/2 (msg, state -> new_state), and render/1. The loop's input events, named callbacks, timers, and out-of-band messages are all delivered to update/2:

  • raw input events arrive as their event tuple, e.g. {:key, :q}
  • named callbacks arrive as {name, data}
  • fired timers arrive as {:timer, timer_id}

update/2 returns the new state, or {:stop, reason} to quit. Render is skipped when it returns a state value-equal to the previous one.

An app that does not define update/2 falls back to the Callback backend.

Summary

Functions

Passes the raw event tuple to the app's update/2.

Passes a named callback to the app's update/2 as {name, data}.

Calls the app's init/1, falling back to Callback.mount/2 when it defines none.

Passes an out-of-band process message to the app's update/2 verbatim.

Passes a fired timer to the app's update/2 as {:timer, timer_id}.

Functions

handle_input(app, event, state)

@spec handle_input(module(), term(), term()) ::
  {:ok, term()} | {:noreply, term()} | {:stop, term()} | term()

Passes the raw event tuple to the app's update/2.

Returns {:noreply, state} when update/2 returns a state equal to the one passed in, {:stop, reason} unchanged, and {:ok, new_state} otherwise. Apps without an update/2 fall through to Drafter.Runtime.Callback.handle_input/3.

handle_message(app, name, data, state)

@spec handle_message(module(), atom(), term(), term()) ::
  {:ok, term()} | {:noreply, term()} | {:stop, term()} | term()

Passes a named callback to the app's update/2 as {name, data}.

Result shapes match handle_input/3. Apps without an update/2 fall through to Drafter.Runtime.Callback.handle_message/4.

mount(app, props)

@spec mount(module(), map()) :: term()

Calls the app's init/1, falling back to Callback.mount/2 when it defines none.

on_message(app, msg, state)

@spec on_message(module(), term(), term()) :: term()

Passes an out-of-band process message to the app's update/2 verbatim.

ready(app, state)

@spec ready(module(), term()) :: term()

Delegates to Drafter.Runtime.Callback.ready/2.

refresh_rate(app)

@spec refresh_rate(module()) :: Drafter.Runtime.refresh_rate()

Delegates to Drafter.Runtime.Callback.refresh_rate/1.

scroll_active(app, state)

@spec scroll_active(module(), term()) :: term()

Delegates to Drafter.Runtime.Callback.scroll_active/2.

scroll_idle(app, state)

@spec scroll_idle(module(), term()) :: term()

Delegates to Drafter.Runtime.Callback.scroll_idle/2.

timer(app, timer_id, state)

@spec timer(module(), term(), term()) :: term()

Passes a fired timer to the app's update/2 as {:timer, timer_id}.

Returns whatever update/2 returns, without the equality check applied by handle_input/3.