@@ -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 { useDebounce } from 'runed' ;
@@ -15,11 +15,19 @@ const key = Symbol('resources-context');
1515
1616type Query = QueryObserverResult < ResourceName [ ] , Error > ;
1717
18+ /** @todo (mp) Expose in the ts-sdk and remove */
19+ const MachineState = {
20+ Unspecified : 0 ,
21+ Initializing : 1 ,
22+ Running : 2 ,
23+ } ;
24+
1825interface QueryContext {
1926 current : ResourceName [ ] ;
2027 error : Error | undefined ;
2128 fetching : boolean ;
2229 loading : boolean ;
30+ pending : boolean ;
2331 refetch : ( ) => Promise < Query > | Promise < void > ;
2432}
2533
@@ -73,7 +81,6 @@ const sortResourceNames = (resourceNames: ResourceName[]) => {
7381} ;
7482
7583export const provideResourceNamesContext = ( ) => {
76- const connectionStatuses = useConnectionStatuses ( ) ;
7784 const machineStatuses = useMachineStatuses ( ) ;
7885 const clients = useRobotClients ( ) ;
7986
@@ -82,9 +89,12 @@ export const provideResourceNamesContext = () => {
8289 const results = [ ] ;
8390
8491 for ( const partID of partIDs ) {
92+ const machineStatus = machineStatuses . current [ partID ] ?. data ;
8593 const client = clients . current [ partID ] ;
8694 const options = queryOptions ( {
87- enabled : client !== undefined ,
95+ refetchOnMount : false ,
96+ enabled :
97+ client !== undefined && machineStatus ?. state === MachineState . Running ,
8898 queryKey : [
8999 'viam-svelte-sdk' ,
90100 'partID' ,
@@ -136,38 +146,18 @@ export const provideResourceNamesContext = () => {
136146 } ) ;
137147
138148 /**
139- * ResourceNames are not guaranteed on first fetch, refetch until they're populated
149+ * Refetch part resource names based on revision and machine state
140150 */
141151 $effect ( ( ) => {
142152 for ( const partID of partIDs ) {
143- const status = connectionStatuses . current [ partID ] ;
144- const query = queries . current [ partID ] ;
145- const connected = status === MachineConnectionEvent . CONNECTED ;
146- if (
147- connected &&
148- query ?. isFetched &&
149- ! query . isLoading &&
150- query . data ?. length === 0
151- ) {
152- untrack ( ( ) => debouncedRefetch [ partID ] ?.( ) ) ;
153- }
154- }
155- } ) ;
156-
157- /**
158- * Individually refetch part resource names based on revision
159- */
160- $effect ( ( ) => {
161- for ( const partID of partIDs ) {
162- const revision =
163- machineStatuses . current [ partID ] ?. data ?. config ?. revision ?? '' ;
153+ const machineStatus = machineStatuses . current [ partID ] ?. data ;
154+ const revision = machineStatus ?. config ?. revision ?? '' ;
164155 const lastRevision = revisions . get ( partID ) ;
165- revisions . set ( partID , revision ) ;
166156
167- if ( ! lastRevision ) continue ;
157+ revisions . set ( partID , revision ) ;
168158
169- if ( revision !== lastRevision ) {
170- queries . current [ partID ] ?. refetch ( ) ;
159+ if ( lastRevision && revision !== lastRevision ) {
160+ debouncedRefetch [ partID ] ?.( ) ;
171161 }
172162 }
173163 } ) ;
@@ -185,6 +175,7 @@ export const useResourceNames = (
185175 const error = $derived ( query ?. error ?? undefined ) ;
186176 const fetching = $derived ( query ?. isFetching ?? true ) ;
187177 const loading = $derived ( query ?. isLoading ?? true ) ;
178+ const pending = $derived ( query ?. isPending ?? true ) ;
188179
189180 const subtype = $derived (
190181 typeof resourceSubtype === 'function' ? resourceSubtype ( ) : resourceSubtype
@@ -217,6 +208,9 @@ export const useResourceNames = (
217208 get loading ( ) {
218209 return loading ;
219210 } ,
211+ get pending ( ) {
212+ return pending ;
213+ } ,
220214 refetch ( ) {
221215 return query ?. refetch ( ) ?? Promise . resolve ( ) ;
222216 } ,
0 commit comments