Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/gc-time-last-wins.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 21 additions & 1 deletion packages/query-core/src/__tests__/mutationObserver.test.tsx
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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),
Expand Down
72 changes: 48 additions & 24 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/query-core/src/__tests__/removable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/removable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 0 additions & 1 deletion packages/query-devtools/src/Devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,6 @@ const QueryDetails = (props: { initialSearch?: string }) => {
// Never resolve
})
},
gcTime: -1,
})
activeQueryVal.setState({
data: undefined,
Expand Down
Loading