tomkonidas

tomkonidas

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

Showing Posts 1 to 10

woylie

woylie

That would make sense to me. The function could be called stream_insert_all as well.

I tried to implement regular pagination using streams, where all items are replaced when you change the page. The solution in short was:

  • use dom ID based on index, not based on the item
  • reduce over the new items the same way as you do in your stream_insert_many function
  • to clear out extra items if the new page has less items than the previous one, reduce over the unused indexes to delete those extra items

In that scenario, it would have been useful to have both stream_insert_all and stream_delete_all_by_dom_id

Even more useful would be a stream_reset to delete all items. With that, I wouldn’t have to use index-based dom IDs.

Instead of adding new *_all functions, the existing ones could also be updated to accept either a single item or a list of items.

aiwaiwa

aiwaiwa

Thank you for mentioning that. Trying to implement sorting and filtering and can’t figure it out

chrismccord

chrismccord

Creator of Phoenix

emptying a stream is on my todo list, but it didn’t make the cut for the last release :slight_smile:

27
Post #3
aiwaiwa

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

tomkonidas OP

Is there anything on the roadmap for inserting many items at a time?

barkerja

barkerja

cjbottaro

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…

  def live_view do
    quote do
      use Phoenix.LiveView,
        layout: {Web.Scholarship.Layouts, :app}

      import Phoenix.LiveView, except: [
        stream_insert: 3,
        stream_insert: 4
      ]

      def stream_insert(socket, name, item_or_items, opts \\ [])

      def stream_insert(socket, name, items, opts) when is_list(items) do
        Enum.reduce(items, socket, fn item, socket ->
          stream_insert(socket, name, item, opts)
        end)
      end

      def stream_insert(socket, name, item, opts) do
        Phoenix.LiveView.stream_insert(socket, name, item, opts)
      end

      unquote(html_helpers())
    end
  end
akaibukai

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_insert and not bother with extra function, after all we’re in Elixir realm where pattern matching is a thing, isn’t it?

akaibukai

akaibukai

Yes. Please, only suggest that option :smiling_face:

bkilshaw

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_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.

Edit: Nevermind, looks like the work is done :slight_smile:

Where Next? Top

Trending in Discussions Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews