11import {
22 type Client ,
3- createRobotClient ,
43 type DialConf ,
54 MachineConnectionEvent ,
6- type RobotClient ,
5+ RobotClient ,
76} from '@viamrobotics/sdk' ;
87import { getContext , onMount , setContext } from 'svelte' ;
98import { useQueryClient } from '@tanstack/svelte-query' ;
@@ -35,12 +34,7 @@ export const provideRobotClientsContext = (
3534
3635 let lastConfigs : Record < PartID , DialConf | undefined > = { } ;
3736
38- const disconnect = async ( partID : PartID , config ?: DialConf ) => {
39- // If currently making the initial connection, abort it.
40- if ( config ?. reconnectAbortSignal !== undefined ) {
41- config . reconnectAbortSignal . abort = true ;
42- }
43-
37+ const disconnect = async ( partID : PartID ) => {
4438 const client = clients [ partID ] ;
4539
4640 if ( ! client ) {
@@ -65,22 +59,18 @@ export const provideRobotClientsContext = (
6559 connectionStatus [ partID ] ??= MachineConnectionEvent . DISCONNECTED ;
6660
6761 try {
68- await disconnect ( partID , config ) ;
69-
70- connectionStatus [ partID ] = MachineConnectionEvent . CONNECTING ;
71-
72- // Reset the abort signal if it exists
73- if ( config . reconnectAbortSignal !== undefined ) {
74- config . reconnectAbortSignal = {
75- abort : false ,
76- } ;
77- }
62+ await disconnect ( partID ) ;
7863
7964 config . reconnectMaxAttempts ??= 1e9 ;
8065 config . reconnectMaxWait ??= 1000 ;
8166
82- const client = await createRobotClient ( config ) ;
67+ const client = new RobotClient ( ) ;
8368 ( client as RobotClient & { partID : string } ) . partID = partID ;
69+
70+ clients [ partID ] = client ;
71+
72+ connectionStatus [ partID ] = MachineConnectionEvent . CONNECTING ;
73+
8474 client . on ( 'connectionstatechange' , async ( event ) => {
8575 connectionStatus [ partID ] = (
8676 event as { eventType : MachineConnectionEvent }
@@ -97,7 +87,8 @@ export const provideRobotClientsContext = (
9787 }
9888 } ) ;
9989
100- clients [ partID ] = client ;
90+ await client . dial ( config ) ;
91+
10192 connectionStatus [ partID ] = MachineConnectionEvent . CONNECTED ;
10293 } catch ( error ) {
10394 console . error ( error ) ;
@@ -113,10 +104,8 @@ export const provideRobotClientsContext = (
113104 Object . keys ( lastConfigs )
114105 ) ;
115106
116- lastConfigs = $state . snapshot ( configs ) ;
117-
118107 for ( const partID of removed ) {
119- disconnect ( partID , lastConfigs [ partID ] ) ;
108+ disconnect ( partID ) ;
120109 }
121110
122111 for ( const partID of added ) {
@@ -134,6 +123,8 @@ export const provideRobotClientsContext = (
134123 connect ( partID , config ) ;
135124 }
136125 }
126+
127+ lastConfigs = $state . snapshot ( configs ) ;
137128 } ) ;
138129
139130 onMount ( ( ) => {
0 commit comments