Skip to content

Commit 175e21a

Browse files
Merge pull request #67 from viamrobotics/poll-1
Don't poll when disabled, include errors in hooks
2 parents eef2e3f + cfcf5b2 commit 175e21a

12 files changed

+86
-29
lines changed

.changeset/mean-crews-tickle.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+
Don't poll when disabled, include errors in hooks

src/lib/hooks/app/create-app-query.svelte.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export const createAppQuery = <T extends AppClient, K extends keyof T>(
4242
);
4343
const _args = $derived(typeof args === 'function' ? args() : args);
4444
const methodName = $derived(String(method));
45+
const enabled = $derived(
46+
appClient !== undefined && _options?.enabled !== false
47+
);
4548

4649
const queryOptions = $derived(
4750
createQueryOptions({
@@ -51,7 +54,7 @@ export const createAppQuery = <T extends AppClient, K extends keyof T>(
5154
methodName,
5255
...(_args ? [_args] : []),
5356
],
54-
enabled: appClient !== undefined && _options?.enabled !== false,
57+
enabled,
5558
queryFn: async () => {
5659
if (!appClient) {
5760
throw new Error('appClient is undefined');
@@ -88,7 +91,7 @@ export const createAppQuery = <T extends AppClient, K extends keyof T>(
8891

8992
usePolling(
9093
() => queryOptions.queryKey,
91-
() => _options?.refetchInterval ?? false
94+
() => enabled && (_options?.refetchInterval ?? false)
9295
);
9396

9497
return fromStore(createQuery(toStore(() => queryOptions)));

src/lib/hooks/app/create-data-query.svelte.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export const createDataQuery = <T extends DataClient, K extends keyof T>(
4242
);
4343
const _args = $derived(typeof args === 'function' ? args() : args);
4444
const methodName = $derived(String(method));
45+
const enabled = $derived(
46+
dataClient !== undefined && _options?.enabled !== false
47+
);
4548

4649
const queryOptions = $derived(
4750
createQueryOptions({
@@ -51,7 +54,7 @@ export const createDataQuery = <T extends DataClient, K extends keyof T>(
5154
methodName,
5255
...(_args ? [_args] : []),
5356
],
54-
enabled: dataClient !== undefined && _options?.enabled !== false,
57+
enabled,
5558
queryFn: async () => {
5659
if (!dataClient) {
5760
throw new Error('dataClient is undefined');
@@ -88,7 +91,7 @@ export const createDataQuery = <T extends DataClient, K extends keyof T>(
8891

8992
usePolling(
9093
() => queryOptions.queryKey,
91-
() => _options?.refetchInterval ?? false
94+
() => enabled && (_options?.refetchInterval ?? false)
9295
);
9396

9497
return fromStore(createQuery(toStore(() => queryOptions)));

src/lib/hooks/create-resource-query.svelte.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
5252
const _args = $derived(typeof args === 'function' ? args() : args);
5353
const name = $derived(client.current?.name);
5454
const methodName = $derived(String(method));
55+
const enabled = $derived(
56+
client.current !== undefined &&
57+
_options?.enabled !== false &&
58+
enabledQueries.resourceQueries
59+
);
5560

5661
const queryOptions = $derived(
5762
createQueryOptions({
@@ -64,10 +69,7 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
6469
methodName,
6570
...(_args ? [_args] : []),
6671
],
67-
enabled:
68-
client.current !== undefined &&
69-
_options?.enabled !== false &&
70-
enabledQueries.resourceQueries,
72+
enabled,
7173
retry: false,
7274
queryFn: async () => {
7375
const clientFunc = client.current?.[method];
@@ -101,7 +103,7 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
101103

102104
usePolling(
103105
() => queryOptions.queryKey,
104-
() => _options?.refetchInterval ?? false
106+
() => enabled && (_options?.refetchInterval ?? false)
105107
);
106108

107109
return fromStore(createQuery(toStore(() => queryOptions)));

src/lib/hooks/create-robot-query.svelte.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
5151
);
5252
const _args = $derived(typeof args === 'function' ? args() : args);
5353
const methodName = $derived(String(method));
54+
const enabled = $derived(
55+
client.current !== undefined &&
56+
_options?.enabled !== false &&
57+
enabledQueries.robotQueries
58+
);
5459

5560
const queryOptions = $derived(
5661
createQueryOptions({
@@ -62,10 +67,7 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
6267
methodName,
6368
...(_args ? [_args] : []),
6469
],
65-
enabled:
66-
client.current !== undefined &&
67-
_options?.enabled !== false &&
68-
enabledQueries.robotQueries,
70+
enabled,
6971
retry: false,
7072
queryFn: async () => {
7173
const clientFunc = client.current?.[method];
@@ -99,7 +101,7 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
99101

100102
usePolling(
101103
() => queryOptions.queryKey,
102-
() => _options?.refetchInterval ?? false
104+
() => enabled && (_options?.refetchInterval ?? false)
103105
);
104106

105107
return fromStore(createQuery(toStore(() => queryOptions)));

src/lib/hooks/create-stream-client.svelte.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const createStreamClient = (
1717
const debug = useQueryLogger();
1818
const enabledQueries = useEnabledQueries();
1919
let mediaStream = $state.raw<MediaStream | null>(null);
20+
let error = $state.raw<Error>();
2021

2122
const client = useRobotClient(partID);
2223
const streamClient = $derived(
@@ -42,7 +43,12 @@ export const createStreamClient = (
4243
$effect(() => {
4344
const name = resourceName();
4445
const client = streamClient;
45-
client?.getStream(name);
46+
try {
47+
client?.getStream(name);
48+
error = undefined;
49+
} catch (nextError) {
50+
error = nextError as Error;
51+
}
4652
return () => client?.remove(name);
4753
});
4854

@@ -126,6 +132,9 @@ export const createStreamClient = (
126132
get current() {
127133
return streamClient;
128134
},
135+
get error() {
136+
return error;
137+
},
129138
get mediaStream() {
130139
return mediaStream;
131140
},

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ export const provideMachineStatusContext = (refetchInterval: () => number) => {
7373

7474
$effect(() => {
7575
for (const option of options) {
76-
usePolling(() => option.queryKey, refetchInterval);
76+
usePolling(
77+
() => option.queryKey,
78+
() => (option.enabled ? refetchInterval() : false)
79+
);
7780
}
7881
});
7982

src/lib/hooks/robot-clients.svelte.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const dialKey = Symbol('dial-configs-context');
1515

1616
interface ClientContext {
1717
current: Record<PartID, Client | undefined>;
18+
errors: Record<PartID, Error | undefined>;
1819
}
1920

2021
interface ConnectionStatusContext {
@@ -30,6 +31,7 @@ export const provideRobotClientsContext = (
3031
) => {
3132
const queryClient = useQueryClient();
3233
const clients = $state<Record<PartID, Client | undefined>>({});
34+
const errors = $state<Record<PartID, Error | undefined>>({});
3335
const connectionStatus = $state<Record<PartID, MachineConnectionEvent>>({});
3436

3537
let lastConfigs: Record<PartID, DialConf | undefined> = {};
@@ -88,10 +90,11 @@ export const provideRobotClientsContext = (
8890
});
8991

9092
await client.dial(config);
93+
errors[partID] = undefined;
9194

9295
connectionStatus[partID] = MachineConnectionEvent.CONNECTED;
9396
} catch (error) {
94-
console.error(error);
97+
errors[partID] = error as Error;
9598
connectionStatus[partID] = MachineConnectionEvent.DISCONNECTED;
9699
}
97100
};
@@ -139,6 +142,9 @@ export const provideRobotClientsContext = (
139142
get current() {
140143
return clients;
141144
},
145+
get errors() {
146+
return errors;
147+
},
142148
});
143149

144150
setContext<ConnectionStatusContext>(connectionKey, {

src/lib/hooks/use-polling.svelte.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function usePolling(
1414
) {
1515
const queryClient = useQueryClient();
1616
let timeoutId: ReturnType<typeof setTimeout>;
17+
let active = true;
1718

1819
$effect(() => {
1920
const key = queryKey();
@@ -23,13 +24,20 @@ export function usePolling(
2324
return;
2425
}
2526

27+
active = true;
28+
2629
const poll = async () => {
30+
if (!active) return;
31+
2732
await queryClient.refetchQueries({ queryKey: key });
2833
timeoutId = setTimeout(poll, currentInterval);
2934
};
3035

3136
timeoutId = setTimeout(poll, currentInterval);
3237

33-
return () => clearTimeout(timeoutId);
38+
return () => {
39+
clearTimeout(timeoutId);
40+
active = false;
41+
};
3442
});
3543
}

src/routes/+layout.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ let { children }: Props = $props();
4343
{/each}
4444
</div>
4545

46-
<ViamProvider {dialConfigs}>
46+
<ViamProvider
47+
{dialConfigs}
48+
logQueries
49+
>
4750
<Parts />
4851
{@render children()}
4952
<SvelteQueryDevtools />

0 commit comments

Comments
 (0)