fix(react-data-query): Retain queries in cache until GC#9502
fix(react-data-query): Retain queries in cache until GC#9502FrederikBolding wants to merge 4 commits into
Conversation
dc46c9c to
ada6f7a
Compare
|
|
||
| it('proxies requests to the underlying service', async () => { | ||
| const { clientA: client } = createClients(); | ||
| const { clientA: client, service } = createClients(); |
There was a problem hiding this comment.
Nit: Maybe we should change createClients to withClients and have it automatically call service.destroy? Seems like this would make writing tests easier in the future (but of course also adds to the size of this PR).
There was a problem hiding this comment.
Agreed that these tests will get hard to maintain long term, we can resolve this separately though
| await Promise.all([promiseA, promiseB]); | ||
|
|
||
| const hash = hashQueryKey(getAssetsQueryKey); | ||
| jest.advanceTimersByTime(inMilliseconds(1, Duration.Day)); |
There was a problem hiding this comment.
Where does this duration come from? Does it make sense for this duration to be set explicitly in the test somehow? Otherwise it makes this test a bit magical.
There was a problem hiding this comment.
Perhaps a comment would help? This is cacheTime for ExampleDataService:
| }); | ||
| }); | ||
|
|
||
| jest.advanceTimersByTime(0); |
There was a problem hiding this comment.
Why is this necessary? I've seen this in other tests but can't remember why. Is GC occurring at this step or no?
There was a problem hiding this comment.
All observer notifications from TanStack look something like this: setTimeout(() => notify(), 0);. So it is required in order for the subscription promise to resolve when using fake timers.
Explanation
Following this PR, instead of automatically removing queries from the UI query client when a
BaseDataServiceinstance evicts it from the cache, we retain the query in cache until theQueryClientitself cleans it up (viacacheTime).This prevents issues with observers in the UI having their cache wiped just because a refetch hasn't happened in a while. Since the UI query client considers the data stale, it may also choose to refetch and repopulate the query in the
BaseDataServiceinstance before eviction anyways.References
https://consensyssoftware.atlassian.net/browse/WPC-1168
Checklist
Note
Medium Risk
Changes cache lifecycle for all data-service-backed queries; behavior shifts from immediate UI eviction on service removal to TanStack
cacheTime, which could affect stale-data visibility but fixes unintended cache wipes for active observers.Overview
UI query client no longer drops cached data when
BaseDataServiceevicts a query. The granularcacheUpdatedlistener now ignoresremovedpayloads instead of callingcache.remove, so active observers keep their data until TanStack Query’s owncacheTimegarbage collection runs after subscriptions end.Tests replace the old “sync removal on remove event” case with scenarios using fake timers: data stays in cache while observers remain subscribed even after long simulated time, and entries clear only after observers are destroyed and
cacheTimeelapses. Changelog documents the fix under Unreleased.Reviewed by Cursor Bugbot for commit ada6f7a. Bugbot is set up for automated code reviews on this repo. Configure here.