Skip to content

Commit f2ee414

Browse files
fix(svelte-query): Fix staleTime reactivity bug (#5682)
1 parent 07318de commit f2ee414

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/svelte-query/src/createBaseQuery.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ export function createBaseQuery<
2222
Observer: typeof QueryObserver,
2323
queryClient?: QueryClient,
2424
): CreateBaseQueryResult<TData, TError> {
25+
/** Load query client */
2526
const client = useQueryClient(queryClient)
2627

28+
/** Converts options to a svelte store if not already a store object */
2729
const optionsStore = isSvelteStore(options) ? options : readable(options)
2830

29-
const defaultedOptionsStore = derived(optionsStore, ($options) => {
30-
const defaultedOptions = client.defaultQueryOptions($options)
31+
/** Creates a store that has the default options applied */
32+
const defaultedOptionsStore = derived(optionsStore, ($optionsStore) => {
33+
const defaultedOptions = client.defaultQueryOptions($optionsStore)
3134
defaultedOptions._optimisticResults = 'optimistic'
32-
3335
return defaultedOptions
3436
})
3537

38+
/** Creates the observer */
3639
const observer = new Observer<
3740
TQueryFnData,
3841
TError,
@@ -51,12 +54,16 @@ export function createBaseQuery<
5154
return observer.subscribe(notifyManager.batchCalls(set))
5255
})
5356

54-
const { subscribe } = derived(result, ($result) => {
55-
$result = observer.getOptimisticResult(get(defaultedOptionsStore))
56-
return !get(defaultedOptionsStore).notifyOnChangeProps
57-
? observer.trackResult($result)
58-
: $result
59-
})
57+
/** Subscribe to changes in result and defaultedOptionsStore */
58+
const { subscribe } = derived(
59+
[result, defaultedOptionsStore],
60+
([$result, $defaultedOptionsStore]) => {
61+
$result = observer.getOptimisticResult($defaultedOptionsStore)
62+
return !$defaultedOptionsStore.notifyOnChangeProps
63+
? observer.trackResult($result)
64+
: $result
65+
},
66+
)
6067

6168
return { subscribe }
6269
}

0 commit comments

Comments
 (0)