Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/gc-removal-time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/query-core": major
---

Garbage collect a query or mutation at the latest removal time any of its users has scheduled, instead of after the largest `gcTime` it has seen. Remove the `gcTime` property from `Query` and `Mutation`; read `options.gcTime` instead.
21 changes: 2 additions & 19 deletions packages/query-core/src/__tests__/mutation.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ describe('mutation', () => {
| 'removeObserver'
| 'continue'
| 'execute'
| 'gcTime'
| 'destroy'
>()
})
Expand Down Expand Up @@ -339,7 +338,7 @@ describe('mutation', () => {
})

describe('removeObserver', () => {
it('should take a single observer and return nothing', () => {
it('should take an observer and a gcTime and return nothing', () => {
const mutation = Mutation.build<
{ value: string },
CustomError,
Expand All @@ -352,7 +351,7 @@ describe('mutation', () => {

expectTypeOf(mutation.removeObserver).returns.toEqualTypeOf<void>()
expectTypeOf(mutation.removeObserver).parameters.toEqualTypeOf<
[MutationObserver<any, any, any, any>]
[MutationObserver<any, any, any, any>, number | undefined]
>()
})
})
Expand Down Expand Up @@ -410,22 +409,6 @@ describe('mutation', () => {
})
})

describe('gcTime', () => {
it('should be a number inherited from Removable', () => {
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
string
>(new QueryClient(), {
mutationFn: (variables: number) =>
Promise.resolve({ value: String(variables) }),
})

expectTypeOf(mutation.gcTime).toEqualTypeOf<number>()
})
})

describe('destroy', () => {
it('should take no arguments and return nothing', () => {
const mutation = Mutation.build<
Expand Down
47 changes: 47 additions & 0 deletions packages/query-core/src/__tests__/mutationObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ describe('mutationObserver', () => {
expect(queryClient.getMutationCache().findAll()).toHaveLength(0)
})

it('should record the gcTime it used for a mutation when its mutation key changes', async () => {
const observer = new MutationObserver(queryClient, {
mutationKey: createKey(),
mutationFn: (text: string) => sleep(5).then(() => text),
gcTime: 1000,
})
const unsubscribe = observer.subscribe(vi.fn())

observer.mutate('input')
await vi.advanceTimersByTimeAsync(5)
expect(queryClient.getMutationCache().findAll()).toHaveLength(1)

observer.setOptions({
mutationKey: createKey(),
mutationFn: (text: string) => sleep(5).then(() => text),
gcTime: Infinity,
})

await vi.advanceTimersByTimeAsync(999)
expect(queryClient.getMutationCache().findAll()).toHaveLength(1)

// Removed after the old key's gcTime, not kept forever with the new key's
await vi.advanceTimersByTimeAsync(1)
expect(queryClient.getMutationCache().findAll()).toHaveLength(0)

unsubscribe()
})

it('should keep a pending mutation until it settles and its gcTime has passed', async () => {
const observer = new MutationObserver(queryClient, {
mutationFn: (text: string) => sleep(45).then(() => text),
gcTime: 10,
})
const unsubscribe = observer.subscribe(vi.fn())

observer.mutate('input')
unsubscribe()

// Still pending when its gc timer fires at 10, 20, 30 and 40ms, so it is
// rescheduled each time; it settles at 45ms
await vi.advanceTimersByTimeAsync(49)
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
8 changes: 5 additions & 3 deletions packages/query-core/src/__tests__/query.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ describe('query', () => {
})

describe('removeObserver', () => {
it('should take a single observer and return void', () => {
it('should take an observer and a gcTime and return void', () => {
const query = queryClient
.getQueryCache()
.build<{ value: string }, CustomError>(queryClient, {
Expand All @@ -821,7 +821,10 @@ describe('query', () => {
})

expectTypeOf(query.removeObserver).parameters.toEqualTypeOf<
[observer: QueryObserver<any, any, any, any, any>]
[
observer: QueryObserver<any, any, any, any, any>,
gcTime: number | undefined,
]
>()
expectTypeOf(query.removeObserver).returns.toEqualTypeOf<void>()
})
Expand Down Expand Up @@ -952,7 +955,6 @@ describe('query', () => {
| 'promise'
| 'queryType'
| 'resetState'
| 'gcTime'
| 'setOptions'
| 'setData'
| 'setState'
Expand Down
154 changes: 126 additions & 28 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,124 @@ describe('query', () => {
}
})

it('should use the longest garbage collection time it has seen', async () => {
it('should be garbage collected at the latest removal time scheduled by its observers', 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 queryFn = () => 'data'
const observerA = new QueryObserver(queryClient, {
queryKey: key,
queryFn,
gcTime: 5 * 60 * 1000,
})
const observerB = new QueryObserver(queryClient, {
queryKey: key,
queryFn,
gcTime: 60 * 1000,
})
const unsubscribeA = observerA.subscribe(noop)
const unsubscribeB = observerB.subscribe(noop)
await vi.advanceTimersByTimeAsync(0)
const query = queryCache.find({ queryKey: key })!

// 0:30 A becomes inactive: removal at 5:30. Waiting first makes sure this is
// A's own removal time, not the 5:00 recorded when the query was created.
await vi.advanceTimersByTimeAsync(30 * 1000)
unsubscribeA()

// 1:30 B becomes inactive: 2:30 is earlier, so removal stays at 5:30
await vi.advanceTimersByTimeAsync(60 * 1000)
unsubscribeB()

// Not removed at 2:30, as it would be if the last gcTime won
await vi.advanceTimersByTimeAsync(60 * 1000)
expect(queryCache.find({ queryKey: key })).toBe(query)

// Not removed before 5:30
await vi.advanceTimersByTimeAsync(3 * 60 * 1000 - 1)
expect(queryCache.find({ queryKey: key })).toBe(query)

// Removed at 5:30, not at 6:30 as it would be if the largest gcTime won
await vi.advanceTimersByTimeAsync(1)
expect(queryCache.find({ queryKey: key })).toBeUndefined()
})

it('should record the gcTime an observer used for a query when it switches to another key', async () => {
const key1 = createKey()
const key2 = createKey()
const queryFn = () => 'data'
const observer = new QueryObserver(queryClient, {
queryKey: key1,
queryFn,
gcTime: 1000,
})
const unsubscribe = observer.subscribe(noop)
await vi.advanceTimersByTimeAsync(0)
const query1 = queryCache.find({ queryKey: key1 })!

observer.setOptions({ queryKey: key2, queryFn, gcTime: Infinity })

await vi.advanceTimersByTimeAsync(999)
expect(queryCache.find({ queryKey: key1 })).toBe(query1)

// Removed after key1's gcTime, not kept forever with key2's
await vi.advanceTimersByTimeAsync(1)
expect(queryCache.find({ queryKey: key1 })).toBeUndefined()

unsubscribe()
})

it('should record the gcTime a fetch was started with, even if an observer changes it meanwhile', async () => {
const key = createKey()
// An existing query with a short gcTime, so its creation can't hide the fetch's removal time
const query = queryCache.build(queryClient, { queryKey: key, gcTime: 10 })

// The fetch asks for 1000ms and settles at 50ms: removal at 1050ms
const fetch = queryClient.query({
queryKey: key,
queryFn: () => sleep(50).then(() => 'data'),
gcTime: 1000,
})

// While it runs, an observer replaces the query's options with a gcTime of 10ms
const observer = new QueryObserver(queryClient, {
queryKey: key,
enabled: false,
gcTime: 10,
})
const unsubscribe = observer.subscribe(noop)

await vi.advanceTimersByTimeAsync(50)
await fetch
// 60ms is earlier, so removal stays at 1050ms
unsubscribe()

await vi.advanceTimersByTimeAsync(999)
expect(queryCache.find({ queryKey: key })).toBe(query)

await vi.advanceTimersByTimeAsync(1)
expect(queryCache.find({ queryKey: key })).toBeUndefined()
})

it('should keep a later removal time recorded at the end of an earlier fetch', async () => {
const key = createKey()
const queryFn = () => sleep(10).then(() => 'data')

// The first fetch settles at 10ms: removal at 1010ms
const first = queryClient.query({ queryKey: key, queryFn, gcTime: 1000 })
await vi.advanceTimersByTimeAsync(10)
await first
const query = queryCache.find({ queryKey: key })!
expect(query.gcTime).toBe(200)

// A second fetch settles at 510ms: 520ms is earlier, so removal stays at 1010ms
await vi.advanceTimersByTimeAsync(490)
const second = queryClient.query({ queryKey: key, queryFn, gcTime: 10 })
await vi.advanceTimersByTimeAsync(10)
await second

await vi.advanceTimersByTimeAsync(499)
expect(queryCache.find({ queryKey: key })).toBe(query)

await vi.advanceTimersByTimeAsync(1)
expect(queryCache.find({ queryKey: key })).toBeUndefined()
})

it('should continue retry after focus regain and resolve all promises', async () => {
Expand Down Expand Up @@ -886,7 +979,9 @@ describe('query', () => {
expect(query.getObserversCount()).toBe(0)

const notifySpy = vi.spyOn(queryCache, 'notify')
expect(() => query.removeObserver(observer)).not.toThrow()
expect(() =>
query.removeObserver(observer, observer.options.gcTime),
).not.toThrow()
expect(notifySpy).not.toHaveBeenCalled()
})

Expand Down Expand Up @@ -1034,16 +1129,19 @@ describe('query', () => {
resetIsServer()
})

it('should use an infinite garbage collection time on the server', () => {
it('should use an infinite garbage collection time on the server', async () => {
const resetIsServer = setIsServer(true)

try {
const key = createKey()
const query = queryCache.build(queryClient, {
queryKey: createKey(),
queryKey: key,
queryFn: () => 'data',
})

expect(query.gcTime).toBe(Infinity)
await vi.advanceTimersByTimeAsync(24 * 60 * 60 * 1000)

expect(queryCache.find({ queryKey: key })).toBe(query)
} finally {
resetIsServer()
}
Expand Down Expand Up @@ -1497,7 +1595,7 @@ describe('query', () => {
const promise1 = query.fetch()
await vi.advanceTimersByTimeAsync(10)

query.removeObserver(observer1)
query.removeObserver(observer1, observer1.options.gcTime)

const observer2 = new QueryObserver(queryClient, {
queryKey: key,
Expand Down
Loading
Loading