Skip to content

Commit bec5dad

Browse files
Merge pull request #15 from viamrobotics/client-undefined-check
Enable machine status and resource names queries only when robot client is defined
2 parents eef2b72 + ecd531b commit bec5dad

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

.changeset/eighty-singers-fall.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@viamrobotics/svelte-sdk': patch
3+
---
4+
5+
Enable machine status and resource names queries only when robot client is defined

src/lib/compare.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const isJsonEqual = (object1: object, object2: object) => {
3434
return false;
3535
}
3636
}
37+
3738
return true;
3839
};
3940

src/lib/hooks/machine-status.svelte.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ export const provideMachineStatusContext = (refetchInterval: () => number) => {
2424
const options = $derived(
2525
Object.entries(clients.current).map(([partID, client]) => {
2626
return queryOptions({
27+
enabled: client !== undefined,
2728
queryKey: ['partID', partID, 'robotClient', 'machineStatus'],
2829
refetchInterval: refetchInterval(),
29-
queryFn: async () => {
30-
return client?.getMachineStatus();
30+
queryFn: async (): Promise<MachineStatus> => {
31+
if (!client) {
32+
throw new Error('No client');
33+
}
34+
return client.getMachineStatus();
3135
},
3236
});
3337
})
@@ -38,7 +42,6 @@ export const provideMachineStatusContext = (refetchInterval: () => number) => {
3842
queries: toStore(() => options),
3943
combine: (results) => {
4044
const partIDs = Object.keys(clients.current);
41-
4245
return Object.fromEntries(
4346
results.map((result, index) => [partIDs[index], result])
4447
);

src/lib/hooks/resource-names.svelte.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
createQueries,
3+
queryOptions,
34
type QueryObserverResult,
45
} from '@tanstack/svelte-query';
56
import type { ResourceName } from '@viamrobotics/sdk';
@@ -33,13 +34,16 @@ export const provideResourceNamesContext = () => {
3334
const revision =
3435
machineStatuses.current[partID]?.data?.config?.revision ?? '';
3536

36-
return {
37+
return queryOptions({
38+
enabled: client !== undefined,
3739
queryKey: ['partID', partID, 'robotClient', 'resourceNames', revision],
3840
queryFn: async () => {
39-
if (!client) return [];
41+
if (!client) {
42+
throw new Error('No client');
43+
}
4044
return client.resourceNames();
4145
},
42-
};
46+
});
4347
})
4448
);
4549

0 commit comments

Comments
 (0)