Skip to content

Commit

Permalink
Set serial session as undefined and disconnect delegate before reconn…
Browse files Browse the repository at this point in the history
…ect so that software reset does not throw
  • Loading branch information
microbit-grace committed Aug 5, 2024
1 parent b0d390d commit 3364066
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface MicrobitRadioBridgeConnectionOptions {
interface ConnectCallbacks {
onConnecting: () => void;
onReconnecting: () => void;
onPrepareFinalReconnectAttempt: () => void;
onFail: () => void;
onSuccess: () => void;
}
Expand Down Expand Up @@ -139,10 +140,16 @@ export class MicrobitRadioBridgeConnection
this.setStatus(ConnectionStatus.RECONNECTING);
}
},
onPrepareFinalReconnectAttempt: () => {
// So that serial session does not get repetitively disposed in
// delegate status listener when delegate is disconnected for reconnection
this.serialSession = undefined;
},
onFail: () => {
if (this.status !== ConnectionStatus.DISCONNECTED) {
this.setStatus(ConnectionStatus.DISCONNECTED);
}
this.serialSession = undefined;
this.serialSessionOpen = false;
},
onSuccess: () => {
Expand Down Expand Up @@ -354,11 +361,7 @@ class RadioBridgeSerialSession {
this.responseMap.clear();
this.delegate.removeEventListener("serialdata", this.serialDataListener);
this.delegate.removeEventListener("serialerror", this.serialErrorListener);
try {
await this.delegate.softwareReset();
} catch (e) {
// If this fails, the bridge micro:bit has already been reset.
}
await this.delegate.softwareReset();
}

private async sendCmdWaitResponse(
Expand Down Expand Up @@ -407,6 +410,8 @@ class RadioBridgeSerialSession {
type: "Serial",
message: "Serial connection lost...final attempt to reconnect",
});
this.callbacks.onPrepareFinalReconnectAttempt();
await this.delegate.disconnect();
await this.dispose();
await this.delegate.connect();
await this.connect();
Expand Down

0 comments on commit 3364066

Please sign in to comment.