|
1 | 1 | import { notifyManager } from '@tanstack/query-core'
|
2 |
| -import { untrack } from 'svelte' |
3 | 2 | import { useIsRestoring } from './useIsRestoring'
|
4 | 3 | import { useQueryClient } from './useQueryClient'
|
5 | 4 | import type {
|
@@ -32,81 +31,53 @@ export function createBaseQuery<
|
32 | 31 | const isRestoring = useIsRestoring()
|
33 | 32 |
|
34 | 33 | /** Creates a store that has the default options applied */
|
35 |
| - function updateOptions() { |
36 |
| - const queryKey: TQueryKey = $state.snapshot(options().queryKey) as any // remove proxy prevent reactive query in DevTools |
37 |
| - const defaultedOptions = client.defaultQueryOptions({ |
38 |
| - ...options(), |
39 |
| - queryKey: queryKey, |
40 |
| - }) |
41 |
| - defaultedOptions._optimisticResults = 'optimistic' |
42 |
| - if (isRestoring()) { |
43 |
| - defaultedOptions._optimisticResults = 'isRestoring' |
44 |
| - } |
45 |
| - |
46 |
| - defaultedOptions.structuralSharing = false |
47 |
| - |
48 |
| - return defaultedOptions |
49 |
| - } |
| 34 | + const defaultedOptions = $derived(() => { |
| 35 | + const defaultOptions = client.defaultQueryOptions(options()) |
| 36 | + defaultOptions._optimisticResults = isRestoring() |
| 37 | + ? 'isRestoring' |
| 38 | + : 'optimistic' |
| 39 | + defaultOptions.structuralSharing = false |
| 40 | + return defaultOptions |
| 41 | + }) |
50 | 42 |
|
51 |
| - const defaultedOptionsStore = updateOptions |
52 | 43 | /** Creates the observer */
|
53 | 44 | const observer = new Observer<
|
54 | 45 | TQueryFnData,
|
55 | 46 | TError,
|
56 | 47 | TData,
|
57 | 48 | TQueryData,
|
58 | 49 | TQueryKey
|
59 |
| - >(client, defaultedOptionsStore()) |
| 50 | + >(client, defaultedOptions()) |
60 | 51 |
|
61 | 52 | const result = $state<QueryObserverResult<TData, TError>>(
|
62 |
| - observer.getOptimisticResult(defaultedOptionsStore()), |
| 53 | + observer.getOptimisticResult(defaultedOptions()), |
63 | 54 | )
|
64 | 55 |
|
65 |
| - function upResult(r: QueryObserverResult<TData, TError>) { |
| 56 | + function updateResult(r: QueryObserverResult<TData, TError>) { |
66 | 57 | Object.assign(result, r)
|
67 | 58 | }
|
68 | 59 |
|
69 | 60 | $effect(() => {
|
70 |
| - let un = () => undefined |
71 |
| - if (!isRestoring()) { |
72 |
| - { |
73 |
| - // @ts-expect-error |
74 |
| - un = observer.subscribe((v) => { |
| 61 | + const unsubscribe = isRestoring() |
| 62 | + ? () => undefined |
| 63 | + : observer.subscribe(() => { |
75 | 64 | notifyManager.batchCalls(() => {
|
76 |
| - const temp = observer.getOptimisticResult(defaultedOptionsStore()) |
77 |
| - upResult(temp) |
| 65 | + updateResult(observer.getOptimisticResult(defaultedOptions())) |
78 | 66 | })()
|
79 | 67 | })
|
80 |
| - } |
81 |
| - } |
82 | 68 |
|
83 | 69 | observer.updateResult()
|
84 |
| - return () => { |
85 |
| - un() |
86 |
| - } |
| 70 | + return () => unsubscribe() |
87 | 71 | })
|
88 | 72 |
|
89 | 73 | /** Subscribe to changes in result and defaultedOptionsStore */
|
90 | 74 | $effect.pre(() => {
|
91 |
| - observer.setOptions(defaultedOptionsStore(), { listeners: false }) |
92 |
| - upResult(observer.getOptimisticResult(defaultedOptionsStore())) |
93 |
| - // result = observer.getOptimisticResult(defaultedOptionsStore()) //prevent lag , somehow observer.subscribe does not return |
| 75 | + observer.setOptions(defaultedOptions(), { listeners: false }) |
| 76 | + updateResult(observer.getOptimisticResult(defaultedOptions())) |
94 | 77 | })
|
95 | 78 |
|
96 |
| - const final_ = $state({ value: result }) |
97 |
| - |
98 |
| - // update result |
99 |
| - $effect(() => { |
100 |
| - // svelte does not need this with it is proxy state and fine-grained reactivity? |
101 |
| - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
102 |
| - if (result !== null) |
103 |
| - untrack(() => { |
104 |
| - const v = !defaultedOptionsStore().notifyOnChangeProps |
105 |
| - ? observer.trackResult(result) |
106 |
| - : result |
107 |
| - |
108 |
| - final_.value = Object.assign(final_.value, v) |
109 |
| - }) |
110 |
| - }) |
111 |
| - return final_.value |
| 79 | + // Handle result property usage tracking |
| 80 | + return !defaultedOptions().notifyOnChangeProps |
| 81 | + ? observer.trackResult(result) |
| 82 | + : result |
112 | 83 | }
|
0 commit comments