Skip to content

Commit 0902e78

Browse files
Merge pull request #20 from viamrobotics/fix-disconnect
Fix disconnect issue
2 parents 7317e44 + a698060 commit 0902e78

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

.changeset/wise-roses-tan.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+
Fix disconnect issue

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
MachineConnectionEvent,
66
type RobotClient,
77
} from '@viamrobotics/sdk';
8-
import { getContext, setContext } from 'svelte';
8+
import { getContext, onMount, setContext } from 'svelte';
99
import { useQueryClient } from '@tanstack/svelte-query';
1010
import type { PartID } from '../part';
1111
import { comparePartIds, isJsonEqual } from '../compare';
@@ -90,37 +90,40 @@ export const provideRobotClientsContext = (
9090
}
9191
};
9292

93-
$effect.pre(() => {
93+
$effect(() => {
9494
const configs = dialConfigs();
9595

9696
const { added, removed, unchanged } = comparePartIds(
9797
Object.keys(configs),
9898
Object.keys(lastConfigs)
9999
);
100100

101+
for (const partID of removed) {
102+
disconnect(partID, lastConfigs[partID]);
103+
}
104+
101105
for (const partID of added) {
102106
const config = configs[partID];
103107
if (config) {
104108
connect(partID, config);
105109
}
106110
}
107111

108-
for (const partID of removed) {
109-
disconnect(partID, lastConfigs[partID]);
110-
}
111-
112112
for (const partID of unchanged) {
113113
const config = configs[partID];
114114
const lastConfig = lastConfigs[partID];
115+
115116
if (config && lastConfig && !isJsonEqual(lastConfig, config)) {
116117
connect(partID, config);
117118
}
118119
}
119120

120121
lastConfigs = configs;
122+
});
121123

124+
onMount(() => {
122125
return () => {
123-
for (const partID of Object.keys(configs)) {
126+
for (const partID of Object.keys(dialConfigs())) {
124127
clients[partID]?.disconnect();
125128
}
126129
};

src/routes/+layout.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import '../app.css';
33
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools';
44
import { ViamProvider } from '$lib';
55
import type { Snippet } from 'svelte';
6-
import { dialConfigs } from './configs';
6+
import { dialConfigs as configs } from './configs';
77
import Parts from './components/parts.svelte';
88
99
interface Props {
1010
children: Snippet;
1111
}
1212
13+
let dialConfigs = $state(configs);
14+
15+
$effect(() => {
16+
const id = setInterval(() => (dialConfigs = { ...dialConfigs }), 3000);
17+
return () => clearInterval(id);
18+
});
19+
1320
let { children }: Props = $props();
1421
</script>
1522

0 commit comments

Comments
 (0)