Packages
phoenix_live_view
1.2.2
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.3
1.2.0-rc.2
1.2.0-rc.1
1.2.0-rc.0
1.1.32
1.1.31
1.1.30
1.1.29
1.1.28
1.1.27
1.1.26
1.1.25
1.1.24
1.1.23
1.1.22
1.1.21
1.1.20
1.1.19
1.1.18
1.1.17
1.1.16
1.1.15
1.1.14
1.1.13
1.1.12
1.1.11
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
retired
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.1.0-rc.4
1.1.0-rc.3
1.1.0-rc.2
1.1.0-rc.1
1.1.0-rc.0
1.0.18
1.0.17
1.0.16
1.0.15
1.0.14
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
retired
1.0.7
1.0.6
retired
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.9
1.0.0-rc.8
1.0.0-rc.7
1.0.0-rc.6
1.0.0-rc.5
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.20.17
0.20.16
0.20.15
0.20.14
0.20.13
0.20.12
0.20.11
0.20.10
0.20.9
0.20.8
0.20.7
0.20.6
0.20.5
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.18
0.18.17
0.18.16
0.18.15
0.18.14
0.18.13
0.18.12
0.18.11
0.18.10
0.18.9
0.18.8
0.18.7
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.14
0.17.13
0.17.12
0.17.11
0.17.10
0.17.9
0.17.8
0.17.7
0.17.6
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.8
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.6.0-dev
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
Rich, real-time user experiences with server-rendered HTML
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/phoenix_live_view/live_stream.ex
defmodule Phoenix.LiveView.LiveStream do
@moduledoc false
defstruct name: nil,
dom_id: nil,
ref: nil,
inserts: [],
deletes: [],
reset?: false,
consumable?: false
alias Phoenix.LiveView.LiveStream
def new(name, ref, items, opts) when is_list(opts) do
dom_prefix = to_string(name)
dom_id = Keyword.get_lazy(opts, :dom_id, fn -> &default_id(dom_prefix, &1) end)
if not is_function(dom_id, 1) do
raise ArgumentError,
"stream :dom_id must return a function which accepts each item, got: #{inspect(dom_id)}"
end
# We need to go through the items one time to map them into the proper insert tuple format.
# Conveniently, we reverse the list in this pass, which we need to in order to be consistent
# with manually calling stream_insert multiple times, as stream_insert prepends.
items_list =
for item <- items, reduce: [] do
items -> [{dom_id.(item), -1, item, opts[:limit], opts[:update_only]} | items]
end
%LiveStream{
ref: ref,
name: name,
dom_id: dom_id,
inserts: items_list,
deletes: [],
reset?: false
}
end
defp default_id(dom_prefix, %{id: id} = _struct_or_map), do: dom_prefix <> "-#{to_string(id)}"
defp default_id(dom_prefix, other) do
raise ArgumentError, """
expected stream :#{dom_prefix} to be a struct or map with :id key, got: #{inspect(other)}
If you would like to generate custom DOM IDs based on other keys, use `Phoenix.LiveView.stream_configure/3` with the :dom_id option beforehand.
"""
end
def reset(%LiveStream{} = stream) do
%{stream | reset?: true}
end
def prune(%LiveStream{} = stream) do
%{stream | inserts: [], deletes: [], reset?: false}
end
def delete_item(%LiveStream{} = stream, item) do
delete_item_by_dom_id(stream, stream.dom_id.(item))
end
def delete_item_by_dom_id(%LiveStream{} = stream, dom_id) do
%{stream | deletes: [dom_id | stream.deletes]}
end
def insert_item(%LiveStream{} = stream, item, at, limit, update_only) do
item_id = stream.dom_id.(item)
%{stream | inserts: [{item_id, at, item, limit, update_only} | stream.inserts]}
end
def mark_consumable(%Phoenix.LiveView.LiveStream{} = stream) do
%{stream | consumable?: true}
end
def mark_consumable(collection), do: collection
def annotate_comprehension(comprehension, %Phoenix.LiveView.LiveStream{} = stream) do
inserts =
for {id, at, _item, limit, update_only} <- stream.inserts, do: [id, at, limit, update_only]
data = [stream.ref, inserts, stream.deletes]
if stream.reset? do
Map.put(comprehension, :stream, data ++ [true])
else
Map.put(comprehension, :stream, data)
end
end
def annotate_comprehension(comprehension, _collection), do: comprehension
defimpl Enumerable, for: LiveStream do
def count(%LiveStream{inserts: inserts}), do: {:ok, length(inserts)}
def member?(%LiveStream{}, _item), do: raise(RuntimeError, "not implemented")
def reduce(%LiveStream{} = stream, acc, fun) do
if stream.consumable? do
# the inserts are stored in reverse insert order, so we need to reverse them
# before rendering; we also remove duplicates to only use the most recent
# inserts, which, as the items are reversed, are first
inserts = uniq_inserts(stream.inserts, [], %{})
do_reduce(inserts, acc, fun)
else
raise ArgumentError, """
streams can only be consumed directly by a for comprehension.
If you are attempting to consume the stream ahead of time, such as with
`Enum.with_index(@streams.#{stream.name})`, you need to place the relevant information
within the stream items instead.
"""
end
end
defp do_reduce(_list, {:halt, acc}, _fun), do: {:halted, acc}
defp do_reduce(list, {:suspend, acc}, fun), do: {:suspended, acc, &do_reduce(list, &1, fun)}
defp do_reduce([], {:cont, acc}, _fun), do: {:done, acc}
defp do_reduce([{dom_id, _at, item, _limit, _update_only} | tail], {:cont, acc}, fun) do
do_reduce(tail, fun.({dom_id, item}, acc), fun)
end
defp uniq_inserts([{id, _, _, _, _} | rest], inserts, ids) when is_map_key(ids, id),
do: uniq_inserts(rest, inserts, ids)
defp uniq_inserts([{id, _, _, _, _} = insert | rest], inserts, ids),
do: uniq_inserts(rest, [insert | inserts], Map.put(ids, id, true))
defp uniq_inserts([], inserts, _ids), do: inserts
# Returns a function that slices the data structure contiguously.
def slice(%LiveStream{}), do: raise(RuntimeError, "not implemented")
end
end