diff --git a/examples/vanilla/package-lock.json b/examples/vanilla/package-lock.json index b432016f4..b12d8c48b 100644 --- a/examples/vanilla/package-lock.json +++ b/examples/vanilla/package-lock.json @@ -19,7 +19,7 @@ }, "../..": { "name": "@viamrobotics/sdk", - "version": "0.16.0", + "version": "0.19.1", "license": "Apache-2.0", "dependencies": { "@viamrobotics/rpc": "^0.2.5", diff --git a/examples/vanilla/src/main.ts b/examples/vanilla/src/main.ts index 89d06dcef..618cd16d8 100644 --- a/examples/vanilla/src/main.ts +++ b/examples/vanilla/src/main.ts @@ -12,6 +12,7 @@ const disconnectEl = document.getElementById('disconnect'); const resourcesEl = document.getElementById('resources'); let machine: VIAM.RobotClient | undefined = undefined; +const reconnectAbortSignal = { abort: false }; const handleConnectionStateChange = (event: unknown) => { updateConnectionStatus( @@ -42,6 +43,7 @@ const connect = async () => { return; } + reconnectAbortSignal.abort = false; updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTING); try { @@ -53,6 +55,7 @@ const connect = async () => { }, authEntity: API_KEY_ID, signalingAddress: 'https://app.viam.com:443', + reconnectAbortSignal, }); updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTED); machine.on('connectionstatechange', handleConnectionStateChange); @@ -62,6 +65,9 @@ const connect = async () => { }; const disconnect = async () => { + // If currently establishing initial connection, abort. + reconnectAbortSignal.abort = true; + if (!machine) { return; } diff --git a/src/robot/dial.ts b/src/robot/dial.ts index 09d3349ac..5d1a7c6a2 100644 --- a/src/robot/dial.ts +++ b/src/robot/dial.ts @@ -16,6 +16,7 @@ export interface DialDirectConf { noReconnect?: boolean; reconnectMaxAttempts?: number; reconnectMaxWait?: number; + reconnectAbortSignal?: { abort: boolean }; // set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT, // and a value of 0 would disable the timeout. dialTimeout?: number; @@ -88,6 +89,7 @@ export interface DialWebRTCConf { noReconnect?: boolean; reconnectMaxAttempts?: number; reconnectMaxWait?: number; + reconnectAbortSignal?: { abort: boolean }; // WebRTC signalingAddress: string; iceServers?: ICEServer[]; @@ -178,13 +180,14 @@ export const createRobotClient = async ( retry: (_error, attemptNumber) => { // eslint-disable-next-line no-console console.debug(`Failed to connect, attempt ${attemptNumber} with backoff`); - // Always retry the next attempt - return true; + + // Abort reconnects if the the caller specifies, otherwise retry + return !conf.reconnectAbortSignal?.abort; }, }; // Try to dial via WebRTC first. - if (isDialWebRTCConf(conf)) { + if (isDialWebRTCConf(conf) && !conf.reconnectAbortSignal?.abort) { try { return conf.noReconnect ? await dialWebRTC(conf) @@ -195,13 +198,15 @@ export const createRobotClient = async ( } } - try { - return conf.noReconnect - ? await dialDirect(conf) - : await backOff(async () => dialDirect(conf), backOffOpts); - } catch { - // eslint-disable-next-line no-console - console.debug('Failed to connect via gRPC'); + if (!conf.reconnectAbortSignal?.abort) { + try { + return conf.noReconnect + ? await dialDirect(conf) + : await backOff(async () => dialDirect(conf), backOffOpts); + } catch { + // eslint-disable-next-line no-console + console.debug('Failed to connect via gRPC'); + } } throw new Error('Failed to connect to robot');