Skip to content

Commit 4ca22ce

Browse files
authored
Add closed bool to prevent reconnection (#361)
1 parent 6fe5765 commit 4ca22ce

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

examples/vanilla/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/vanilla/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const connect = async () => {
5252
credential: {
5353
type: 'api-key',
5454
payload: API_KEY,
55+
authEntity: API_KEY_ID,
5556
},
5657
authEntity: API_KEY_ID,
5758
signalingAddress: 'https://app.viam.com:443',

src/robot/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export class RobotClient extends EventDispatcher implements Robot {
9999

100100
private savedCreds: Credentials | undefined;
101101

102+
private closed: boolean;
103+
102104
private robotServiceClient: RobotServiceClient | undefined;
103105

104106
private armServiceClient: ArmServiceClient | undefined;
@@ -182,6 +184,8 @@ export class RobotClient extends EventDispatcher implements Robot {
182184
this.emit('connectionstatechange', { eventType });
183185
});
184186
}
187+
188+
this.closed = false;
185189
}
186190

187191
private onDisconnect(event?: Event) {
@@ -191,6 +195,10 @@ export class RobotClient extends EventDispatcher implements Robot {
191195
return;
192196
}
193197

198+
if (this.closed) {
199+
return;
200+
}
201+
194202
// eslint-disable-next-line no-console
195203
console.debug('Connection closed, will try to reconnect');
196204
void backOff(async () => this.connect(), {
@@ -395,6 +403,7 @@ export class RobotClient extends EventDispatcher implements Robot {
395403
this.peerConn = undefined;
396404
}
397405
this.sessionManager.reset();
406+
this.closed = true;
398407
this.emit(MachineConnectionEvent.DISCONNECTED, {});
399408
}
400409

@@ -411,6 +420,7 @@ export class RobotClient extends EventDispatcher implements Robot {
411420
dialTimeout,
412421
}: ConnectOptions = {}) {
413422
this.emit(MachineConnectionEvent.CONNECTING, {});
423+
this.closed = false;
414424

415425
if (this.connecting) {
416426
// This lint is clearly wrong due to how the event loop works such that after an await, the condition may no longer be true.

0 commit comments

Comments
 (0)