Hi,
I have a stateful LiveComponent where I would like to handle a timeout. This timeout should occur a few seconds after one of the handle_event
functions is no longer being called.
I already did this in a LiveView and it was super easy: I call Process.send_after
and do the work in the matching handle_info
that is called with socket
as a parameter.
In a LiveComponent, handle_info
does not exist. Handling this in the parent LiveView won’t work because I need to alter the socket
from the component but the socket
passed to handle_info
is the parent LiveView’s socket
.
In every scenario I tried I end up with the following problem: at one point I need to update the socket
after the delay, so I do something like this:
def start_timeout(socket) do
Process.sleep(2000)
<send event with the socket with whatever tool>
end
The problem with this approach is that there is no garantee that the socket was not altered during the sleep time. And I really have no idea, with my actual knowledge of Elixir and Phoenix, how to handle this case.
Any clues on how this can be done at the LiveComponent’s level? At this point the only solution I see is to migrate the LiveComponent’s code in the LiveView and use handle_info
from there. I know this is not the best solution as I would loose the modularity and end up with monolithic Liveviews. But I am just not ready to dig in LiveViews code to fully understand the inner logic.
Thank you.