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: improve long running task performance in query core #8107

Merged
merged 12 commits into from
Oct 9, 2024
Merged
Prev Previous commit
Next Next commit
refactor: remove two more arrays to be more efficient
davidaghassi committed Oct 1, 2024

Verified

This commit was signed with the committer’s verified signature.
jasnell James M Snell
commit c1d125c42738caac292e6d03035c675a4ea0418a
29 changes: 11 additions & 18 deletions packages/query-core/src/queriesObserver.ts
Original file line number Diff line number Diff line change
@@ -208,37 +208,30 @@ export class QueriesObserver<
this.#observers.map((observer) => [observer.options.queryHash, observer]),
)

const matchingObservers: Array<QueryObserverMatch> = []
const unmatchedQueries: Array<DefaultedQueryObserverOptions> = []
const observers: Array<QueryObserverMatch> = []

queries.forEach((options) => {
const defaultedOptions = this.#client.defaultQueryOptions(options)
const match = prevObserversMap.get(defaultedOptions.queryHash)
if (match) {
matchingObservers.push({
observers.push({
defaultedQueryOptions: defaultedOptions,
observer: match,
})
} else {
unmatchedQueries.push(defaultedOptions)
}
})

const newOrReusedObservers: Array<QueryObserverMatch> =
unmatchedQueries.map((options) => {
const existingObserver = this.#observers.find(
(o) => o.options.queryHash === options.queryHash,
(o) => o.options.queryHash === defaultedOptions.queryHash,
)
return {
defaultedQueryOptions: options,
observers.push({
defaultedQueryOptions: defaultedOptions,
observer:
existingObserver ?? new QueryObserver(this.#client, options),
}
})

const allObservers = matchingObservers.concat(newOrReusedObservers)
existingObserver ??
new QueryObserver(this.#client, defaultedOptions),
})
}
})

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