Open History to see the last commit
1import { untrack } from 'svelte'
2import { useIsRestoring } from './useIsRestoring.js'
3import { useQueryClient } from './useQueryClient.js'
4import { createRawRef } from './containers.svelte.js'
5import { watchChanges } from './utils.svelte.js'
6import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core'
7import type {
8 Accessor,
9 CreateBaseQueryOptions,
10 CreateBaseQueryResult,
11} from './types.js'
12β
13/**
14 * Base implementation for `createQuery` and `createInfiniteQuery`
15 * @param options - A function that returns query options
16 * @param Observer - The observer from query-core
17 * @param queryClient - Custom query client which overrides provider
18 */
19export function createBaseQuery<
20 TQueryFnData,
21 TError,
22 TData,
23 TQueryData,
24 TQueryKey extends QueryKey,
25>(
26 options: Accessor<
27 CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
28 >,
29 Observer: typeof QueryObserver,
30 queryClient?: Accessor<QueryClient>,
31): CreateBaseQueryResult<TData, TError> {
32 /** Load query client */
33 const client = $derived(useQueryClient(queryClient?.()))
34 const isRestoring = useIsRestoring()
35β
36 const resolvedOptions = $derived.by(() => {
37 const opts = client.defaultQueryOptions(options())
38 opts._optimisticResults = isRestoring.current ? 'isRestoring' : 'optimistic'
39 return opts
40 })
41β
42 /** Creates the observer */
43 // svelte-ignore state_referenced_locally - intentional, initial value
44 let observer = $state(
45 new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
46 client,
47 resolvedOptions,
48 ),
49 )
50 watchChanges(
51 () => client,
52 'pre',
53 () => {
54 observer = new Observer<
55 TQueryFnData,
56 TError,
57 TData,
58 TQueryData,
59 TQueryKey
60 >(client, resolvedOptions)
61 },
62 )
63β
64 function createResult() {
65 const result = observer.getOptimisticResult(resolvedOptions)
66 return !resolvedOptions.notifyOnChangeProps
67 ? observer.trackResult(result)
68 : result
69 }
70 const [query, update] = createRawRef(
71 // svelte-ignore state_referenced_locally - intentional, initial value
72 createResult(),
73 )
74β
75 $effect(() => {
76 const unsubscribe = isRestoring.current
77 ? () => undefined
78 : observer.subscribe(() => untrack(() => update(createResult())))
79 observer.updateResult()
80 return unsubscribe
81 })
82β
83 watchChanges(
84 () => resolvedOptions,
85 'pre',
86 () => {
87 observer.setOptions(resolvedOptions)
88 },
89 )
90 watchChanges(
91 () => [resolvedOptions, observer],
92 'pre',
93 () => {
94 // The only reason this is necessary is because of `isRestoring`.
95 // Because we don't subscribe while restoring, the following can occur:
96 // - `isRestoring` is true
97 // - `isRestoring` becomes false
98 // - `observer.subscribe` and `observer.updateResult` is called in the above effect,
99 // but the subsequent `fetch` has already completed
100 // - `result` misses the intermediate restored-but-not-fetched state
101 //
102 // this could technically be its own effect but that doesn't seem necessary
103 update(createResult())
104 },
105 )
106β
107 return query
108}