Reply (Reply v1.2.1)

Copy Markdown View Source

Pipe the response of Phoenix LiveView and GenServer callbacks instead of building tuples by hand.

def mount(_params, _session, socket) do
  socket
  |> assign(:posts, Blog.list_posts())
  |> ok()
end

See the README for installation.

Overview

FunctionReturnsLiveViewGenServer
ok(socket_or_state){:ok, socket_or_state}✓✓
ok(socket_or_state, opts_or_action){:ok, socket_or_state, opts_or_action}✓✓
noreply(socket_or_state){:noreply, socket_or_state}✓✓
noreply(state, action){:noreply, state, action}✓
reply(socket_or_state, payload){:reply, payload, socket_or_state}✓✓
reply(state, payload, action){:reply, payload, state, action}✓
stop(state, reason){:stop, reason, state}✓
stop(state, reason, payload){:stop, reason, payload, state}✓

An action is a timeout, :hibernate or {:continue, term}.

In a Phoenix.Channel, use import Reply, except: [reply: 2] to avoid a conflict with Phoenix.Channel.reply/2.

Summary

Functions

Returns {:noreply, socket_or_state}.

Returns {:noreply, state, action}.

Returns {:ok, socket_or_state}.

Returns {:ok, socket_or_state, opts_or_action}.

Returns {:reply, payload, socket_or_state}.

Returns {:reply, payload, state, action}.

Returns {:stop, reason, state}.

Returns {:stop, reason, payload, state}.

Functions

noreply(socket_or_state)

Returns {:noreply, socket_or_state}.

iex> %{count: 0} |> noreply()
{:noreply, %{count: 0}}

noreply(state, action)

Returns {:noreply, state, action}.

iex> %{count: 0} |> noreply(:hibernate)
{:noreply, %{count: 0}, :hibernate}

ok(socket_or_state)

Returns {:ok, socket_or_state}.

iex> %{count: 0} |> ok()
{:ok, %{count: 0}}

ok(socket_or_state, opts_or_action)

Returns {:ok, socket_or_state, opts_or_action}.

Mount options in a LiveView, an action in a GenServer.

iex> %{count: 0} |> ok({:continue, :load})
{:ok, %{count: 0}, {:continue, :load}}

reply(socket_or_state, payload)

Returns {:reply, payload, socket_or_state}.

iex> %{count: 1} |> reply(:ok)
{:reply, :ok, %{count: 1}}

reply(state, payload, action)

Returns {:reply, payload, state, action}.

iex> %{count: 1} |> reply(:ok, {:continue, :persist})
{:reply, :ok, %{count: 1}, {:continue, :persist}}

stop(state, reason)

Returns {:stop, reason, state}.

iex> %{count: 1} |> stop(:normal)
{:stop, :normal, %{count: 1}}

stop(state, reason, payload)

Returns {:stop, reason, payload, state}.

iex> %{count: 1} |> stop(:normal, :ok)
{:stop, :normal, :ok, %{count: 1}}