Packages
phoenix_live_view
0.20.11
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.3
1.2.0-rc.2
1.2.0-rc.1
1.2.0-rc.0
1.1.32
1.1.31
1.1.30
1.1.29
1.1.28
1.1.27
1.1.26
1.1.25
1.1.24
1.1.23
1.1.22
1.1.21
1.1.20
1.1.19
1.1.18
1.1.17
1.1.16
1.1.15
1.1.14
1.1.13
1.1.12
1.1.11
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
retired
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.1.0-rc.4
1.1.0-rc.3
1.1.0-rc.2
1.1.0-rc.1
1.1.0-rc.0
1.0.18
1.0.17
1.0.16
1.0.15
1.0.14
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
retired
1.0.7
1.0.6
retired
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.9
1.0.0-rc.8
1.0.0-rc.7
1.0.0-rc.6
1.0.0-rc.5
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.20.17
0.20.16
0.20.15
0.20.14
0.20.13
0.20.12
0.20.11
0.20.10
0.20.9
0.20.8
0.20.7
0.20.6
0.20.5
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.18
0.18.17
0.18.16
0.18.15
0.18.14
0.18.13
0.18.12
0.18.11
0.18.10
0.18.9
0.18.8
0.18.7
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.14
0.17.13
0.17.12
0.17.11
0.17.10
0.17.9
0.17.8
0.17.7
0.17.6
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.8
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.6.0-dev
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
Rich, real-time user experiences with server-rendered HTML
Current section
Files
Jump to
Current section
Files
lib/phoenix_live_view/async.ex
defmodule Phoenix.LiveView.Async do
@moduledoc false
alias Phoenix.LiveView.{AsyncResult, Socket, Channel}
def start_async(%Socket{} = socket, key, func, opts \\ []) when is_function(func, 0) do
run_async_task(socket, key, func, :start, opts)
end
def assign_async(%Socket{} = socket, key_or_keys, func, opts \\ [])
when (is_atom(key_or_keys) or is_list(key_or_keys)) and
is_function(func, 0) do
keys = List.wrap(key_or_keys)
# verifies result inside task
wrapped_func = fn ->
case func.() do
{:ok, %{} = assigns} ->
if Enum.find(keys, &(not is_map_key(assigns, &1))) do
raise ArgumentError, """
expected assign_async to return map of assigns for all keys
in #{inspect(keys)}, but got: #{inspect(assigns)}
"""
else
{:ok, assigns}
end
{:error, reason} ->
{:error, reason}
other ->
raise ArgumentError, """
expected assign_async to return {:ok, map} of
assigns for #{inspect(keys)} or {:error, reason}, got: #{inspect(other)}
"""
end
end
reset = Keyword.get(opts, :reset, false)
new_assigns =
Enum.map(keys, fn key ->
case {reset, socket.assigns} do
{false, %{^key => %AsyncResult{ok?: true} = existing}} ->
{key, AsyncResult.loading(existing, keys)}
_ ->
{key, AsyncResult.loading(keys)}
end
end)
socket
|> Phoenix.Component.assign(new_assigns)
|> run_async_task(keys, wrapped_func, :assign, opts)
end
defp run_async_task(%Socket{} = socket, key, func, kind, opts) do
if Phoenix.LiveView.connected?(socket) do
lv_pid = self()
cid = cid(socket)
{:ok, pid} = if supervisor = Keyword.get(opts, :supervisor) do
Task.Supervisor.start_child(supervisor, fn ->
Process.link(lv_pid)
do_async(lv_pid, cid, key, func, kind)
end)
else
Task.start_link(fn -> do_async(lv_pid, cid, key, func, kind) end)
end
ref =
:erlang.monitor(:process, pid, alias: :reply_demonitor, tag: {__MODULE__, key, cid, kind})
send(pid, {:context, ref})
update_private_async(socket, &Map.put(&1, key, {ref, pid, kind}))
else
socket
end
end
defp do_async(lv_pid, cid, key, func, async_kind) do
receive do
{:context, ref} ->
try do
result = func.()
Channel.report_async_result(ref, async_kind, ref, cid, key, {:ok, result})
catch
catch_kind, reason ->
Process.unlink(lv_pid)
caught_result = to_exit(catch_kind, reason, __STACKTRACE__)
Channel.report_async_result(ref, async_kind, ref, cid, key, caught_result)
:erlang.raise(catch_kind, reason, __STACKTRACE__)
end
end
end
def cancel_async(%Socket{} = socket, %AsyncResult{} = result, reason) do
case result do
%AsyncResult{loading: keys} when is_list(keys) ->
new_assigns = for key <- keys, do: {key, AsyncResult.failed(result, {:exit, reason})}
socket
|> Phoenix.Component.assign(new_assigns)
|> cancel_async(keys, reason)
%AsyncResult{} ->
socket
end
end
def cancel_async(%Socket{} = socket, key, reason) do
case get_private_async(socket, key) do
{_ref, pid, _kind} when is_pid(pid) ->
Process.unlink(pid)
Process.exit(pid, reason)
socket
nil ->
socket
end
end
def handle_async(socket, maybe_component, kind, key, ref, result) do
case prune_current_async(socket, key, ref) do
{:ok, pruned_socket} ->
handle_kind(pruned_socket, maybe_component, kind, key, result)
:error ->
socket
end
end
def handle_trap_exit(socket, maybe_component, kind, key, ref, reason) do
handle_async(socket, maybe_component, kind, key, ref, {:exit, reason})
end
defp handle_kind(socket, maybe_component, :start, key, result) do
callback_mod = maybe_component || socket.view
case Phoenix.LiveView.Lifecycle.handle_async(key, result, socket) do
{:cont, %Socket{} = socket} ->
case callback_mod.handle_async(key, result, socket) do
{:noreply, %Socket{} = new_socket} ->
new_socket
other ->
raise ArgumentError, """
expected #{inspect(callback_mod)}.handle_async/3 to return {:noreply, socket}, got:
#{inspect(other)}
"""
end
{:halt, %Socket{} = socket} ->
socket
end
end
defp handle_kind(socket, _maybe_component, :assign, keys, result) do
case result do
{:ok, {:ok, %{} = assigns}} ->
new_assigns =
for {key, val} <- assigns do
{key, AsyncResult.ok(get_current_async!(socket, key), val)}
end
Phoenix.Component.assign(socket, new_assigns)
{:ok, {:error, reason}} ->
new_assigns =
for key <- keys do
{key, AsyncResult.failed(get_current_async!(socket, key), {:error, reason})}
end
Phoenix.Component.assign(socket, new_assigns)
{:exit, _reason} = normalized_exit ->
new_assigns =
for key <- keys do
{key, AsyncResult.failed(get_current_async!(socket, key), normalized_exit)}
end
Phoenix.Component.assign(socket, new_assigns)
end
end
# handle race of async being canceled and then reassigned
defp prune_current_async(socket, key, ref) do
case get_private_async(socket, key) do
{^ref, _pid, _kind} -> {:ok, update_private_async(socket, &Map.delete(&1, key))}
{_ref, _pid, _kind} -> :error
nil -> :error
end
end
defp update_private_async(%{private: private} = socket, func) do
existing = Map.get(private, :live_async, %{})
%{socket | private: Map.put(private, :live_async, func.(existing))}
end
defp get_private_async(%Socket{} = socket, key) do
socket.private[:live_async][key]
end
defp get_current_async!(socket, key) do
# handle case where assign is temporary and needs to be rebuilt
case socket.assigns do
%{^key => %AsyncResult{} = current_async} -> current_async
%{^key => _other} -> AsyncResult.loading(key)
%{} -> raise ArgumentError, "missing async assign #{inspect(key)}"
end
end
defp to_exit(:throw, reason, stack), do: {:exit, {{:nocatch, reason}, stack}}
defp to_exit(:error, reason, stack), do: {:exit, {reason, stack}}
defp to_exit(:exit, reason, _stack), do: {:exit, reason}
defp cid(%Socket{} = socket) do
if myself = socket.assigns[:myself], do: myself.cid
end
end