From fa7e9b89308bff6fdadb969fe86a5056dfe860bc Mon Sep 17 00:00:00 2001 From: Eaden McKee <_@eaden.net> Date: Sun, 22 Dec 2024 12:54:17 +1300 Subject: [PATCH 1/2] Update phoenix_live_view.ex Document edge case --- lib/phoenix_live_view.ex | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/phoenix_live_view.ex b/lib/phoenix_live_view.ex index 0f6a95823..5daad9135 100644 --- a/lib/phoenix_live_view.ex +++ b/lib/phoenix_live_view.ex @@ -1668,7 +1668,7 @@ defmodule Phoenix.LiveView do When inserting single items using `stream_insert/4`, the limit needs to be passed as an option for it to be enforced on the client: - stream_insert(socket, :songs, song, limit: -10) + (socket, :songs, song, limit: -10) ## Required DOM attributes @@ -1864,7 +1864,13 @@ defmodule Phoenix.LiveView do As shown, an existing item on the client can be updated by issuing a `stream_insert` for the existing item. When the client updates an existing item, the item will remain in the same location as it was previously, and will not be moved to the end of the - parent children. To both update an existing item and move it to another position, + parent children. + + One exception to this is if you call `stream` with a collection of items in your `mount` function, + and then `stream_insert` an updated item that was previously streamed in your `handle_params` function. + You will end up with a duplicate item. + + To both update an existing item and move it to another position, issue a `stream_delete`, followed by a `stream_insert`. For example: song = get_song!(id) From d714a3beb14d63e7524c849e1f3b5eb572b47797 Mon Sep 17 00:00:00 2001 From: Eaden McKee <_@eaden.net> Date: Sun, 22 Dec 2024 12:56:01 +1300 Subject: [PATCH 2/2] Update phoenix_live_view.ex revert typo --- lib/phoenix_live_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/phoenix_live_view.ex b/lib/phoenix_live_view.ex index 5daad9135..0cd8a8583 100644 --- a/lib/phoenix_live_view.ex +++ b/lib/phoenix_live_view.ex @@ -1668,7 +1668,7 @@ defmodule Phoenix.LiveView do When inserting single items using `stream_insert/4`, the limit needs to be passed as an option for it to be enforced on the client: - (socket, :songs, song, limit: -10) + stream_insert(socket, :songs, song, limit: -10) ## Required DOM attributes