Hi,
i am using a Stream to show some items. I append items via PubSub and want to limit the amount of items shown in the UI. As i understand the docs i can set the limit
option to an integer and the stream takes care of adding/removing the items, so that the UI shows only the amount/limit i specified.
But while testing this, the limit gets only applied on the first mount but not when I use stream_insert/4
. Then i can add as many items as i want, and the amount of items in the UI grows indefinitely. Is this the correct/intended behaviour?
Minimal example:
def mount(params, _session, socket) do
items = MyApp.get_items()
if connected?(socket) do
MyAppWeb.Endpoint.subscribe("items")
end
{:ok, socket |> stream(:results, items, at: 0, limit: 10)}
end
def handle_info(%{event: "new-item", payload: %Item{} = item}, socket) do
{:noreply, socket |> stream_insert(:items, item, at: 0)}
end
def render(assigns) do
~H"""
<div phx-update="stream" id="items">
<img
:for={{id, item} <- @streams.items}
id={id}
src={item.src}
</img>
</div>
"""
end
Edit: Using LiveView 0.20.14