@@ -3,10 +3,10 @@ import {
33 queryOptions ,
44 type QueryObserverResult ,
55} from '@tanstack/svelte-query' ;
6- import { MachineConnectionEvent , type ResourceName } from '@viamrobotics/sdk' ;
7- import { getContext , setContext , untrack } from 'svelte' ;
6+ import type { ResourceName } from '@viamrobotics/sdk' ;
7+ import { getContext , setContext } from 'svelte' ;
88import { fromStore , toStore } from 'svelte/store' ;
9- import { useConnectionStatuses , useRobotClients } from './robot-clients.svelte' ;
9+ import { useRobotClients } from './robot-clients.svelte' ;
1010import type { PartID } from '../part' ;
1111import { useMachineStatuses } from './machine-status.svelte' ;
1212import { useQueryLogger } from '$lib/query-logger' ;
@@ -16,11 +16,19 @@ const key = Symbol('resources-context');
1616
1717type Query = QueryObserverResult < ResourceName [ ] , Error > ;
1818
19+ /** @todo (mp) Expose in the ts-sdk and remove */
20+ const MachineState = {
21+ Unspecified : 0 ,
22+ Initializing : 1 ,
23+ Running : 2 ,
24+ } ;
25+
1926interface QueryContext {
2027 current : ResourceName [ ] ;
2128 error : Error | undefined ;
2229 fetching : boolean ;
2330 loading : boolean ;
31+ pending : boolean ;
2432 refetch : ( ) => Promise < Query > | Promise < void > ;
2533}
2634
@@ -74,7 +82,6 @@ const sortResourceNames = (resourceNames: ResourceName[]) => {
7482} ;
7583
7684export const provideResourceNamesContext = ( ) => {
77- const connectionStatuses = useConnectionStatuses ( ) ;
7885 const machineStatuses = useMachineStatuses ( ) ;
7986 const clients = useRobotClients ( ) ;
8087 const debug = useQueryLogger ( ) ;
@@ -84,9 +91,12 @@ export const provideResourceNamesContext = () => {
8491 const results = [ ] ;
8592
8693 for ( const partID of partIDs ) {
94+ const machineStatus = machineStatuses . current [ partID ] ?. data ;
8795 const client = clients . current [ partID ] ;
8896 const options = queryOptions ( {
89- enabled : client !== undefined ,
97+ refetchOnMount : false ,
98+ enabled :
99+ client !== undefined && machineStatus ?. state === MachineState . Running ,
90100 queryKey : [
91101 'viam-svelte-sdk' ,
92102 'partID' ,
@@ -147,38 +157,18 @@ export const provideResourceNamesContext = () => {
147157 } ) ;
148158
149159 /**
150- * ResourceNames are not guaranteed on first fetch, refetch until they're populated
160+ * Refetch part resource names based on revision
151161 */
152162 $effect ( ( ) => {
153163 for ( const partID of partIDs ) {
154- const status = connectionStatuses . current [ partID ] ;
155- const query = queries . current [ partID ] ;
156- const connected = status === MachineConnectionEvent . CONNECTED ;
157- if (
158- connected &&
159- query ?. isFetched &&
160- ! query . isLoading &&
161- query . data ?. length === 0
162- ) {
163- untrack ( ( ) => debouncedRefetch [ partID ] ?.( ) ) ;
164- }
165- }
166- } ) ;
167-
168- /**
169- * Individually refetch part resource names based on revision
170- */
171- $effect ( ( ) => {
172- for ( const partID of partIDs ) {
173- const revision =
174- machineStatuses . current [ partID ] ?. data ?. config ?. revision ?? '' ;
164+ const machineStatus = machineStatuses . current [ partID ] ?. data ;
165+ const revision = machineStatus ?. config ?. revision ?? '' ;
175166 const lastRevision = revisions . get ( partID ) ;
176- revisions . set ( partID , revision ) ;
177167
178- if ( ! lastRevision ) continue ;
168+ revisions . set ( partID , revision ) ;
179169
180- if ( revision !== lastRevision ) {
181- queries . current [ partID ] ?. refetch ( ) ;
170+ if ( lastRevision && revision !== lastRevision ) {
171+ debouncedRefetch [ partID ] ?.( ) ;
182172 }
183173 }
184174 } ) ;
@@ -196,6 +186,7 @@ export const useResourceNames = (
196186 const error = $derived ( query ?. error ?? undefined ) ;
197187 const fetching = $derived ( query ?. isFetching ?? true ) ;
198188 const loading = $derived ( query ?. isLoading ?? true ) ;
189+ const pending = $derived ( query ?. isPending ?? true ) ;
199190
200191 const subtype = $derived (
201192 typeof resourceSubtype === 'function' ? resourceSubtype ( ) : resourceSubtype
@@ -228,6 +219,9 @@ export const useResourceNames = (
228219 get loading ( ) {
229220 return loading ;
230221 } ,
222+ get pending ( ) {
223+ return pending ;
224+ } ,
231225 refetch ( ) {
232226 return query ?. refetch ( ) ?? Promise . resolve ( ) ;
233227 } ,
0 commit comments