Hello. I have a doubt. I’m in a LiveView process. How can I send a long-running task inside of that process without blocking it but I still want to know when that long-running task ends? That’s because I have a loading spinner that would be in true state until it finishes. Blocking the LV process until everything finishes is the easiest thing but I must not do it because I have a PubSub that updates the socket asynchronously.
loading
is the name of my assign for the loading spinner state
Here’s my code:
# the handle_info was called like this
...
send(self(), {:fetch_from_api, contents})
{:noreply, assign(socket, loading: true)}
end
@impl Phoenix.LiveView
def handle_info({:fetch_from_api, contents}, socket) do
# loops through a list and fetches result from API and inserts the on DB
# as soon as they are fetched
Task.start(fn -> MyAPI.fetch_results(contents) end)
{:noreply, assign(socket, loading: false)}
end
end
My problem is that my loading spinner doesn’t show because Task.start(fn -> MyAPI.fetch_results(contents) end)
opens another process
Removing the Task
part solves the problem because my loading spinner will end when MyAPI.fetch_results(content)
ends
But I cannot do it synchronously because I can’t block this LV process. Why? Because I have a PubSub showing me results in a HTML table asynchronously every time the data is inserted in the DB. If I do that my spinner will be true until the processing is being done but the data will be inserted at the end of that processing.
I’d appreciate any help if someone knows