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 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
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

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
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