|
| 1 | +import { |
| 2 | + createQuery, |
| 3 | + queryOptions as createQueryOptions, |
| 4 | + type QueryObserverResult, |
| 5 | +} from '@tanstack/svelte-query'; |
| 6 | +import type { DataClient } from '@viamrobotics/sdk'; |
| 7 | +import { toStore, fromStore } from 'svelte/store'; |
| 8 | +import { usePolling } from '../use-polling.svelte'; |
| 9 | +import { useQueryLogger } from '../../query-logger'; |
| 10 | +import { useViamClient } from './use-app-client.svelte'; |
| 11 | +import type { ArgumentsType, ResolvedReturnType } from './types'; |
| 12 | + |
| 13 | +interface QueryOptions { |
| 14 | + // enabled defaults to true if unspecified |
| 15 | + enabled?: boolean; |
| 16 | + refetchInterval: number | false; |
| 17 | + refetchIntervalInBackground?: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +export const createDataQuery = <T extends DataClient, K extends keyof T>( |
| 21 | + method: K, |
| 22 | + ...additional: |
| 23 | + | [ |
| 24 | + args?: (() => ArgumentsType<T[K]>) | ArgumentsType<T[K]>, |
| 25 | + options?: (() => QueryOptions) | QueryOptions, |
| 26 | + ] |
| 27 | + | [options?: (() => QueryOptions) | QueryOptions] |
| 28 | +): { current: QueryObserverResult<ResolvedReturnType<T[K]>> } => { |
| 29 | + const viamClient = useViamClient(); |
| 30 | + const dataClient = $derived(viamClient.current?.dataClient as T); |
| 31 | + const debug = useQueryLogger(); |
| 32 | + |
| 33 | + let [args, options] = additional; |
| 34 | + |
| 35 | + if (options === undefined && args !== undefined) { |
| 36 | + options = args as QueryOptions; |
| 37 | + args = undefined; |
| 38 | + } |
| 39 | + |
| 40 | + const _options = $derived( |
| 41 | + typeof options === 'function' ? options() : options |
| 42 | + ); |
| 43 | + const _args = $derived(typeof args === 'function' ? args() : args); |
| 44 | + const methodName = $derived(String(method)); |
| 45 | + |
| 46 | + const queryOptions = $derived( |
| 47 | + createQueryOptions({ |
| 48 | + queryKey: [ |
| 49 | + 'viam-svelte-app-sdk', |
| 50 | + 'dataClient', |
| 51 | + methodName, |
| 52 | + ...(_args ? [_args] : []), |
| 53 | + ], |
| 54 | + enabled: dataClient !== undefined && _options?.enabled !== false, |
| 55 | + queryFn: async () => { |
| 56 | + if (!dataClient) { |
| 57 | + throw new Error('dataClient is undefined'); |
| 58 | + } |
| 59 | + |
| 60 | + const clientFunc = dataClient[method]; |
| 61 | + |
| 62 | + if (typeof clientFunc !== 'function') { |
| 63 | + throw new TypeError( |
| 64 | + `${String(method)} is not a method on the resource client.` |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + const logger = debug.createLogger(); |
| 69 | + logger('REQ', 'dataClient', methodName, _args); |
| 70 | + |
| 71 | + try { |
| 72 | + const response = (await clientFunc.apply( |
| 73 | + dataClient, |
| 74 | + _args |
| 75 | + )) as Promise<ResolvedReturnType<T[K]>>; |
| 76 | + |
| 77 | + logger('RES', 'dataClient', methodName, response); |
| 78 | + return response; |
| 79 | + } catch (error) { |
| 80 | + logger('ERR', 'dataClient', methodName, error); |
| 81 | + throw error; |
| 82 | + } |
| 83 | + }, |
| 84 | + ..._options, |
| 85 | + refetchInterval: false, |
| 86 | + }) |
| 87 | + ); |
| 88 | + |
| 89 | + usePolling( |
| 90 | + () => queryOptions.queryKey, |
| 91 | + () => _options?.refetchInterval ?? false |
| 92 | + ); |
| 93 | + |
| 94 | + return fromStore(createQuery(toStore(() => queryOptions))); |
| 95 | +}; |
0 commit comments