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
Most Liked
chrismccord
zolrath
Both resetting and bulk inserts are on the main branch on github!
https://github.com/phoenixframework/phoenix_live_view/pull/2577
A new version with the features hasn’t been released yet but you can point your dependency to the main branch to try them out!
{:phoenix_live_view, github: "phoenixframework/phoenix_live_view", branch: "main"}
MitchellJeppson
For posterity, those who came here looking for a stream_insert_all but can’t find it, you just use stream/4:
By default, calling stream/4 on an existing stream will bulk insert the new items on the client while leaving the existing items in place
Popular in Discussions
Other popular topics
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









