Skip to content

Commit

Permalink
docs: clarify async_result slots rendering (#2862)
Browse files Browse the repository at this point in the history
Make it clear loading and result slots aren't rendered again on
subsequent assign_async calls after the first successful result
  • Loading branch information
gfrancischelli authored Dec 18, 2023
1 parent 69a801a commit 8a5315d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/phoenix_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2945,6 +2945,8 @@ defmodule Phoenix.Component do

@doc """
Renders an async assign with slots for the different loading states.
The result state takes precedence over subsequent loading and failed
states.
*Note*: The inner block receives the result of the async assign as a :let.
The let is only accessible to the inner block and is not in scope to the
Expand All @@ -2963,19 +2965,29 @@ defmodule Phoenix.Component do
<% end %>
</.async_result>
```
To display loading and failed states again on subsequent `assign_async` calls,
reset the assign to a result-free `%AsyncResult{}`:
```elixir
{:noreply,
socket
|> assign_async(:page, :data, &reload_data/0)
|> assign(:page, AsyncResult.loading())}
```
"""
@doc type: :component
attr.(:assign, AsyncResult, required: true)
slot.(:loading, doc: "rendered while the assign is loading")
slot.(:loading, doc: "rendered while the assign is loading for the first time")

slot.(:failed,
doc:
"rendered when an error or exit is caught or assign_async returns `{:error, reason}`. Receives the error as a :let."
"rendered when an error or exit is caught or assign_async returns `{:error, reason}` for the first time. Receives the error as a `:let`"
)

slot.(:inner_block,
doc:
"rendered when the assign is loaded successfully via AsyncResult.ok/2. Receives the result as a :let"
"rendered when the assign is loaded successfully via `AsyncResult.ok/2`. Receives the result as a `:let`"
)

def async_result(%{assign: async_assign} = assigns) do
Expand Down

0 comments on commit 8a5315d

Please sign in to comment.