Skip to content
Source
Sign inSign up
TanStack/query

fix(svelte-query): read result properties first accessed after the query settled from the current result #12487

Openlazerg(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12487/headopened View on GitHub

Showing 3 changed files with 53 additions and 3 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/svelte-late-tracked-property.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/svelte-query': patch
+---
+​
+Return the latest value of a query result property that is first read after the query settled, instead of the value from before it settled.
11 additions and 3 deletionspackages/svelte-query/src/createBaseQuery.svelte.tsModified
View file
@@ -69,9 +69,17 @@
*/
function createResult() {
const result = observer.getOptimisticResult(resolvedOptions)
- return !resolvedOptions.notifyOnChangeProps
- ? observer.trackResult(result)
- : result
+ if (resolvedOptions.notifyOnChangeProps) {
+ return result
+ }
+ // Changes to properties that weren't tracked yet don't notify, so read them from the
+ // observer's current result instead of the one captured here.
+ const currentObserver = observer
+ return observer.trackResult(
+ new Proxy(result, {
+ get: (_, key) => Reflect.get(currentObserver.getCurrentResult(), key),
+ }),
+ )
const [query, update] = createRawRef(
// svelte-ignore state_referenced_locally - intentional, initial value