Hi everyone,
I’m working with a stream of a simple list with “songs” being prepended or appended to the list.
I’m able to see the code-paths being hit, where the stream is supposed to either reset or prepend data. Unfortunately, only appending works as expected, and prepending and resetting does not. Because I’m persisting the list prior to updating the stream, when I refresh, I see the correct order.
Can you help me diagnose what I’m doing wrong? I’ve also attached a screenshot of the phenomenon for the resetting case.
Package versions:
{:phoenix_html, "~> 3.3"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.20.14"},
The following code is from the LiveView:
@impl true
def mount(_params, _session, socket) do
{
:ok,
socket
|> stream(:song_queue, [])
}
end
@impl true
def handle_info({:queue_next, %EmbeddedSong{} = song}, socket) do
Listening.queue_song_next(socket.assigns.room, song.apple_id)
{
:noreply,
socket
|> stream_insert(:song_queue, song, at: 0)
|> put_flash(:info, "Added '#{song.name}' to start of queue.")
}
end
@impl true
def handle_event("clear_queue", _params, socket) do
Listening.clear_queue!(socket.assigns.room)
{
:noreply,
socket
|> stream(:song_queue, [], reset: true)
|> put_flash(:info, "Cleared the queue.")
}
end
@impl true
def render(assigns) do
~H"""
<p>Listening</p>
<.live_component module={SearchComponent} id="search-music-component" />
<br />
<hr />
<div>
<p>Queue</p>
<ol id="song-queue-list" phx-update="stream">
<li :for={{_id, song} <- @streams.song_queue}>
<%= song.name %> by <%= song.artist %>
</li>
</ol>
<button phx-click="clear_queue">Clear</button>
</div>
"""
end