tomkonidas
Phoenix LiveView Stream API for inserting many
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
Trending in Discussions
Other Trending Topics
Latest Phoenix Threads
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 12 Posts
woylie
That would make sense to me. The function could be called
stream_insert_allas well.I tried to implement regular pagination using streams, where all items are replaced when you change the page. The solution in short was:
stream_insert_manyfunctionIn that scenario, it would have been useful to have both
stream_insert_allandstream_delete_all_by_dom_idEven more useful would be a
stream_resetto delete all items. With that, I wouldn’t have to use index-based dom IDs.Instead of adding new
*_allfunctions, the existing ones could also be updated to accept either a single item or a list of items.aiwaiwa
Thank you for mentioning that. Trying to implement sorting and filtering and can’t figure it out
chrismccord
emptying a stream is on my todo list, but it didn’t make the cut for the last release
aiwaiwa
Thank you so for much for doing that!
I’ll make sure to point folks to your reply as this Q continues to popup.
tomkonidas
Is there anything on the roadmap for inserting many items at a time?
barkerja
cjbottaro
I’m also here looking for a way to clear a stream. I figured just recreating it would do that, but got that “hook already defined” error.
I also want this feature, but here’s what I’m doing in the meantime…
akaibukai
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_insertand not bother with extra function, after all we’re in Elixir realm where pattern matching is a thing, isn’t it?akaibukai
Yes. Please, only suggest that option
bkilshaw
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_deletesince 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 havingstreamempty out existing streams first, allowing you to overwrite the stream with a new one.Edit: Nevermind, looks like the work is done