solid-primitives icon indicating copy to clipboard operation
solid-primitives copied to clipboard

No error handling available for createInfiniteScroll

Open 007qr opened this issue 7 months ago • 4 comments

Describe the bug

In the JSX Docs it clearly says that we get any errors using pages.error but the current implementation has not property that says error and loading... How am I suppose to get errors back

/**
 * Provides an easy way to implement infinite scrolling.
 *
 * ```ts
 * const [pages, loader, { page, setPage, setPages, end, setEnd }] = createInfiniteScroll(fetcher);
 * ```
 * @param fetcher `(page: number) => Promise<T[]>`
 * @return `pages()` is an accessor contains array of contents
 * @property `pages.loading` is a boolean indicator for the loading state
 * @property `pages.error` contains any error encountered
 * @return `infiniteScrollLoader` is a directive used to set the loader element
 * @method `page` is an accessor that contains page number
 * @method `setPage` allows to manually change the page number
 * @method `setPages` allows to manually change the contents of the page
 * @method `end` is a boolean indicator for end of the page
 * @method `setEnd` allows to manually change the end
 */
export function createInfiniteScroll<T>(fetcher: (page: number) => Promise<T[]>): [
  pages: Accessor<T[]>,
  loader: (el: Element) => void,
  options: {
    page: Accessor<number>;
    setPage: Setter<number>;
    setPages: Setter<T[]>;
    end: Accessor<boolean>;
    setEnd: Setter<boolean>;
  },
] {
  const [pages, setPages] = createSignal<T[]>([]);
  const [page, setPage] = createSignal(0);
  const [end, setEnd] = createSignal(false);

  let add: (el: Element) => void = noop;
  if (!isServer) {
    const io = new IntersectionObserver(e => {
      if (e.length > 0 && e[0]!.isIntersecting && !end() && !contents.loading) {
        setPage(p => p + 1);
      }
    });
    onCleanup(() => io.disconnect());
    add = (el: Element) => {
      io.observe(el);
      tryOnCleanup(() => io.unobserve(el));
    };
  }```

  const [contents] = createResource(page, fetcher);

  createComputed(() => {
    const content = contents.latest;
    if (!content) return;
    batch(() => {
      if (content.length === 0) setEnd(true);
      setPages(p => [...p, ...content]);
    });
  });

  return [
    pages,
    add,
    {
      page: page,
      setPage: setPage,
      setPages: setPages,
      end: end,
      setEnd: setEnd,
    },
  ];
}

### Minimal Reproduction Link

https://example.com/

007qr avatar Jun 04 '25 06:06 007qr

You're right. In general createInfiniteScroll isn't well designed. I would advise to copy the source code and modify to your needs. As you can see it's not that complicated.

thetarnav avatar Jun 04 '25 06:06 thetarnav

We should fix this nevertheless.

atk avatar Jun 04 '25 07:06 atk

Sure, should the contents resource be returned in the options object? This way there is access to the last error, and even a way to trigger suspense.

thetarnav avatar Jun 04 '25 07:06 thetarnav

EIther that or we could add getters to pages.

atk avatar Jun 04 '25 12:06 atk