Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix O(N) time & space in QueryCache search when queryKey is present #9589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
justjake wants to merge 8 commits into TanStack:main
base: main
Choose a base branch
Loading
from justjake:jake--queryCache-indexing
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
63 changes: 63 additions & 0 deletions packages/query-core/src/__tests__/queryCache.test.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,69 @@ describe('queryCache', () => {
expect(queryCache.findAll({ fetchStatus: 'fetching' })).toEqual([])
})

test('only visits queries with the given prefix', async () => {
const prefix = ['posts']
queryClient.prefetchQuery({
queryKey: ['other'],
queryFn: () => sleep(100).then(() => 'other'),
})
queryClient.prefetchQuery({
queryKey: ['other', {}],
queryFn: () => sleep(100).then(() => 'otherObjectSuffix'),
})
queryClient.prefetchQuery({
queryKey: [{}, 'objectPrefix'],
queryFn: () => sleep(100).then(() => 'otherObjectPrefix'),
})

queryClient.prefetchQuery({
queryKey: prefix,
queryFn: () => sleep(100).then(() => 'exactMatch'),
})
const exactMatchQuery = queryCache.find({ queryKey: prefix })

queryClient.prefetchQuery({
queryKey: [...prefix, 1],
queryFn: () => sleep(100).then(() => 'primitiveSuffix'),
})
const primitiveSuffixQuery = queryCache.find({ queryKey: [...prefix, 1] })

queryClient.prefetchQuery({
queryKey: [...prefix, 2, 'primitiveSuffix'],
queryFn: () => sleep(100).then(() => 'primitiveSuffixLength2'),
})
const primitiveSuffixLength2Query = queryCache.find({
queryKey: [...prefix, 2, 'primitiveSuffix'],
})

queryClient.prefetchQuery({
queryKey: [...prefix, 3, {}, 'obj'],
queryFn: () => sleep(100).then(() => 'matchingObjectSuffix'),
})
const matchingObjectSuffixQuery = queryCache.find({
queryKey: [...prefix, 3, {}, 'obj'],
})

await vi.advanceTimersByTimeAsync(100)

let predicateCallCount = 0
const results = queryCache.findAll({
queryKey: prefix,
predicate: () => {
predicateCallCount++
return true
},
})

expect(predicateCallCount).toBe(results.length)
expect(results).toEqual([
exactMatchQuery,
primitiveSuffixQuery,
primitiveSuffixLength2Query,
matchingObjectSuffixQuery,
])
})

test('should return all the queries when no filters are defined', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
15 changes: 15 additions & 0 deletions packages/query-core/src/query.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,23 @@ export class Query<
setOptions(
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
): void {
const oldOptions = (this.options as typeof this.options | undefined)
? this.options
: undefined

this.options = { ...this.#defaultOptions, ...options }

if (oldOptions) {
// Do not do this update when first created.
// The QueryCache manages admission / removal itself.
// We only need to keep it up-to-date here.
const prevHashFn = oldOptions.queryKeyHashFn
const newHashFn = this.options.queryKeyHashFn
if (prevHashFn !== newHashFn) {
this.#cache.onQueryKeyHashFunctionChanged(prevHashFn, newHashFn)
}
}

this.updateGcTime(this.options.gcTime)
}

Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /