Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createBaseQuery.svelte.ts

107 lines3.0 KB

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