Updating after sending event

In a live component, I have the following code:

@impl true
def handle_event(
      %{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})}

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?

Ok, my take on it is:

    send(self(), :reload_wrapper)
    {:noreply, assign(socket, on_update: :scroll_down)}

and in update() I check for on_update and run the function:

  @impl true
  def update(assigns, socket) do
    |> assign(assigns)
    |> on_update()
    |> ok()

  def scroll_down(socket) do
    push_event(socket, "action", %{id: :deduction_list_wrapper, action: :scroll_down})

A bit dirty, but it gets the job done.