Skip to content

Commit 51f3072

Browse files
authored
Fix assign_async return docs (#4236)
* Fix assign_async return docs * Reject keyword assigns in assign_async tests
1 parent af1631c commit 51f3072

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

lib/phoenix_live_view.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,8 +2245,8 @@ defmodule Phoenix.LiveView do
22452245
an `Phoenix.LiveView.AsyncResult` struct holding the status of the operation
22462246
and the result when the function completes.
22472247
2248-
The function must return either a map or a keyword list with the assigns
2249-
to merge into the socket.
2248+
The function must return either `{:ok, assigns}` or `{:error, reason}`,
2249+
where `assigns` is a map with the keys passed to `assign_async/3`.
22502250
22512251
The task is only started when the socket is connected.
22522252

lib/phoenix_live_view/async.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ defmodule Phoenix.LiveView.Async do
363363

364364
defp handle_kind(socket, _maybe_component, :assign, keys, result) do
365365
case result do
366-
{:ok, {:ok, assigns}} when is_map(assigns) or is_list(assigns) ->
366+
{:ok, {:ok, assigns}} when is_map(assigns) ->
367367
new_assigns =
368368
for {key, val} <- assigns do
369369
{key, AsyncResult.ok(get_current_async!(socket, key), val)}

test/phoenix_live_view/integrations/assign_async_test.exs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
3232
assert render(lv)
3333
end
3434

35+
test "keyword list return", %{conn: conn} do
36+
{:ok, lv, _html} = live(conn, "/assign_async?test=bad_keyword")
37+
38+
assert render_async(lv) =~
39+
"expected assign_async to return {:ok, map} of\\nassigns for [:data] or {:error, reason}, got: {:ok, [data: 123]}"
40+
41+
assert render(lv)
42+
end
43+
3544
test "valid return", %{conn: conn} do
3645
{:ok, lv, _html} = live(conn, "/assign_async?test=ok")
3746
assert render_async(lv) =~ "data: 123"
@@ -222,7 +231,9 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
222231

223232
test "valid return", %{conn: conn} do
224233
{:ok, lv, _html} = live(conn, "/assign_async?test=sup_ok")
225-
assert render_async(lv) =~ "data: 123"
234+
html = render_async(lv)
235+
assert html =~ "data: 123"
236+
refute html =~ "expected assign_async to return"
226237
end
227238

228239
test "raise during execution", %{conn: conn} do

test/support/live_views/assign_async.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ defmodule Phoenix.LiveViewTest.Support.AssignAsyncLive do
4040
{:ok, assign_async(socket, :data, fn -> {:ok, %{bad: 123}} end)}
4141
end
4242

43+
def mount(%{"test" => "bad_keyword"}, _session, socket) do
44+
{:ok, assign_async(socket, :data, fn -> {:ok, data: 123} end)}
45+
end
46+
4347
def mount(%{"test" => "ok"}, _session, socket) do
4448
{:ok, assign_async(socket, :data, fn -> {:ok, %{data: 123}} end)}
4549
end
4650

4751
def mount(%{"test" => "sup_ok"}, _session, socket) do
4852
{:ok,
49-
assign_async(socket, :data, fn -> {:ok, data: 123} end, supervisor: TestAsyncSupervisor)}
53+
assign_async(socket, :data, fn -> {:ok, %{data: 123}} end, supervisor: TestAsyncSupervisor)}
5054
end
5155

5256
def mount(%{"test" => "raise"}, _session, socket) do

0 commit comments

Comments
 (0)