Preload AsyncResult


Is there a way to preload %AsyncResult{}? My UI is a chat widget where the messages are an array of messages wrapped inside AsyncResult.

When a user submits a new message, I want to make an optimistic update before I need to do some network and DB operations. In my handle_event, can I assign to the socket’s :messages my current messages ++ [new_message] before also running assign_async on the same key?


After thinking about it more, it seems like I could do something like:

|> assign(:messages, %AsyncResult{ok?: true, result: "Hello"})
|> assign_async(:messages, fn -> {:ok, %{messages: something_async()}} end)

One thing that I like about the new LV stream/stream_insert APIs is that they require each element to have an :id, which makes reasoning about these things easy: insert your optimistic render into the stream with an id immediately, and when the async result comes back with the same id, inserting it into the collection will replace the optimistic render. You might be able to extend that pattern to your assigns as well.