Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions lib/phoenix_live_view/diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ defmodule Phoenix.LiveView.Diff do
"""
def to_iodata(map, component_mapper \\ fn _cid, content -> content end) do
to_iodata(map, Map.get(map, @components, %{}), Map.get(map, @template), component_mapper)
|> elem(0)
end

defp to_iodata(
Expand All @@ -60,7 +59,7 @@ defmodule Phoenix.LiveView.Diff do
template = template || kc[@template]

if !keyed or keyed[@keyed_count] == 0 do
{[], components}
[]
else
keyed_to_iodata(keyed[@keyed_count] - 1, keyed, static, components, template, mapper, [])
end
Expand All @@ -71,14 +70,13 @@ defmodule Phoenix.LiveView.Diff do
end

defp to_iodata(cid, components, _template, mapper) when is_integer(cid) do
# Resolve component pointers and update the component entries
components = resolve_components_xrefs(cid, components)
{iodata, components} = to_iodata(Map.fetch!(components, cid), components, nil, mapper)
{mapper.(cid, iodata), components}
diff = resolve_components_xrefs(cid, components)
iodata = to_iodata(diff, components, nil, mapper)
mapper.(cid, iodata)
end

defp to_iodata(binary, components, _template, _mapper) when is_binary(binary) do
{binary, components}
defp to_iodata(binary, _components, _template, _mapper) when is_binary(binary) do
binary
end

defp to_iodata_parts(parts, static, components, template, mapper) do
Expand All @@ -89,22 +87,22 @@ defmodule Phoenix.LiveView.Diff do
defp keyed_to_iodata(index, keyed, static, components, template, mapper, acc)
when index >= 0 do
diff = Map.fetch!(keyed, index)
{iodata, components} = to_iodata_parts(diff, static, components, template, mapper)
iodata = to_iodata_parts(diff, static, components, template, mapper)
keyed_to_iodata(index - 1, keyed, static, components, template, mapper, [iodata | acc])
end

defp keyed_to_iodata(_index, _keyed, _static, components, _template, _mapper, acc) do
{acc, components}
defp keyed_to_iodata(_index, _keyed, _static, _components, _template, _mapper, acc) do
acc
end

defp one_to_iodata([], _parts, _counter, acc, components, _template, _mapper) do
{acc, components}
defp one_to_iodata([], _parts, _counter, acc, _components, _template, _mapper) do
acc
end

defp one_to_iodata([head | tail], parts, counter, acc, components, template, mapper) do
{iodata, components} =
iodata =
case Map.fetch!(parts, counter) do
binary when is_binary(binary) -> {binary, components}
binary when is_binary(binary) -> binary
other -> to_iodata(other, components, template, mapper)
end

Expand All @@ -117,12 +115,10 @@ defmodule Phoenix.LiveView.Diff do
defp resolve_components_xrefs(cid, components) do
case components[cid] do
%{@static => static} = diff when is_integer(static) ->
static = abs(static)
components = resolve_components_xrefs(static, components)
Map.put(components, cid, deep_merge(components[static], Map.delete(diff, @static)))
deep_merge(resolve_components_xrefs(abs(static), components), Map.delete(diff, @static))

%{} ->
components
diff ->
diff
end
end

Expand Down
Loading