Phoenix LiveView Stream API for inserting many

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
5 Likes