(
options: StoreOrVal<
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
>,
Observer: typeof QueryObserver,
queryClient?: QueryClient,
)
| 16 | } from './types.js' |
| 17 | |
| 18 | export function createBaseQuery< |
| 19 | TQueryFnData, |
| 20 | TError, |
| 21 | TData, |
| 22 | TQueryData, |
| 23 | TQueryKey extends QueryKey, |
| 24 | >( |
| 25 | options: StoreOrVal< |
| 26 | CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> |
| 27 | >, |
| 28 | Observer: typeof QueryObserver, |
| 29 | queryClient?: QueryClient, |
| 30 | ): CreateBaseQueryResult<TData, TError> { |
| 31 | /** Load query client */ |
| 32 | const client = useQueryClient(queryClient) |
| 33 | const isRestoring = useIsRestoring() |
| 34 | /** Converts options to a svelte store if not already a store object */ |
| 35 | const optionsStore = isSvelteStore(options) ? options : readable(options) |
| 36 | |
| 37 | /** Creates a store that has the default options applied */ |
| 38 | const defaultedOptionsStore = derived( |
| 39 | [optionsStore, isRestoring], |
| 40 | ([$optionsStore, $isRestoring]) => { |
| 41 | const defaultedOptions = client.defaultQueryOptions($optionsStore) |
| 42 | defaultedOptions._optimisticResults = $isRestoring |
| 43 | ? 'isRestoring' |
| 44 | : 'optimistic' |
| 45 | return defaultedOptions |
| 46 | }, |
| 47 | ) |
| 48 | |
| 49 | /** Creates the observer */ |
| 50 | const observer = new Observer< |
| 51 | TQueryFnData, |
| 52 | TError, |
| 53 | TData, |
| 54 | TQueryData, |
| 55 | TQueryKey |
| 56 | >(client, get(defaultedOptionsStore)) |
| 57 | |
| 58 | defaultedOptionsStore.subscribe(($defaultedOptions) => { |
| 59 | observer.setOptions($defaultedOptions) |
| 60 | }) |
| 61 | |
| 62 | const result = derived< |
| 63 | typeof isRestoring, |
| 64 | QueryObserverResult<TData, TError> |
| 65 | >(isRestoring, ($isRestoring, set) => { |
| 66 | const unsubscribe = $isRestoring |
| 67 | ? noop |
| 68 | : observer.subscribe(notifyManager.batchCalls(set)) |
| 69 | observer.updateResult() |
| 70 | return unsubscribe |
| 71 | }) |
| 72 | |
| 73 | /** Subscribe to changes in result and defaultedOptionsStore */ |
| 74 | const { subscribe } = derived( |
| 75 | [result, defaultedOptionsStore], |
no test coverage detected
searching dependent graphs…