In a live component, I have the following code:
@impl true
def handle_event(
"add_item",
_,
%{assigns: %{wrapper: wrapper}} = socket
) do
{:ok, _new_item} = create_item(wrapper)
send(self(), :reload_wrapper)
{:noreply, push_event(socket, "action", %{id: :deduction_list_wrapper, action: :scroll_down})}
end
Basically wrapper
is loaded at the live view level and dispatched to different child components.
A component has an add button, and this component will create a new item and trigger a reload at the live view level. I need to reload the whole wrapper because of how everything is structured.
Now, I’d like to dispatch the push_event
AFTER the send()
triggered update. The send()
call will reload the wrapper from the database and pass it along to the current component.
So now I have:
(L is live view, C is live component, JS is client)
1 send() # C
2 push_event() # C
3 update # JS (new item is not here yet)
4 handle_event # JS
5 handle_info # L
6 update(wrapper: new_wrapper) # C
7 update # JS (new item is added)
So I want 4 to be pushed at the end.
How would you handle this kind of situation?