|
5 | 5 | MachineConnectionEvent, |
6 | 6 | type RobotClient, |
7 | 7 | } from '@viamrobotics/sdk'; |
8 | | -import { getContext, setContext } from 'svelte'; |
| 8 | +import { getContext, onMount, setContext } from 'svelte'; |
9 | 9 | import { useQueryClient } from '@tanstack/svelte-query'; |
10 | 10 | import type { PartID } from '../part'; |
11 | 11 | import { comparePartIds, isJsonEqual } from '../compare'; |
@@ -90,37 +90,41 @@ export const provideRobotClientsContext = ( |
90 | 90 | } |
91 | 91 | }; |
92 | 92 |
|
93 | | - $effect.pre(() => { |
| 93 | + $effect(() => { |
94 | 94 | const configs = dialConfigs(); |
95 | 95 |
|
96 | 96 | const { added, removed, unchanged } = comparePartIds( |
97 | 97 | Object.keys(configs), |
98 | 98 | Object.keys(lastConfigs) |
99 | 99 | ); |
100 | 100 |
|
| 101 | + for (const partID of removed) { |
| 102 | + disconnect(partID, lastConfigs[partID]); |
| 103 | + } |
| 104 | + |
101 | 105 | for (const partID of added) { |
102 | 106 | const config = configs[partID]; |
103 | 107 | if (config) { |
104 | 108 | connect(partID, config); |
105 | 109 | } |
106 | 110 | } |
107 | 111 |
|
108 | | - for (const partID of removed) { |
109 | | - disconnect(partID, lastConfigs[partID]); |
110 | | - } |
111 | | - |
112 | 112 | for (const partID of unchanged) { |
113 | 113 | const config = configs[partID]; |
114 | 114 | const lastConfig = lastConfigs[partID]; |
| 115 | + |
115 | 116 | if (config && lastConfig && !isJsonEqual(lastConfig, config)) { |
| 117 | + console.log('hi', partID); |
116 | 118 | connect(partID, config); |
117 | 119 | } |
118 | 120 | } |
119 | 121 |
|
120 | 122 | lastConfigs = configs; |
| 123 | + }); |
121 | 124 |
|
| 125 | + onMount(() => { |
122 | 126 | return () => { |
123 | | - for (const partID of Object.keys(configs)) { |
| 127 | + for (const partID of Object.keys(dialConfigs())) { |
124 | 128 | clients[partID]?.disconnect(); |
125 | 129 | } |
126 | 130 | }; |
|
0 commit comments