diff --git a/.changeset/gc-time-last-wins.md b/.changeset/gc-time-last-wins.md new file mode 100644 index 00000000000..567391fd7f0 --- /dev/null +++ b/.changeset/gc-time-last-wins.md @@ -0,0 +1,6 @@ +--- +'@tanstack/query-core': major +'@tanstack/query-devtools': patch +--- + +A query or mutation now uses the `gcTime` from the options that were applied to it last. Before, it kept the longest `gcTime` it had seen, so a long value could not be shortened and `Infinity` stayed forever. Options without a `gcTime` now set it back to the default of five minutes, so use the same `gcTime` wherever a query is used, for example with shared `queryOptions`. The devtools no longer pass `gcTime: -1` when they trigger the loading state. diff --git a/packages/query-core/src/__tests__/mutationObserver.test.tsx b/packages/query-core/src/__tests__/mutationObserver.test.tsx index 86037973441..9135158cd47 100644 --- a/packages/query-core/src/__tests__/mutationObserver.test.tsx +++ b/packages/query-core/src/__tests__/mutationObserver.test.tsx @@ -1,6 +1,6 @@ import { createKey, sleep } from '@tanstack/query-test-utils' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { MutationObserver, QueryClient } from '..' +import { MutationObserver, QueryClient, noop } from '..' import type { MutationFunctionContext, MutationObserverResult } from '..' describe('mutationObserver', () => { @@ -68,6 +68,26 @@ describe('mutationObserver', () => { expect(queryClient.getMutationCache().findAll()).toHaveLength(0) }) + it('should use the gcTime of the options that were applied last to a pending mutation', async () => { + const mutationFn = () => sleep(10).then(() => 'data') + const observer = new MutationObserver(queryClient, { + mutationFn, + gcTime: 1000, + }) + const unsubscribe = observer.subscribe(noop) + + const result = observer.mutate() + observer.setOptions({ mutationFn, gcTime: 10 }) + await vi.advanceTimersByTimeAsync(10) + await result + unsubscribe() + + await vi.advanceTimersByTimeAsync(9) + expect(queryClient.getMutationCache().findAll()).toHaveLength(1) + await vi.advanceTimersByTimeAsync(1) + expect(queryClient.getMutationCache().findAll()).toHaveLength(0) + }) + it('should reattach the observer to the in-flight mutation when resubscribing', async () => { const mutation = new MutationObserver(queryClient, { mutationFn: (text: string) => sleep(20).then(() => text), diff --git a/packages/query-core/src/__tests__/query.test.tsx b/packages/query-core/src/__tests__/query.test.tsx index d94723fc651..dd351c8adf0 100644 --- a/packages/query-core/src/__tests__/query.test.tsx +++ b/packages/query-core/src/__tests__/query.test.tsx @@ -90,31 +90,55 @@ describe('query', () => { } }) - it('should use the longest garbage collection time it has seen', async () => { + it('should use the garbage collection time of the options that were applied last', async () => { const key = createKey() - await queryClient - .query({ - queryKey: key, - queryFn: () => 'data', - gcTime: 100, - }) - .catch(noop) - await queryClient - .query({ - queryKey: key, - queryFn: () => 'data', - gcTime: 200, - }) - .catch(noop) - await queryClient - .query({ - queryKey: key, - queryFn: () => 'data', - gcTime: 10, - }) - .catch(noop) - const query = queryCache.find({ queryKey: key })! - expect(query.gcTime).toBe(200) + + await queryClient.query({ + queryKey: key, + queryFn: () => 'data', + gcTime: 100, + }) + await queryClient.query({ + queryKey: key, + queryFn: () => 'data', + gcTime: 200, + }) + await queryClient.query({ + queryKey: key, + queryFn: () => 'data', + gcTime: 10, + }) + + await vi.advanceTimersByTimeAsync(9) + expect(queryCache.find({ queryKey: key })).toBeDefined() + await vi.advanceTimersByTimeAsync(1) + expect(queryCache.find({ queryKey: key })).toBeUndefined() + }) + + it('should not keep an infinite garbage collection time when later options set a finite one', async () => { + const key = createKey() + const firstObserver = new QueryObserver(queryClient, { + queryKey: key, + queryFn: () => 'data', + gcTime: Infinity, + }) + const unsubscribeFirst = firstObserver.subscribe(noop) + await vi.advanceTimersByTimeAsync(0) + unsubscribeFirst() + + const secondObserver = new QueryObserver(queryClient, { + queryKey: key, + queryFn: () => 'data', + gcTime: 10, + }) + const unsubscribeSecond = secondObserver.subscribe(noop) + await vi.advanceTimersByTimeAsync(0) + unsubscribeSecond() + + await vi.advanceTimersByTimeAsync(9) + expect(queryCache.find({ queryKey: key })).toBeDefined() + await vi.advanceTimersByTimeAsync(1) + expect(queryCache.find({ queryKey: key })).toBeUndefined() }) it('should continue retry after focus regain and resolve all promises', async () => { diff --git a/packages/query-core/src/__tests__/removable.test.tsx b/packages/query-core/src/__tests__/removable.test.tsx index 5129f93aa14..80c41306cd7 100644 --- a/packages/query-core/src/__tests__/removable.test.tsx +++ b/packages/query-core/src/__tests__/removable.test.tsx @@ -75,13 +75,22 @@ describe('Removable', () => { expect(removable.gcTime).toBe(1000) }) - it('should never decrease an already larger gcTime', () => { + it('should replace a larger gcTime with a smaller one', () => { const removable = new RemovableTest() removable.callUpdateGcTime(10 * 60 * 1000) removable.callUpdateGcTime(1000) - expect(removable.gcTime).toBe(10 * 60 * 1000) + expect(removable.gcTime).toBe(1000) + }) + + it('should go back to the default when a later call provides no gcTime', () => { + const removable = new RemovableTest() + + removable.callUpdateGcTime(10 * 60 * 1000) + removable.callUpdateGcTime(undefined) + + expect(removable.gcTime).toBe(5 * 60 * 1000) }) }) diff --git a/packages/query-core/src/removable.ts b/packages/query-core/src/removable.ts index 679a670e006..824041aae41 100644 --- a/packages/query-core/src/removable.ts +++ b/packages/query-core/src/removable.ts @@ -34,11 +34,11 @@ export abstract class Removable { } /** - * Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes. + * Sets `gcTime` from the latest options. Without a value, it defaults to 5 minutes. * @param newGcTime - The `gcTime` from the latest options, if any. */ protected updateGcTime(newGcTime: number | undefined): void { - this.gcTime = Math.max(this.gcTime || 0, newGcTime ?? 5 * 60 * 1000) + this.gcTime = newGcTime ?? 5 * 60 * 1000 } /** diff --git a/packages/query-core/src/types.ts b/packages/query-core/src/types.ts index 5feaa73ec28..f11c6e4fc01 100644 --- a/packages/query-core/src/types.ts +++ b/packages/query-core/src/types.ts @@ -397,7 +397,7 @@ export interface QueryOptions< /** * The time in milliseconds that unused/inactive cache data remains in memory. * When a query's cache becomes unused or inactive, that cache data will be garbage collected after this duration. - * When different garbage collection times are specified, the longest one will be used. + * When different garbage collection times are specified for the same query, the one that was applied last is used. * Setting it to `Infinity` will disable garbage collection. * * Defaults to `5 * 60 * 1000` (5 minutes). diff --git a/packages/query-devtools/src/Devtools.tsx b/packages/query-devtools/src/Devtools.tsx index a55dd7c6339..bcac1cb2972 100644 --- a/packages/query-devtools/src/Devtools.tsx +++ b/packages/query-devtools/src/Devtools.tsx @@ -1860,7 +1860,6 @@ const QueryDetails = (props: { initialSearch?: string }) => { // Never resolve }) }, - gcTime: -1, }) activeQueryVal.setState({ data: undefined,