diff --git a/lib/phoenix_live_view/channel.ex b/lib/phoenix_live_view/channel.ex index 6312e287b..44269f4e0 100644 --- a/lib/phoenix_live_view/channel.ex +++ b/lib/phoenix_live_view/channel.ex @@ -950,14 +950,16 @@ defmodule Phoenix.LiveView.Channel do defp render_diff(state, socket, force?) do changed? = Utils.changed?(socket) - {socket, diff, components} = + {socket, diff, fingerprints, components} = if force? or changed? do :telemetry.span( [:phoenix, :live_view, :render], %{socket: socket, force?: force?, changed?: changed?}, fn -> rendered = Phoenix.LiveView.Renderer.to_rendered(socket, socket.view) - {socket, diff, components} = Diff.render(socket, rendered, state.components) + + {diff, fingerprints, components} = + Diff.render(socket, rendered, state.fingerprints, state.components) socket = socket @@ -965,19 +967,20 @@ defmodule Phoenix.LiveView.Channel do |> Utils.clear_changed() { - {socket, diff, components}, + {socket, diff, fingerprints, components}, %{socket: socket, force?: force?, changed?: changed?} } end ) else - {socket, %{}, state.components} + {socket, %{}, state.fingerprints, state.components} end diff = Diff.render_private(socket, diff) new_socket = Utils.clear_temp(socket) - {:diff, diff, %{state | socket: new_socket, components: components}} + {:diff, diff, + %{state | socket: new_socket, fingerprints: fingerprints, components: components}} end defp reply(state, {ref, extra}, status, payload) do @@ -1373,6 +1376,7 @@ defmodule Phoenix.LiveView.Channel do socket: lv_socket, topic: phx_socket.topic, components: Diff.new_components(), + fingerprints: Diff.new_fingerprints(), upload_names: %{}, upload_pids: %{} } diff --git a/lib/phoenix_live_view/diff.ex b/lib/phoenix_live_view/diff.ex index d124bc4f2..32506427d 100644 --- a/lib/phoenix_live_view/diff.ex +++ b/lib/phoenix_live_view/diff.ex @@ -123,15 +123,16 @@ defmodule Phoenix.LiveView.Diff do Renders a diff for the rendered struct in regards to the given socket. """ def render( - %{fingerprints: {expected, _}} = socket, + socket, %Rendered{fingerprint: actual} = rendered, + {expected, _}, {_, _, uuids} ) when expected != nil and expected != actual do - render(%{socket | fingerprints: new_fingerprints()}, rendered, new_components(uuids)) + render(socket, rendered, new_fingerprints(), new_components(uuids)) end - def render(%{fingerprints: prints} = socket, %Rendered{} = rendered, components) do + def render(socket, %Rendered{} = rendered, prints, components) do {diff, prints, pending, components, nil} = traverse(socket, rendered, prints, %{}, components, nil, true) @@ -142,10 +143,9 @@ defmodule Phoenix.LiveView.Diff do {cdiffs, components} = render_pending_components(socket, pending, cid_to_component, %{}, components) - socket = %{socket | fingerprints: prints} diff = maybe_put_title(diff, socket) {diff, cdiffs} = extract_events({diff, cdiffs}) - {socket, maybe_put_cdiffs(diff, cdiffs), components} + {maybe_put_cdiffs(diff, cdiffs), prints, components} end defp maybe_put_cdiffs(diff, cdiffs) when cdiffs == %{}, do: diff @@ -202,16 +202,16 @@ defmodule Phoenix.LiveView.Diff do {cids, _, _} = components case cids do - %{^cid => {component, id, assigns, private, fingerprints}} -> + %{^cid => {component, id, assigns, private, prints}} -> {csocket, extra} = socket - |> configure_socket_for_component(assigns, private, fingerprints) + |> configure_socket_for_component(assigns, private) |> fun.(component) diff = render_private(csocket, %{}) {pending, cdiffs, components} = - render_component(csocket, component, id, cid, false, cids, %{}, components) + render_component(csocket, component, id, prints, cid, false, cids, %{}, components) {cdiffs, components} = render_pending_components(socket, pending, cids, cdiffs, components) @@ -233,9 +233,9 @@ defmodule Phoenix.LiveView.Diff do {cid_to_component, _id_to_cid, _} = components case cid_to_component do - %{^cid => {component, _id, assigns, private, fingerprints}} -> + %{^cid => {component, _id, assigns, private, _prints}} -> socket - |> configure_socket_for_component(assigns, private, fingerprints) + |> configure_socket_for_component(assigns, private) |> fun.(component) %{} -> @@ -658,21 +658,21 @@ defmodule Phoenix.LiveView.Diff do "for component #{inspect(component)} when rendering template" end - {socket, components} = + {socket, components, prints} = case cids do %{^cid => {_component, _id, assigns, private, prints}} -> {private, components} = unmark_for_deletion(private, components) - {configure_socket_for_component(socket, assigns, private, prints), components} + {configure_socket_for_component(socket, assigns, private), components, prints} %{} -> myself_assigns = %{myself: %Phoenix.LiveComponent.CID{cid: cid}} {mount_component(socket, component, myself_assigns), - put_cid(components, component, id, cid)} + put_cid(components, component, id, cid), new_fingerprints()} end assigns_sockets = [{new_assigns, socket} | assigns_sockets] - metadata = [{cid, id, new?} | metadata] + metadata = [{cid, id, prints, new?} | metadata] seen_ids = Map.put(seen_ids, [component | id], true) {assigns_sockets, metadata, components, seen_ids} end) @@ -709,7 +709,7 @@ defmodule Phoenix.LiveView.Diff do defp zip_components( [%{__struct__: Phoenix.LiveView.Socket} = socket | sockets], - [{cid, id, new?} | metadata], + [{cid, id, prints, new?} | metadata], component, cids, {pending, diffs, components} @@ -717,7 +717,7 @@ defmodule Phoenix.LiveView.Diff do diffs = maybe_put_events(diffs, socket) {new_pending, diffs, components} = - render_component(socket, component, id, cid, new?, cids, diffs, components) + render_component(socket, component, id, prints, cid, new?, cids, diffs, components) pending = Map.merge(pending, new_pending, fn _, v1, v2 -> v2 ++ v1 end) zip_components(sockets, metadata, component, cids, {pending, diffs, components}) @@ -768,17 +768,17 @@ defmodule Phoenix.LiveView.Diff do "as the list of assigns given, got: #{inspect(preloaded)}" end - defp render_component(socket, component, id, cid, new?, cids, diffs, components) do + defp render_component(socket, component, id, prints, cid, new?, cids, diffs, components) do changed? = new? or Utils.changed?(socket) - {socket, pending, diff, {cid_to_component, id_to_cid, uuids}} = + {socket, prints, pending, diff, components} = if changed? do rendered = component_to_rendered(socket, component, id) {changed?, linked_cid, prints} = - maybe_reuse_static(rendered, socket, component, cids, components) + maybe_reuse_static(rendered, component, prints, cids, components) - {diff, component_prints, pending, components, nil} = + {diff, prints, pending, components, nil} = traverse(socket, rendered, prints, %{}, components, nil, changed?) children_cids = @@ -790,13 +790,12 @@ defmodule Phoenix.LiveView.Diff do socket = put_in(socket.private.children_cids, children_cids) - |> Map.replace!(:fingerprints, component_prints) |> Lifecycle.after_render() |> Utils.clear_changed() - {socket, pending, diff, components} + {socket, prints, pending, diff, components} else - {socket, %{}, %{}, components} + {socket, prints, %{}, %{}, components} end diffs = @@ -806,8 +805,13 @@ defmodule Phoenix.LiveView.Diff do diffs end - socket = Utils.clear_temp(socket) - cid_to_component = Map.put(cid_to_component, cid, dump_component(socket, component, id)) + dump = + socket + |> Utils.clear_temp() + |> dump_component(component, id, prints) + + {cid_to_component, id_to_cid, uuids} = components + cid_to_component = Map.put(cid_to_component, cid, dump) {pending, diffs, {cid_to_component, id_to_cid, uuids}} end @@ -850,18 +854,18 @@ defmodule Phoenix.LiveView.Diff do # that will be changed before it is sent to the client. # # We don't want to traverse all of the components, so we will try it @attempts times. - defp maybe_reuse_static(rendered, socket, component, old_cids, components) do + defp maybe_reuse_static(rendered, component, prints, old_cids, components) do {new_cids, id_to_cid, _uuids} = components + {current_print, _} = prints %{fingerprint: print} = rendered - %{fingerprints: {socket_print, _} = socket_prints} = socket - with true <- socket_print != print, + with true <- current_print != print, iterator = :maps.iterator(Map.fetch!(id_to_cid, component)), {cid, existing_prints} <- find_same_component_print(print, iterator, old_cids, new_cids, @attempts) do {false, cid, existing_prints} else - _ -> {true, nil, socket_prints} + _ -> {true, nil, prints} end end @@ -921,23 +925,22 @@ defmodule Phoenix.LiveView.Diff do |> Map.put(:lifecycle, %Phoenix.LiveView.Lifecycle{}) socket = - configure_socket_for_component(socket, assigns, private, new_fingerprints()) + configure_socket_for_component(socket, assigns, private) |> Utils.assign(:flash, %{}) Utils.maybe_call_live_component_mount!(socket, component) end - defp configure_socket_for_component(socket, assigns, private, prints) do + defp configure_socket_for_component(socket, assigns, private) do %{ socket | assigns: Map.put(assigns, :__changed__, %{}), private: private, - fingerprints: prints, redirected: nil } end - defp dump_component(socket, component, id) do - {component, id, socket.assigns, socket.private, socket.fingerprints} + defp dump_component(socket, component, id, prints) do + {component, id, socket.assigns, socket.private, prints} end end diff --git a/lib/phoenix_live_view/socket.ex b/lib/phoenix_live_view/socket.ex index 55bfcc503..ba5b41040 100644 --- a/lib/phoenix_live_view/socket.ex +++ b/lib/phoenix_live_view/socket.ex @@ -67,7 +67,6 @@ defmodule Phoenix.LiveView.Socket do router: nil, assigns: %{__changed__: %{}}, private: %{live_temp: %{}}, - fingerprints: Phoenix.LiveView.Diff.new_fingerprints(), redirected: nil, host_uri: nil, transport_pid: nil @@ -78,8 +77,6 @@ defmodule Phoenix.LiveView.Socket do @typedoc "The data in a LiveView as stored in the socket." @type assigns :: map | assigns_not_in_socket() - @type fingerprints :: {nil, map} | {binary, map} - @type t :: %__MODULE__{ id: binary(), endpoint: module(), @@ -89,7 +86,6 @@ defmodule Phoenix.LiveView.Socket do router: module(), assigns: assigns, private: map(), - fingerprints: fingerprints, redirected: nil | tuple(), host_uri: URI.t() | :not_mounted_at_router, transport_pid: pid() | nil diff --git a/lib/phoenix_live_view/static.ex b/lib/phoenix_live_view/static.ex index 72500f414..19f27f300 100644 --- a/lib/phoenix_live_view/static.ex +++ b/lib/phoenix_live_view/static.ex @@ -285,7 +285,10 @@ defmodule Phoenix.LiveView.Static do defp to_rendered_content_tag(socket, tag, view, attrs) do rendered = Phoenix.LiveView.Renderer.to_rendered(socket, view) - {_, diff, _} = Diff.render(socket, rendered, Diff.new_components()) + + {diff, _, _} = + Diff.render(socket, rendered, Diff.new_fingerprints(), Diff.new_components()) + content_tag(tag, attrs, Diff.to_iodata(diff)) end diff --git a/lib/phoenix_live_view/test/live_view_test.ex b/lib/phoenix_live_view/test/live_view_test.ex index 84b3ac826..4a85d8875 100644 --- a/lib/phoenix_live_view/test/live_view_test.ex +++ b/lib/phoenix_live_view/test/live_view_test.ex @@ -492,7 +492,9 @@ defmodule Phoenix.LiveViewTest do end defp rendered_to_diff_string(rendered, socket) do - {_, diff, _} = Diff.render(socket, rendered, Diff.new_components()) + {diff, _, _} = + Diff.render(socket, rendered, Diff.new_fingerprints(), Diff.new_components()) + diff |> Diff.to_iodata() |> IO.iodata_to_binary() end diff --git a/test/phoenix_live_view/diff_test.exs b/test/phoenix_live_view/diff_test.exs index 2a09443f4..413d07a32 100644 --- a/test/phoenix_live_view/diff_test.exs +++ b/test/phoenix_live_view/diff_test.exs @@ -76,8 +76,7 @@ defmodule Phoenix.LiveView.DiffTest do fingerprints \\ Diff.new_fingerprints(), components \\ Diff.new_components() ) do - socket = %Socket{endpoint: __MODULE__, fingerprints: fingerprints} - Diff.render(socket, rendered, components) + Diff.render(%Socket{endpoint: __MODULE__}, rendered, fingerprints, components) end defp rendered_to_binary(map) do @@ -134,7 +133,7 @@ defmodule Phoenix.LiveView.DiffTest do describe "full renders without fingerprints" do test "basic template" do rendered = basic_template(%{time: "10:30", subtitle: "Sunny"}) - {socket, full_render, _} = render(rendered) + {full_render, fingerprints, _} = render(rendered) assert full_render == %{ 0 => "10:30", @@ -146,12 +145,12 @@ defmodule Phoenix.LiveView.DiffTest do assert rendered_to_binary(full_render) == "
\n

It's 10:30

\n Sunny\n
" - assert socket.fingerprints == {rendered.fingerprint, %{}} + assert fingerprints == {rendered.fingerprint, %{}} end test "template with literal" do rendered = literal_template(%{title: "foo"}) - {socket, full_render, _} = render(rendered) + {full_render, fingerprints, _} = render(rendered) assert full_render == %{0 => "foo", 1 => "<div>", :s => ["
\n ", "\n ", "\n
"], :r => 1} @@ -159,11 +158,11 @@ defmodule Phoenix.LiveView.DiffTest do assert rendered_to_binary(full_render) == "
\n foo\n <div>\n
" - assert socket.fingerprints == {rendered.fingerprint, %{}} + assert fingerprints == {rendered.fingerprint, %{}} end test "nested %Rendered{}'s" do - {socket, full_render, _} = render(nested_rendered()) + {full_render, fingerprints, _} = render(nested_rendered()) assert full_render == %{ @@ -180,14 +179,14 @@ defmodule Phoenix.LiveView.DiffTest do assert rendered_to_binary(full_render) == "

hi

s1abcs2efgs3s1efgs2" - assert socket.fingerprints == {123, %{2 => {789, %{}}, 1 => {456, %{}}}} + assert fingerprints == {123, %{2 => {789, %{}}, 1 => {456, %{}}}} end test "comprehensions" do %{fingerprint: fingerprint} = rendered = comprehension_template(%{title: "Users", names: ["phoenix", "elixir"]}) - {socket, full_render, _} = render(rendered) + {full_render, fingerprints, _} = render(rendered) assert full_render == %{ 0 => "Users", @@ -199,7 +198,7 @@ defmodule Phoenix.LiveView.DiffTest do } } - assert {^fingerprint, %{1 => comprehension_print}} = socket.fingerprints + assert {^fingerprint, %{1 => comprehension_print}} = fingerprints assert is_integer(comprehension_print) end @@ -208,7 +207,7 @@ defmodule Phoenix.LiveView.DiffTest do %{fingerprint: fingerprint} = rendered = comprehension_template(%{title: "Users", names: []}) - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => "Users", @@ -217,12 +216,12 @@ defmodule Phoenix.LiveView.DiffTest do 1 => "" } - assert {^fingerprint, inner} = socket.fingerprints + assert {^fingerprint, inner} = fingerprints assert inner == %{} # Making them non-empty adds a fingerprint rendered = comprehension_template(%{title: "Users", names: ["phoenix", "elixir"]}) - {socket, full_render, components} = render(rendered, socket.fingerprints, components) + {full_render, fingerprints, components} = render(rendered, fingerprints, components) assert full_render == %{ 0 => "Users", @@ -232,19 +231,19 @@ defmodule Phoenix.LiveView.DiffTest do } } - assert {^fingerprint, %{1 => comprehension_print}} = socket.fingerprints + assert {^fingerprint, %{1 => comprehension_print}} = fingerprints assert is_integer(comprehension_print) # Making them empty again does not reset the fingerprint rendered = comprehension_template(%{title: "Users", names: []}) - {socket, full_render, _components} = render(rendered, socket.fingerprints, components) + {full_render, fingerprints, _components} = render(rendered, fingerprints, components) assert full_render == %{ 0 => "Users", 1 => %{d: []} } - assert {^fingerprint, %{1 => ^comprehension_print}} = socket.fingerprints + assert {^fingerprint, %{1 => ^comprehension_print}} = fingerprints end test "nested comprehensions" do @@ -256,7 +255,7 @@ defmodule Phoenix.LiveView.DiffTest do scores: [1, 2] }) - {socket, full_render, _} = render(rendered) + {full_render, fingerprints, _} = render(rendered) assert full_render == %{ 0 => "Users", @@ -272,7 +271,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert {^fingerprint, %{1 => comprehension_print}} = socket.fingerprints + assert {^fingerprint, %{1 => comprehension_print}} = fingerprints assert is_integer(comprehension_print) end end @@ -280,31 +279,31 @@ defmodule Phoenix.LiveView.DiffTest do describe "diffed render with fingerprints" do test "basic template skips statics for known fingerprints" do rendered = basic_template(%{time: "10:30", subtitle: "Sunny"}) - {socket, full_render, _} = render(rendered, {rendered.fingerprint, %{}}) + {full_render, fingerprints, _} = render(rendered, {rendered.fingerprint, %{}}) assert full_render == %{0 => "10:30", 1 => "Sunny"} - assert socket.fingerprints == {rendered.fingerprint, %{}} + assert fingerprints == {rendered.fingerprint, %{}} end test "renders nested %Rendered{}'s" do tree = {123, %{2 => {789, %{}}, 1 => {456, %{}}}} - {socket, diffed_render, _} = render(nested_rendered(), tree) + {diffed_render, fingerprints, _} = render(nested_rendered(), tree) assert diffed_render == %{0 => "hi", 1 => %{0 => "abc", 1 => "efg"}, 2 => %{0 => "efg"}} - assert socket.fingerprints == tree + assert fingerprints == tree end test "does not emit nested %Rendered{}'s if they did not change" do tree = {123, %{2 => {789, %{}}, 1 => {456, %{}}}} - {socket, diffed_render, _} = render(nested_rendered(false), tree) + {diffed_render, fingerprints, _} = render(nested_rendered(false), tree) assert diffed_render == %{0 => "hi"} - assert socket.fingerprints == tree + assert fingerprints == tree end test "detects change in nested fingerprint" do old_tree = {123, %{2 => {789, %{}}, 1 => {100_001, %{}}}} - {socket, diffed_render, _} = render(nested_rendered(), old_tree) + {diffed_render, fingerprints, _} = render(nested_rendered(), old_tree) assert diffed_render == %{ @@ -317,12 +316,12 @@ defmodule Phoenix.LiveView.DiffTest do 2 => %{0 => "efg"} } - assert socket.fingerprints == {123, %{2 => {789, %{}}, 1 => {456, %{}}}} + assert fingerprints == {123, %{2 => {789, %{}}, 1 => {456, %{}}}} end test "detects change in root fingerprint" do old_tree = {99999, %{}} - {socket, diffed_render, _} = render(nested_rendered(), old_tree) + {diffed_render, fingerprints, _} = render(nested_rendered(), old_tree) assert diffed_render == %{ 0 => "hi", @@ -335,7 +334,7 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["

", "

", "", ""] } - assert socket.fingerprints == {123, %{2 => {789, %{}}, 1 => {456, %{}}}} + assert fingerprints == {123, %{2 => {789, %{}}, 1 => {456, %{}}}} end end @@ -575,7 +574,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{ @@ -586,7 +585,7 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - assert socket.fingerprints != {rendered.fingerprint, %{}} + assert fingerprints != {rendered.fingerprint, %{}} assert components == Diff.new_components() end @@ -631,7 +630,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{ @@ -645,7 +644,9 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - {_socket, full_render, _components} = render(rendered, socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(rendered, fingerprints, components) + assert full_render == %{0 => %{0 => "DEFAULT", 2 => "DEFAULT"}} end @@ -660,7 +661,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{ @@ -674,7 +675,9 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - {_socket, full_render, _components} = render(rendered, socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(rendered, fingerprints, components) + assert full_render == %{0 => %{0 => "MY ID", 2 => "MY ID"}} end @@ -689,7 +692,7 @@ defmodule Phoenix.LiveView.DiffTest do test "@inner_block with args and parent assign" do assigns = %{socket: %Socket{}, value: 123, id: "DEFAULT"} - {socket, full_render, components} = render(function_tracking(assigns)) + {full_render, fingerprints, components} = render(function_tracking(assigns)) assert full_render == %{ 0 => %{ @@ -703,8 +706,8 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - {_socket, full_render, _components} = - render(function_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(function_tracking(assigns), fingerprints, components) assert full_render == %{ 0 => %{ @@ -717,15 +720,15 @@ defmodule Phoenix.LiveView.DiffTest do assigns = Map.put(assigns, :__changed__, %{}) - {_socket, full_render, _components} = - render(function_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(function_tracking(assigns), fingerprints, components) assert full_render == %{} assigns = Map.put(assigns, :__changed__, %{id: true}) - {_socket, full_render, _components} = - render(function_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(function_tracking(assigns), fingerprints, components) assert full_render == %{ 0 => %{ @@ -736,8 +739,8 @@ defmodule Phoenix.LiveView.DiffTest do assigns = Map.put(assigns, :__changed__, %{value: true}) - {_socket, full_render, _components} = - render(function_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(function_tracking(assigns), fingerprints, components) assert full_render == %{ 0 => %{ @@ -771,7 +774,7 @@ defmodule Phoenix.LiveView.DiffTest do test "slot tracking with multiple slots" do assigns = %{socket: %Socket{}, in_header: "H", in_footer: "F", in_both: "B"} - {socket, full_render, components} = render(slot_tracking(assigns)) + {full_render, fingerprints, components} = render(slot_tracking(assigns)) assert full_render == %{ 0 => %{ @@ -783,8 +786,8 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - {_socket, full_render, _components} = - render(slot_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(slot_tracking(assigns), fingerprints, components) assert full_render == %{ 0 => %{ @@ -795,29 +798,29 @@ defmodule Phoenix.LiveView.DiffTest do assigns = Map.put(assigns, :__changed__, %{}) - {_socket, full_render, _components} = - render(slot_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(slot_tracking(assigns), fingerprints, components) assert full_render == %{} assigns = Map.put(assigns, :__changed__, %{in_header: true}) - {_socket, full_render, _components} = - render(slot_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(slot_tracking(assigns), fingerprints, components) assert full_render == %{0 => %{0 => %{0 => "H"}}} assigns = Map.put(assigns, :__changed__, %{in_footer: true}) - {_socket, full_render, _components} = - render(slot_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(slot_tracking(assigns), fingerprints, components) assert full_render == %{0 => %{1 => %{0 => "F"}}} assigns = Map.put(assigns, :__changed__, %{in_both: true}) - {_socket, full_render, _components} = - render(slot_tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(slot_tracking(assigns), fingerprints, components) assert full_render == %{ 0 => %{ @@ -857,7 +860,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{0 => 1, :s => ["COMPONENT\n", ""]}, @@ -874,7 +877,9 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - {_socket, full_render, _components} = render(rendered, socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(rendered, fingerprints, components) + assert full_render == %{0 => %{0 => 1}} end end @@ -883,7 +888,7 @@ defmodule Phoenix.LiveView.DiffTest do test "on mount" do component = %Component{id: "hello", assigns: %{from: :component}, component: MyComponent} rendered = component_template(%{component: component}) - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => 1, @@ -899,7 +904,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert socket.fingerprints == {rendered.fingerprint, %{}} + assert fingerprints == {rendered.fingerprint, %{}} {cid_to_component, _, 2} = components assert {MyComponent, "hello", _, _, _} = cid_to_component[1] @@ -916,7 +921,7 @@ defmodule Phoenix.LiveView.DiffTest do test "on root fingerprint change" do component = %Component{id: "hello", assigns: %{from: :component}, component: MyComponent} rendered = component_template(%{component: component}) - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => 1, @@ -932,7 +937,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert socket.fingerprints == {rendered.fingerprint, %{}} + assert fingerprints == {rendered.fingerprint, %{}} assert_received {:mount, %Socket{endpoint: __MODULE__, assigns: assigns}} assert assigns[:flash] == %{} @@ -942,8 +947,8 @@ defmodule Phoenix.LiveView.DiffTest do another_rendered = another_component_template(%{component: component}) - {another_socket, another_full_render, _} = - render(another_rendered, socket.fingerprints, components) + {another_full_render, another_fingerprints, _} = + render(another_rendered, fingerprints, components) assert another_full_render == %{ 0 => 2, @@ -959,8 +964,8 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert another_socket.fingerprints == {another_rendered.fingerprint, %{}} - assert socket.fingerprints != another_socket.fingerprints + assert another_fingerprints == {another_rendered.fingerprint, %{}} + assert fingerprints != another_fingerprints assert_received {:mount, %Socket{endpoint: __MODULE__, assigns: assigns}} assert assigns[:flash] == %{} @@ -985,13 +990,13 @@ defmodule Phoenix.LiveView.DiffTest do test "on update without render" do component = %Component{id: "hello", assigns: %{from: :component}, component: MyComponent} rendered = component_template(%{component: component}) - {previous_socket, _, previous_components} = render(rendered) + {_, previous_fingerprints, previous_components} = render(rendered) - {socket, full_render, components} = - render(rendered, previous_socket.fingerprints, previous_components) + {full_render, fingerprints, components} = + render(rendered, previous_fingerprints, previous_components) assert full_render == %{0 => 1} - assert socket.fingerprints == previous_socket.fingerprints + assert fingerprints == previous_fingerprints assert components == previous_components assert_received {:mount, %Socket{endpoint: __MODULE__, assigns: assigns}} @@ -1007,16 +1012,16 @@ defmodule Phoenix.LiveView.DiffTest do test "on update with render" do component = %Component{id: "hello", assigns: %{from: :component}, component: MyComponent} rendered = component_template(%{component: component}) - {previous_socket, _, previous_components} = render(rendered) + {_, previous_fingerprints, previous_components} = render(rendered) component = %Component{id: "hello", assigns: %{from: :rerender}, component: MyComponent} rendered = component_template(%{component: component}) - {socket, full_render, components} = - render(rendered, previous_socket.fingerprints, previous_components) + {full_render, fingerprints, components} = + render(rendered, previous_fingerprints, previous_components) assert full_render == %{0 => 1, :c => %{1 => %{0 => "rerender"}}} - assert socket.fingerprints == previous_socket.fingerprints + assert fingerprints == previous_fingerprints assert components != previous_components assert_received {:mount, %Socket{endpoint: __MODULE__, assigns: assigns}} @@ -1038,7 +1043,7 @@ defmodule Phoenix.LiveView.DiffTest do test "on update with temporary" do component = %Component{id: "hello", assigns: %{from: :component}, component: TempComponent} rendered = component_template(%{component: component}) - {previous_socket, full_render, previous_components} = render(rendered) + {full_render, previous_fingerprints, previous_components} = render(rendered) assert full_render == %{ 0 => 1, @@ -1050,11 +1055,11 @@ defmodule Phoenix.LiveView.DiffTest do component = %Component{id: "hello", assigns: %{from: :rerender}, component: TempComponent} rendered = component_template(%{component: component}) - {socket, full_render, components} = - render(rendered, previous_socket.fingerprints, previous_components) + {full_render, fingerprints, components} = + render(rendered, previous_fingerprints, previous_components) assert full_render == %{0 => 1, :c => %{1 => %{0 => "rerender"}}} - assert socket.fingerprints == previous_socket.fingerprints + assert fingerprints == previous_fingerprints assert components != previous_components assert_received {:temporary_mount, %Socket{endpoint: __MODULE__}} @@ -1101,7 +1106,7 @@ defmodule Phoenix.LiveView.DiffTest do } rendered = component_template(%{component: tree}) - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert %{ c: %{ @@ -1116,7 +1121,7 @@ defmodule Phoenix.LiveView.DiffTest do } } = full_render - assert socket.fingerprints == {rendered.fingerprint, %{}} + assert fingerprints == {rendered.fingerprint, %{}} assert {_, _, 9} = components assert_received {:update_many, [{%{id: "R"}, socket0}]} @@ -1141,17 +1146,17 @@ defmodule Phoenix.LiveView.DiffTest do test "on addition" do component = %Component{id: "hello", assigns: %{from: :component}, component: MyComponent} rendered = component_template(%{component: component}) - {previous_socket, _, previous_components} = render(rendered) + {_, previous_fingerprints, previous_components} = render(rendered) component = %Component{id: "another", assigns: %{from: :another}, component: MyComponent} rendered = component_template(%{component: component}) - {socket, full_render, components} = - render(rendered, previous_socket.fingerprints, previous_components) + {full_render, fingerprints, components} = + render(rendered, previous_fingerprints, previous_components) assert full_render == %{0 => 2, :c => %{2 => %{0 => "another", 1 => "world", :s => -1}}} - assert socket.fingerprints == previous_socket.fingerprints + assert fingerprints == previous_fingerprints assert components != previous_components assert_received {:mount, %Socket{endpoint: __MODULE__}} @@ -1166,13 +1171,13 @@ defmodule Phoenix.LiveView.DiffTest do test "duplicate IDs" do component = %Component{id: "hello", assigns: %{from: :component}, component: TempComponent} rendered = component_template(%{component: component}) - {previous_socket, _, previous_components} = render(rendered) + {_, previous_fingerprints, previous_components} = render(rendered) component = %Component{id: "hello", assigns: %{from: :replaced}, component: MyComponent} rendered = component_template(%{component: component}) - {socket, full_render, components} = - render(rendered, previous_socket.fingerprints, previous_components) + {full_render, fingerprints, components} = + render(rendered, previous_fingerprints, previous_components) assert full_render == %{ 0 => 2, @@ -1186,7 +1191,7 @@ defmodule Phoenix.LiveView.DiffTest do } } - assert socket.fingerprints == previous_socket.fingerprints + assert fingerprints == previous_fingerprints assert components != previous_components assert_received {:temporary_mount, %Socket{endpoint: __MODULE__}} @@ -1214,7 +1219,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{d: [["0", 1], ["1", 2]], s: ["\n ", ": ", "\n "]}, @@ -1231,7 +1236,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert {^fingerprint, %{0 => _}} = socket.fingerprints + assert {^fingerprint, %{0 => _}} = fingerprints {cid_to_component, _, 3} = components assert {MyComponent, "index_1", _, _, _} = cid_to_component[1] @@ -1264,7 +1269,7 @@ defmodule Phoenix.LiveView.DiffTest do %Component{id: "index_2", assigns: %{from: :index_2}, component: IfComponent} ] - {socket, full_render, diff_components} = render(template.(components)) + {full_render, fingerprints, diff_components} = render(template.(components)) assert full_render == %{ 0 => %{d: [["0", 1], ["1", 2]], s: ["\n ", ": ", "\n "]}, @@ -1289,8 +1294,8 @@ defmodule Phoenix.LiveView.DiffTest do %Component{id: "index_3", assigns: %{from: :index_3}, component: IfComponent} ] - {socket, diff, diff_components} = - render(template.(components), socket.fingerprints, diff_components) + {diff, _, diff_components} = + render(template.(components), fingerprints, diff_components) assert diff == %{ 0 => %{d: [["0", 3]]}, @@ -1305,8 +1310,8 @@ defmodule Phoenix.LiveView.DiffTest do %Component{id: "index_4", assigns: %{from: :index_4, if: false}, component: IfComponent} ] - {socket, diff, diff_components} = - render(template.(components), socket.fingerprints, diff_components) + {diff, _, diff_components} = + render(template.(components), fingerprints, diff_components) assert diff == %{ 0 => %{d: [["0", 4]]}, @@ -1323,8 +1328,8 @@ defmodule Phoenix.LiveView.DiffTest do %Component{id: "index_5", assigns: %{from: :index_5}, component: IfComponent} ] - {_socket, diff, diff_components} = - render(template.(components), socket.fingerprints, diff_components) + {diff, _, diff_components} = + render(template.(components), fingerprints, diff_components) assert diff == %{ 0 => %{d: [["0", 1], ["1", 5]]}, @@ -1358,7 +1363,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{ @@ -1384,7 +1389,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert {^fingerprint, %{0 => _}} = socket.fingerprints + assert {^fingerprint, %{0 => _}} = fingerprints {cid_to_component, _, 5} = components assert {MyComponent, "foo-index_1", _, _, _} = cid_to_component[1] @@ -1408,7 +1413,7 @@ defmodule Phoenix.LiveView.DiffTest do """ end - {socket, full_render, diff_components} = render(template.(1, [])) + {full_render, fingerprints, diff_components} = render(template.(1, [])) assert full_render == %{ 0 => 1, @@ -1422,8 +1427,8 @@ defmodule Phoenix.LiveView.DiffTest do assert {RecurComponent, 1, _, _, _} = cid_to_component[1] # Now let's add one level of nesting - {socket, diff, diff_components} = - render(template.(1, [{2, []}]), socket.fingerprints, diff_components) + {diff, _, diff_components} = + render(template.(1, [{2, []}]), fingerprints, diff_components) assert diff == %{ 0 => 1, @@ -1437,8 +1442,8 @@ defmodule Phoenix.LiveView.DiffTest do assert {RecurComponent, 2, _, _, _} = cid_to_component[2] # Now let's add two levels of nesting - {_socket, diff, diff_components} = - render(template.(1, [{2, [{3, []}]}]), socket.fingerprints, diff_components) + {diff, _, diff_components} = + render(template.(1, [{2, [{3, []}]}]), fingerprints, diff_components) assert diff == %{ 0 => 1, @@ -1462,7 +1467,7 @@ defmodule Phoenix.LiveView.DiffTest do """ end - {socket, full_render, diff_components} = render(template.(1, [])) + {full_render, _fingerprints, diff_components} = render(template.(1, [])) assert full_render == %{ 0 => 1, @@ -1477,8 +1482,9 @@ defmodule Phoenix.LiveView.DiffTest do # Now let's add one level of nesting directly {diff, diff_components, :extra} = - Diff.write_component(socket, 1, diff_components, fn socket, _component -> - {Phoenix.Component.assign(socket, children: [{2, []}]), :extra} + Diff.write_component(%Socket{endpoint: __MODULE__}, 1, diff_components, fn + socket, _component -> + {Phoenix.Component.assign(socket, children: [{2, []}]), :extra} end) assert diff == %{ @@ -1509,7 +1515,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{ @@ -1530,7 +1536,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert {^fingerprint, %{0 => _}} = socket.fingerprints + assert {^fingerprint, %{0 => _}} = fingerprints {cid_to_component, _, 3} = components assert {MyComponent, "index_1", _, _, _} = cid_to_component[1] @@ -1563,7 +1569,7 @@ defmodule Phoenix.LiveView.DiffTest do """ - {socket, full_render, components} = render(rendered) + {full_render, fingerprints, components} = render(rendered) assert full_render == %{ 0 => %{ @@ -1583,7 +1589,7 @@ defmodule Phoenix.LiveView.DiffTest do :r => 1 } - assert {^fingerprint, %{0 => _}} = socket.fingerprints + assert {^fingerprint, %{0 => _}} = fingerprints {cid_to_component, _, 2} = components assert {MyComponent, "index_2", _, _, _} = cid_to_component[1] @@ -1604,7 +1610,7 @@ defmodule Phoenix.LiveView.DiffTest do <% end %> """ - {_socket, full_render, _components} = render(rendered) + {full_render, _fingerprints, _components} = render(rendered) assert %{ 0 => %{d: [[1], [2]], s: ["\n ", "\n"]}, @@ -1636,7 +1642,7 @@ defmodule Phoenix.LiveView.DiffTest do test "@inner_block tracking with args and parent assigns" do assigns = %{socket: %Socket{}, parent_value: 123} - {socket, full_render, components} = render(tracking(assigns)) + {full_render, fingerprints, components} = render(tracking(assigns)) assert full_render == %{ 0 => 1, @@ -1661,16 +1667,16 @@ defmodule Phoenix.LiveView.DiffTest do :s => ["", ""] } - {_socket, full_render, _components} = - render(tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(tracking(assigns), fingerprints, components) assert full_render == %{0 => 1} # Changing the root assign assigns = %{socket: %Socket{}, parent_value: 123, __changed__: %{parent_value: true}} - {_socket, full_render, _components} = - render(tracking(assigns), socket.fingerprints, components) + {full_render, _fingerprints, _components} = + render(tracking(assigns), fingerprints, components) assert full_render == %{ 0 => 1,