Skip to content

Commit 8fd3680

Browse files
committed
don't poll when disabled, better error handling
1 parent 4620d0a commit 8fd3680

File tree

10 files changed

+69
-19
lines changed

10 files changed

+69
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const createAppQuery = <T extends AppClient, K extends keyof T>(
8888

8989
usePolling(
9090
() => queryOptions.queryKey,
91-
() => _options?.refetchInterval ?? false
91+
() => (queryOptions.enabled ? _options?.refetchInterval : false)
9292
);
9393

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const createDataQuery = <T extends DataClient, K extends keyof T>(
8888

8989
usePolling(
9090
() => queryOptions.queryKey,
91-
() => _options?.refetchInterval ?? false
91+
() => (queryOptions.enabled ? _options?.refetchInterval : false)
9292
);
9393

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
9696

9797
usePolling(
9898
() => queryOptions.queryKey,
99-
() => _options?.refetchInterval ?? false
99+
() => (queryOptions.enabled ? _options?.refetchInterval : false)
100100
);
101101

102102
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
@@ -15,6 +15,7 @@ export const createStreamClient = (
1515
) => {
1616
const debug = useQueryLogger();
1717
let mediaStream = $state.raw<MediaStream | null>(null);
18+
let error = $state<Error>();
1819

1920
const client = useRobotClient(partID);
2021
const streamClient = $derived(
@@ -40,7 +41,12 @@ export const createStreamClient = (
4041
$effect(() => {
4142
const name = resourceName();
4243
const client = streamClient;
43-
client?.getStream(name);
44+
try {
45+
client?.getStream(name);
46+
error = undefined;
47+
} catch (nextError) {
48+
error = nextError as Error;
49+
}
4450
return () => client?.remove(name);
4551
});
4652

@@ -124,6 +130,9 @@ export const createStreamClient = (
124130
get current() {
125131
return streamClient;
126132
},
133+
get error() {
134+
return error;
135+
},
127136
get mediaStream() {
128137
return mediaStream;
129138
},

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

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

7272
$effect(() => {
7373
for (const option of options) {
74-
usePolling(() => option.queryKey, refetchInterval);
74+
usePolling(
75+
() => option.queryKey,
76+
() => (option.enabled ? refetchInterval() : false)
77+
);
7578
}
7679
});
7780

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

Lines changed: 12 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> = {};
@@ -87,7 +89,12 @@ export const provideRobotClientsContext = (
8789
}
8890
});
8991

90-
await client.dial(config);
92+
try {
93+
await client.dial(config);
94+
errors[partID] = undefined;
95+
} catch (nextError) {
96+
errors[partID] = nextError as Error;
97+
}
9198

9299
connectionStatus[partID] = MachineConnectionEvent.CONNECTED;
93100
} catch (error) {
@@ -98,6 +105,7 @@ export const provideRobotClientsContext = (
98105

99106
$effect(() => {
100107
const configs = dialConfigs();
108+
console.log(configs);
101109

102110
const { added, removed, unchanged } = comparePartIds(
103111
Object.keys(configs),
@@ -139,6 +147,9 @@ export const provideRobotClientsContext = (
139147
get current() {
140148
return clients;
141149
},
150+
get errors() {
151+
return errors;
152+
},
142153
});
143154

144155
setContext<ConnectionStatusContext>(connectionKey, {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { useQueryClient } from '@tanstack/svelte-query';
1010
*/
1111
export function usePolling(
1212
queryKey: () => unknown[],
13-
interval: () => number | false
13+
interval: () => number | false | undefined
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 />

src/routes/components/part.svelte

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
import { useResourceNames } from '$lib';
3+
import Base from './base.svelte';
4+
5+
let { partID } = $props();
6+
7+
const bases = useResourceNames(() => partID, 'base');
8+
$inspect(bases);
9+
</script>
10+
11+
{#each bases.current as base}
12+
<Base
13+
name={base.name}
14+
{partID}
15+
/>
16+
{/each}

src/routes/components/parts.svelte

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import {
77
CameraStream,
88
} from '$lib';
99
import { dialConfigs } from '../configs';
10-
import Base from './base.svelte';
10+
import Part from './part.svelte';
1111
import Version from './version.svelte';
1212
1313
const partIDs = Object.keys(dialConfigs);
1414
1515
let partID = new PersistedState('selected-partID', partIDs[0] ?? '');
1616
17+
$inspect(partID.current);
1718
const status = useConnectionStatus(() => partID.current);
1819
const resources = useResourceNames(() => partID.current);
1920
const cameras = useResourceNames(() => partID.current, 'camera');
20-
const bases = useResourceNames(() => partID.current, 'base');
2121
2222
let streaming = true;
2323
</script>
@@ -28,10 +28,17 @@ let streaming = true;
2828
<button
2929
class="border p-2"
3030
class:bg-blue-100={partID.current === id}
31-
onclick={() => (partID.current = id)}
31+
onclick={() => {
32+
console.log('click');
33+
partID.current = id;
34+
}}
3235
>
3336
part {index + 1}
3437
</button>
38+
39+
{#if id === partID.current}
40+
<Part partID={id} />
41+
{/if}
3542
{/each}
3643

3744
{status.current}
@@ -54,13 +61,6 @@ let streaming = true;
5461
</ul>
5562
{/if}
5663

57-
{#each bases.current as { name } (name)}
58-
<Base
59-
{name}
60-
partID={partID.current}
61-
/>
62-
{/each}
63-
6464
{#each cameras.current as { name } (name)}
6565
{#if streaming}
6666
<CameraStream

0 commit comments

Comments
 (0)