(
options: Accessor<
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
>,
Observer: typeof QueryObserver,
queryClient?: Accessor<QueryClient>,
)
| 17 | * @param queryClient - Custom query client which overrides provider |
| 18 | */ |
| 19 | export 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 | // The following is convoluted but necessary: |
| 76 | // Call eagerly so subscription happens on the server and on suspended branches in the client... |
no test coverage detected