Skip to content
Source
Sign inSign up
TanStack/query

windowSize for useInfiniteQuery #4975

Send to my agent
ClosedTkDodo opened this issue 8 comments
TkDodocommented

as discussed on twitter, as a potentially different api to replace refetchPages.

@arnaudbzn

  1. TkDodo added labelsenhancement

  2. TkDodo added labelsv5

  3. Overview

    useInfiniteQuery supports a new windowSize option. windowSize defines the number of pages to store in the query data.

    The objective is to keep only a "window" of pages in memory and fetch pages outside this window when needed (bi-directional).

    Example: An infinite scroll that can generate +50 pages: Refetching to whole list will sequentially trigger 50 network calls. By setting windowSize to 5, only 5 pages are refetched and kept in memory. Moving outside the window (both directions are supported) will remove a page from the memory to keep the number of pages fixed and the new page is fetched as usual and added to the end or the start of the pages array according to the direction.

    Functional specs

    windowSize has no effect if its value is undefined or equals 0

    When the total number of pages is lower than windowSize the behavior is the same as without windowSize

    When number of pages is equal to windowSize a call to:

    • fetchNextPage will remove the first page and add a new page at the end of the pages array.
    • fetchPreviousPage will remove the last page and insert a new page at the start of the pages array.

    Requirements

    • the infinite list must be bi-directional: getPreviousPageParam must be defined

    Other DX impacts

    The developer has to keep in mind that the number data pages is limited to thewindowSize when using:

    • getQueryData
    • setQueryData (e.g: Optimistic UI)
    • persistQueryClient

    Implementation specs

    In infiniteQueryBehavior class: buildNewPages can be updated to use a new LimitedLengthArray util class (or a set of functions) to manage newPageParams and pages array by applying the window logic if needed.

  4. sounds lovely ❤️ . Maybe we should discuss the name windowSize, I'm not sure it reflects what we're doing. With windowing, I would associate a perf optimization for rendering where we would only return the amount of requested pages to the user, but still cache them all.

    But what we're actually doing is throwing away cached results, and I think we should make that clear. Something like maxCacheSize or maxPages / maxCachedPages maybe?

    Another thing: Should we also stick to that maxSize when setQueryData is called with more than that? I don't think so as we wouldn't know which pages to discard. That means that it's possible to get more pages into the cache via setQueryData, but I guess that's fine...

  5. Quick tests done with the suggested implementation look promising with only the following code changes to buildNewPages ('window' naming is to be improved...) I can create a PR for this WIP or I can share it somewhere else so you can experience the prototype. In the meatime a quick demo here

            const windowSize = 3
            const newPageParamsWindow = new LimitedLengthArray(windowSize, newPageParams)
    
            const buildNewPages = (
              pages: unknown[],
              param: unknown,
              page: unknown,
              previous?: boolean,
            ) => {
              const pagesWindow = new LimitedLengthArray(windowSize, pages)
              if(previous) {
                newPageParamsWindow.addItemToStart(param)
                pagesWindow.addItemToStart(page)
              } else {
                newPageParamsWindow.addItemToEnd(param)
                pagesWindow.addItemToEnd(page)
              }
              return pagesWindow.getItems()
    
              // OLD CODE
              // newPageParams = previous
              //   ? [param, ...newPageParams]
              //   : [...newPageParams, param]
              // return previous ? [page, ...pages] : [...pages, page]
            }
  6. But what we're actually doing is throwing away cached results, and I think we should make that clear. Something like maxCacheSize or maxPages / maxCachedPages maybe?

    maxPages sounds great

    Another thing: Should we also stick to that maxSize when setQueryData is called with more than that? I don't think so as we wouldn't know which pages to discard. That means that it's possible to get more pages into the cache via setQueryData, but I guess that's fine...

    Agreed, it should be fine.

  7. Prototype EDIT with refetch support and maxPages naming Demo of refetch here

          const buildNewPages = (
              pages: unknown[],
              param: unknown,
              page: unknown,
              previous?: boolean,
            ) => {
              const { maxPages } = context.options
              const limitedPages = new LimitedLengthArray(maxPages, [...pages])
              const limitedNewPageParams = new LimitedLengthArray(
                maxPages,
                newPageParams,
              )
    
              if (previous) {
                limitedNewPageParams.addItemToStart(param)
                limitedPages.addItemToStart(page)
              } else {
                limitedNewPageParams.addItemToEnd(param)
                limitedPages.addItemToEnd(page)
              }
    
              return limitedPages.getItems()
            }
  8. @TkDodo Preliminary testing of the prototype looks good. If you agree, I can prepare a PR including tests (I will do the docs later when the solution is validated and stabilized)

  9. Yes please go ahead ❤️

  10. Just to be sure: please use the v5 branch as base

  11. TkDodo closed this

Sign in to join this conversation.