msantoli

msantoli

Assign_async misbehaving with multiple live_views

We are developing a phoenix application where we have a dashbaord with multiple widgets.
Each widdget is a liveview and they are rendered at runtime in the main liveview page:

<%= live_render(
@socket,
module_name,
id: dashboard_widget.id,
session: %{
some_params
}
) %>

In some of these widgets we have assign_async to lazy load data.
If we have only 1 widget in the dashbaord everything is working perfectly.
If we had more thatn 1 widget with assign_asyncs inside, it can happens that on one of the widgets the asyng variable is not updated correctly, we always have the loading state set to true.
Putting IO.inspect to tdebug the async_task is working properly and returns without errors, it looks like it is the liveview variables that don’t get updated.

Thanks
Massimo

Most Liked

gushonorato

gushonorato

It’s hard to help without seeing the code from the parent and child LiveViews, but I have a question: is there any good reason to use LiveViews for implementing the widgets instead of a LiveComponent?

gushonorato

gushonorato

It looks like you’re running into some race conditions with your LiveView updates. I think the issue is that push_state is happening before the ui_state is ready. Try splitting the logic for updating the socket and calling push_event into two separate functions. For example:

def handle_async(:load_ui_data, {:ok, fetched_data}, socket) do
  %{ui_data: ui_data} = socket.assigns
  ui_data = AsyncResult.ok(ui_data, fetched_data)
  socket = assign(socket, :ui_data, ui_data)

  send(self(), :update_timer)

  {:noreply, socket}
end

I get why you’re nesting LiveViews, but if it were up to me, I’d go with a LiveView + LiveComponent setup and a central broker. By consolidating state updates in a single place, you can more effectively manage transitions and reduce the chance of race conditions. Furthermore, using fewer LiveViews and more LiveComponents with a central broker reduces the number of processes the application needs to open, which lowers resource consumption and ensures better scalability as the application grows.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement