Skip to content
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

fix(QueriesObserver): fix improper sorting in QueriesObserver's #find… #8351

Merged
merged 4 commits into from
Nov 26, 2024
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
68 changes: 68 additions & 0 deletions packages/query-core/src/__tests__/queriesObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,72 @@ describe('queriesObserver', () => {
// Clean-up
unsubscribe2()
})

test('should handle duplicate query keys in different positions', async () => {
const key1 = queryKey()
const key2 = queryKey()
const queryFn1 = vi.fn().mockReturnValue(1)
const queryFn2 = vi.fn().mockReturnValue(2)

const observer = new QueriesObserver(queryClient, [
{ queryKey: key1, queryFn: queryFn1 },
{ queryKey: key2, queryFn: queryFn2 },
{ queryKey: key1, queryFn: queryFn1 },
])

const results: Array<Array<QueryObserverResult>> = []
results.push(
observer.getOptimisticResult(
[
{ queryKey: key1, queryFn: queryFn1 },
{ queryKey: key2, queryFn: queryFn2 },
{ queryKey: key1, queryFn: queryFn1 },
],
undefined,
)[0],
)

const unsubscribe = observer.subscribe((result) => {
results.push(result)
})

await sleep(1)
unsubscribe()

expect(results.length).toBe(6)
expect(results[0]).toMatchObject([
{ status: 'pending', fetchStatus: 'idle', data: undefined },
{ status: 'pending', fetchStatus: 'idle', data: undefined },
{ status: 'pending', fetchStatus: 'idle', data: undefined },
])
expect(results[1]).toMatchObject([
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
{ status: 'pending', fetchStatus: 'idle', data: undefined },
{ status: 'pending', fetchStatus: 'idle', data: undefined },
])
expect(results[2]).toMatchObject([
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
{ status: 'pending', fetchStatus: 'idle', data: undefined },
])
expect(results[3]).toMatchObject([
{ status: 'success', fetchStatus: 'idle', data: 1 },
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
{ status: 'pending', fetchStatus: 'idle', data: undefined },
])
expect(results[4]).toMatchObject([
{ status: 'success', fetchStatus: 'idle', data: 1 },
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
{ status: 'success', fetchStatus: 'idle', data: 1 },
])
expect(results[5]).toMatchObject([
{ status: 'success', fetchStatus: 'idle', data: 1 },
{ status: 'success', fetchStatus: 'idle', data: 2 },
{ status: 'success', fetchStatus: 'idle', data: 1 },
])

// Verify that queryFn1 was only called once despite being used twice
expect(queryFn1).toHaveBeenCalledTimes(1)
expect(queryFn2).toHaveBeenCalledTimes(1)
})
})
18 changes: 2 additions & 16 deletions packages/query-core/src/queriesObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,14 @@ export class QueriesObserver<
observer: match,
})
} else {
const existingObserver = this.#observers.find(
(o) => o.options.queryHash === defaultedOptions.queryHash,
)
observers.push({
defaultedQueryOptions: defaultedOptions,
observer:
existingObserver ??
new QueryObserver(this.#client, defaultedOptions),
observer: new QueryObserver(this.#client, defaultedOptions),
TkDodo marked this conversation as resolved.
Show resolved Hide resolved
})
}
})

return observers.sort((a, b) => {
return (
queries.findIndex(
(q) => q.queryHash === a.defaultedQueryOptions.queryHash,
) -
queries.findIndex(
(q) => q.queryHash === b.defaultedQueryOptions.queryHash,
)
)
})
return observers
}

#onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
Expand Down