Skip to content

Commit bbbcea0

Browse files
authored
Merge pull request #66 from viamrobotics/RSDK-12206-update-machine-status-hook
RSDK-12206 Update machine status hook
2 parents 175e21a + cb37189 commit bbbcea0

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

.changeset/red-dogs-move.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+
Update machine status hook to include pending field and sort resource statuses by name.

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

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,79 @@ import {
77
} from '@tanstack/svelte-query';
88
import { getContext, setContext } from 'svelte';
99
import type { PartID } from '$lib/part';
10-
import type { RobotClient } from '@viamrobotics/sdk';
10+
import type { PlainMessage, robotApi } from '@viamrobotics/sdk';
1111
import { usePolling } from './use-polling.svelte';
1212
import { useQueryLogger } from '$lib/query-logger';
1313
import { useEnabledQueries } from './use-enabled-queries.svelte';
1414

1515
const key = Symbol('machine-status-context');
1616

17-
type MachineStatus = Awaited<ReturnType<RobotClient['getMachineStatus']>>;
17+
type MachineStatus = PlainMessage<robotApi.GetMachineStatusResponse>;
1818
type Query = QueryObserverResult<MachineStatus, Error>;
1919

20+
// TODO: move to ts-sdk
21+
export type ResourceStatus = PlainMessage<robotApi.ResourceStatus>;
22+
2023
interface Context {
2124
current: Record<PartID, Query | undefined>;
2225
}
2326

27+
/**
28+
* sorts machine status resources by local/remote -> type -> name (alphabetical)
29+
* to produce a list like:
30+
*
31+
* component a
32+
* component z
33+
* service b
34+
* component remote:c
35+
* service remote:b
36+
* @param machineStatus
37+
*/
38+
const sortResourceStatuses = (machineStatus: MachineStatus) => {
39+
const resources = machineStatus.resources.toSorted(
40+
({ name }, { name: otherName }) => {
41+
if (name === undefined && otherName === undefined) {
42+
return 0;
43+
}
44+
45+
if (name === undefined) {
46+
return -1;
47+
}
48+
49+
if (otherName === undefined) {
50+
return 1;
51+
}
52+
53+
const { name: aName, type: aType, subtype: aSubtype } = name;
54+
const { name: bName, type: bType, subtype: bSubtype } = otherName;
55+
56+
// sort all non-remote resources before remote resources
57+
if (aName.includes(':') !== bName.includes(':')) {
58+
return aName.includes(':') ? 1 : -1;
59+
}
60+
61+
if (aName === bName && aType === bType) {
62+
return aSubtype.localeCompare(bSubtype);
63+
}
64+
65+
if (aName === bName) {
66+
return aType.localeCompare(bType);
67+
}
68+
69+
// sort alphabetically within type
70+
// sort components before services
71+
return aType === bType
72+
? aName.localeCompare(bName)
73+
: aType.localeCompare(bType);
74+
}
75+
);
76+
77+
return {
78+
...machineStatus,
79+
resources,
80+
};
81+
};
82+
2483
export const provideMachineStatusContext = (refetchInterval: () => number) => {
2584
const clients = useRobotClients();
2685
const debug = useQueryLogger();
@@ -49,7 +108,7 @@ export const provideMachineStatusContext = (refetchInterval: () => number) => {
49108
try {
50109
const response = await client.getMachineStatus();
51110
logger('RES', 'robot', 'getMachineStatus', response);
52-
return response;
111+
return sortResourceStatuses(response);
53112
} catch (error) {
54113
logger('ERR', 'robot', 'getMachineStatus', error);
55114
throw error;
@@ -99,6 +158,7 @@ export const useMachineStatus = (partID: () => PartID) => {
99158
const error = $derived(query?.error);
100159
const fetching = $derived(query?.isFetching);
101160
const loading = $derived(query?.isLoading);
161+
const pending = $derived(query?.isPending ?? true);
102162

103163
return {
104164
get current() {
@@ -113,5 +173,11 @@ export const useMachineStatus = (partID: () => PartID) => {
113173
get loading() {
114174
return loading;
115175
},
176+
get pending() {
177+
return pending;
178+
},
179+
refetch() {
180+
return query?.refetch() ?? Promise.resolve();
181+
},
116182
};
117183
};

src/lib/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export {
2222
} from './hooks/create-resource-stream.svelte';
2323
export { createStreamClient } from './hooks/create-stream-client.svelte';
2424

25-
export { useMachineStatus } from './hooks/machine-status.svelte';
25+
export {
26+
useMachineStatus,
27+
type ResourceStatus,
28+
} from './hooks/machine-status.svelte';
2629
export { useResourceNames } from './hooks/resource-names.svelte';
2730

2831
export { usePolling } from './hooks/use-polling.svelte';

0 commit comments

Comments
 (0)