-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(query-core): add structuralSharing option to useQueries #10101
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
1956f42
f9b6982
4a13c69
9950db7
f1815fc
c6399f0
eabd1fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@tanstack/angular-query-experimental': minor | ||
| '@tanstack/svelte-query': minor | ||
| '@tanstack/react-query': minor | ||
| '@tanstack/solid-query': minor | ||
| '@tanstack/query-core': minor | ||
| '@tanstack/vue-query': minor | ||
| --- | ||
|
|
||
| feat(query-core): Allow disabling structuralSharing for useQueries. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -211,6 +211,12 @@ export interface InjectQueriesOptions< | |
| ...{ [K in keyof T]: GetCreateQueryOptionsForCreateQueries<T[K]> }, | ||
| ] | ||
| combine?: (result: QueriesResults<T>) => TCombinedResult | ||
| /** | ||
| * Set this to `false` to disable structural sharing between query results. | ||
| * Only applies when `combine` is provided. | ||
| * Defaults to `true`. | ||
| */ | ||
| structuralSharing?: boolean | ||
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -271,6 +277,8 @@ export function injectQueries< | |
| observerSignal().getOptimisticResult( | ||
| defaultedQueries(), | ||
| (optionsSignal() as QueriesObserverOptions<TCombinedResult>).combine, | ||
| (optionsSignal() as QueriesObserverOptions<TCombinedResult>) | ||
| .structuralSharing, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,12 @@ export interface QueriesObserverOptions< | |
| TCombinedResult = Array<QueryObserverResult>, | ||
| > { | ||
| combine?: CombineFn<TCombinedResult> | ||
| /** | ||
| * Set this to `false` to disable structural sharing between query results. | ||
| * Only applies when `combine` is provided. | ||
| * Defaults to `true`. | ||
| */ | ||
| structuralSharing?: boolean | ||
| } | ||
|
|
||
| export class QueriesObserver< | ||
|
|
@@ -172,6 +178,7 @@ export class QueriesObserver< | |
| getOptimisticResult( | ||
| queries: Array<QueryObserverOptions>, | ||
| combine: CombineFn<TCombinedResult> | undefined, | ||
| structuralSharing: boolean | undefined, | ||
| ): [ | ||
| rawResult: Array<QueryObserverResult>, | ||
| combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult, | ||
|
|
@@ -188,7 +195,12 @@ export class QueriesObserver< | |
| return [ | ||
| result, | ||
| (r?: Array<QueryObserverResult>) => { | ||
| return this.#combineResult(r ?? result, combine, queryHashes) | ||
| return this.#combineResult( | ||
| r ?? result, | ||
| combine, | ||
| structuralSharing, | ||
| queryHashes, | ||
| ) | ||
| }, | ||
| () => { | ||
| return this.#trackResult(result, matches) | ||
|
|
@@ -216,6 +228,7 @@ export class QueriesObserver< | |
| #combineResult( | ||
| input: Array<QueryObserverResult>, | ||
| combine: CombineFn<TCombinedResult> | undefined, | ||
| structuralSharing: boolean | undefined = true, | ||
| queryHashes?: Array<string>, | ||
| ): TCombinedResult { | ||
| if (combine) { | ||
|
|
@@ -238,10 +251,12 @@ export class QueriesObserver< | |
| if (queryHashes !== undefined) { | ||
| this.#lastQueryHashes = queryHashes | ||
| } | ||
| this.#combinedResult = replaceEqualDeep( | ||
| this.#combinedResult, | ||
| combine(input), | ||
| ) | ||
|
|
||
| const combined = combine(input) | ||
|
|
||
| this.#combinedResult = structuralSharing | ||
| ? replaceEqualDeep(this.#combinedResult, combined) | ||
| : combined | ||
|
||
| } | ||
|
|
||
| return this.#combinedResult | ||
|
|
@@ -296,7 +311,11 @@ export class QueriesObserver< | |
| if (this.hasListeners()) { | ||
| const previousResult = this.#combinedResult | ||
| const newTracked = this.#trackResult(this.#result, this.#observerMatches) | ||
| const newResult = this.#combineResult(newTracked, this.#options?.combine) | ||
| const newResult = this.#combineResult( | ||
| newTracked, | ||
| this.#options?.combine, | ||
| this.#options?.structuralSharing, | ||
| ) | ||
|
|
||
| if (previousResult !== newResult) { | ||
| notifyManager.batch(() => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also do the new
preactadapterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done ✅