Skip to content

Commit

Permalink
reduce in insert order
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Jan 18, 2024
1 parent aafffe3 commit a99b22f
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions lib/phoenix_live_view/test/dom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -431,44 +431,48 @@ defmodule Phoenix.LiveViewTest.DOM do
children = updated_existing_children ++ updated_appended

new_children =
Enum.reduce(stream_inserts, children, fn {id, {ref, insert_at, _limit}}, acc ->
old_index = Enum.find_index(acc, &(attribute(&1, "id") == id))
Enum.reduce(streams, children, fn item, acc ->
[ref, inserts, _deletes | _maybe_reset] = item

appended? = Enum.any?(updated_appended, &(attribute(&1, "id") == id))
Enum.reduce(inserts, acc, fn [id, insert_at, _limit], acc ->
old_index = Enum.find_index(acc, &(attribute(&1, "id") == id))

existing? = Enum.any?(updated_existing_children, &(attribute(&1, "id") == id))
deleted? = MapSet.member?(stream_deletes, id)
appended? = Enum.any?(updated_appended, &(attribute(&1, "id") == id))

child =
case old_index && Enum.at(acc, old_index) do
nil -> nil
child -> set_attr(child, "data-phx-stream", ref)
end
existing? = Enum.any?(updated_existing_children, &(attribute(&1, "id") == id))
deleted? = MapSet.member?(stream_deletes, id)

parent_id = parent_id(html_tree, id)
child =
case old_index && Enum.at(acc, old_index) do
nil -> nil
child -> set_attr(child, "data-phx-stream", ref)
end

cond do
!child ->
acc
parent_id = parent_id(html_tree, id)

# skip added children that aren't ours if they are not being appended
not appended? && parent_id && parent_id != container_id ->
acc
cond do
!child ->
acc

# do not append existing child if already present, only update in place
old_index && insert_at == -1 && (existing? or appended?) ->
if deleted? do
acc |> List.delete_at(old_index) |> List.insert_at(insert_at, child)
else
List.replace_at(acc, old_index, child)
end
# skip added children that aren't ours if they are not being appended
not appended? && parent_id && parent_id != container_id ->
acc

old_index && insert_at ->
acc |> List.delete_at(old_index) |> List.insert_at(insert_at, child)
# do not append existing child if already present, only update in place
old_index && insert_at == -1 && (existing? or appended?) ->
if deleted? do
acc |> List.delete_at(old_index) |> List.insert_at(insert_at, child)
else
List.replace_at(acc, old_index, child)
end

!old_index && insert_at ->
List.insert_at(acc, insert_at, child)
end
old_index && insert_at ->
acc |> List.delete_at(old_index) |> List.insert_at(insert_at, child)

!old_index && insert_at ->
List.insert_at(acc, insert_at, child)
end
end)
end)
|> Enum.reject(fn child ->
id = attribute(child, "id")
Expand Down

0 comments on commit a99b22f

Please sign in to comment.