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

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews