After playing around with stream/4 and reading up on all the new LV features, I was tasked at work with making an infinite scroll page. I thought it was the perfect candidate to try this new functionality out.
It was really easy to implement following the docs, the only thing i feel is missing is a way to insert many items to the stream. (think when you reach the bottom of the viewport and you want to load another 20 items).
We only have stream_insert/4 which works with 1 item at a time.
I added this to the project but wanted to know if anyone has been thinking about this as I feel we should have something like this directly from the Stream API.
@type socket :: Phoenix.LiveView.Socket.t()
@doc """
Inserts items or updates existing items in the stream.
See `Phoenix.LiveView.stream_insert/4` for opts.
"""
@spec stream_insert_many(socket(), atom(), list(), keyword()) :: socket()
def stream_insert_many(socket, name, items, opts \\ []) do
Enum.reduce(items, socket, fn item, acc ->
Phoenix.LiveView.stream_insert(acc, name, item, opts)
end)
end
Has anyone else needed to do something like this?
Id gladly work on a PR if this is of interest to the community
Very clever to āhijackā like that. reminds me of monkey patching stuffs in Rails, but here itās way more under control. I like it!
I also think that if we can handle many inserts it should be with the exact same function stream_insert and not bother with extra function, after all weāre in Elixir realm where pattern matching is a thing, isnāt it?
Any chance stream_reset is in the works? Iāve been using streams the last couple of days and the two big features that appear to be missing are bulk inserts (lower priority since thereās easy work arounds) and stream resets.
In my case I have a list of posts with filters. The issue is that updating a filter requires clearing out any existing results and populating with the new ones, but that doesnāt appear to be possible right now. I canāt stream_delete since I donāt have the old results, and I canāt start a new stream since that throws an error.
stream_reset(socket, :posts) would work great, or even having stream empty out existing streams first, allowing you to overwrite the stream with a new one.