sezaru

sezaru

How to make an async call in the component update_many callback?

The update_many/1 callback let’s you make your update call more efficient by handing all update calls from the same component type all at once.

The documentation shows an example where instead of doing a query to the DB for each component, you do it only once inside the update_many/1 callback.

That’s great, but the DB call is still a blocking operation in the LV, meaning that if this query is slow, the LV component will also not finish loading until the callback is done.

The “obvious” solution for that would be to leverage LV’s async functionality by using start_async or assign_async, that way I can make the DB call not block anymore.

The issue now is that I’m back to doing one DB call for each async operation.

I can’t see how to still keep one DB call that update_many/1 allows but doing it without blocking the component.

The only way I can see that being possible right now would be to start a new process in the update_many/1 callback, and inside of it call a bunch of send_update for each component with its reply… Something like this:

def update_many(assigns_and_sockets) do
  ids =
    Enum.map(assigns_and_socket, fn {%{id: id, component_id: comp_id}, _socket} ->
      {id, comp_id}
    end)

  Task.start(fn ->
     results = ids |> Enum.map(fn {id, _} -> ids end) |> DB.get!()

    Enum.each(results, fn {id, comp_id} ->
      send_update __MODULE__, id: com_id, result: results[id]
    end)
  end)

  Enum.map(assigns_and_sockets, fn {assigns, socket} ->
    {assign(assigns, loading?: true), socket}
  end)
end

It is missing the code to handle the send_update message, but this should work. But doing something like this just feels wrong since I’m kinda just “re-implementing” the async operations (and, at least with the above code, without its great guarantees).

Why is this not already built-in in LV’s api? Or, if it is, where can I read more about it?

Marked As Solved

sezaru

sezaru

So, in the end I decided to create a library that does this myself.

You can check it out here: LiveBulkAsync - A library to allow LV's async functionality in update_many function

It should has all the same guarantees that the LV’s async functions has (unless I messed something up :sweat_smile:)

Also Liked

drikanius

drikanius

Funny enough, a few weeks ago I fell into the same rabbit hole. I would really love a more interesting approach to this problem

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement