windowSize for useInfiniteQuery #4975
Send to my agentas discussed on twitter, as a potentially different api to replace refetchPages.
@arnaudbzn
TkDodo added labelsenhancement
TkDodo added labelsv5
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
maxCacheSizeormaxPages/maxCachedPagesmaybe?Another thing: Should we also stick to that maxSize when
setQueryDatais 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 viasetQueryData, but I guess that's fine...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 hereconst 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] }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?
maxPagessounds greatAnother 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.
Prototype EDIT with refetch support and
maxPagesnaming Demo of refetch hereconst 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() }@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)
Yes please go ahead ❤️
Just to be sure: please use the v5 branch as base
TkDodo closed this
Sign in to join this conversation.
Overview#
useInfiniteQuerysupports a newwindowSizeoption.windowSizedefines 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
windowSizeto 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#
windowSizehas no effect if its value isundefinedor equals0When the total number of pages is lower than
windowSizethe behavior is the same as withoutwindowSizeWhen number of pages is equal to
windowSizea call to:fetchNextPagewill remove the first page and add a new page at the end of the pages array.fetchPreviousPagewill remove the last page and insert a new page at the start of the pages array.Requirements#
getPreviousPageParammust be definedOther DX impacts#
The developer has to keep in mind that the number data pages is limited to the
windowSizewhen using:getQueryDatasetQueryData(e.g: Optimistic UI)persistQueryClientImplementation specs#
In
infiniteQueryBehaviorclass:buildNewPagescan be updated to use a newLimitedLengthArrayutil class (or a set of functions) to managenewPageParamsand pages array by applying the window logic if needed.