Skip to content

Commit

Permalink
Use ignore delegate status
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-grace committed Aug 5, 2024
1 parent 3364066 commit e6fe478
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export class MicrobitRadioBridgeConnection
private remoteDeviceId: number | undefined;
private disconnectPromise: Promise<void> | undefined;
private serialSessionOpen = false;
private ignoreDelegateStatus = false;

private delegateStatusListener = (e: ConnectionStatusEvent) => {
const currentStatus = this.status;
if (this.ignoreDelegateStatus) {
return;
}
if (e.status !== ConnectionStatus.CONNECTED) {
this.setStatus(e.status);
this.serialSession?.dispose();
Expand Down Expand Up @@ -109,6 +113,7 @@ export class MicrobitRadioBridgeConnection
}

async connect(): Promise<ConnectionStatus> {
this.ignoreDelegateStatus = false;
if (this.disconnectPromise) {
await this.disconnectPromise;
}
Expand Down Expand Up @@ -143,19 +148,20 @@ export class MicrobitRadioBridgeConnection
onPrepareFinalReconnectAttempt: () => {
// So that serial session does not get repetitively disposed in
// delegate status listener when delegate is disconnected for reconnection
this.serialSession = undefined;
this.ignoreDelegateStatus = true;
},
onFail: () => {
if (this.status !== ConnectionStatus.DISCONNECTED) {
this.setStatus(ConnectionStatus.DISCONNECTED);
}
this.serialSession = undefined;
this.ignoreDelegateStatus = false;
this.serialSessionOpen = false;
},
onSuccess: () => {
if (this.status !== ConnectionStatus.CONNECTED) {
this.setStatus(ConnectionStatus.CONNECTED);
}
this.ignoreDelegateStatus = false;
this.serialSessionOpen = true;
},
},
Expand Down Expand Up @@ -185,8 +191,8 @@ export class MicrobitRadioBridgeConnection
}
this.disconnectPromise = (async () => {
this.serialSessionOpen = false;
await this.delegate.disconnect();
await this.serialSession?.dispose();
await this.serialSession?.dispose(true);
this.ignoreDelegateStatus = true;
this.disconnectPromise = undefined;
})();
}
Expand Down Expand Up @@ -351,7 +357,7 @@ class RadioBridgeSerialSession {
}
}

async dispose() {
async dispose(disconnect: boolean = false) {
this.stopConnectionCheck();
try {
await this.sendCmdWaitResponse(protocol.generateCmdStop());
Expand All @@ -361,6 +367,9 @@ class RadioBridgeSerialSession {
this.responseMap.clear();
this.delegate.removeEventListener("serialdata", this.serialDataListener);
this.delegate.removeEventListener("serialerror", this.serialErrorListener);
if (disconnect) {
await this.delegate.disconnect();
}
await this.delegate.softwareReset();
}

Expand Down Expand Up @@ -411,8 +420,7 @@ class RadioBridgeSerialSession {
message: "Serial connection lost...final attempt to reconnect",
});
this.callbacks.onPrepareFinalReconnectAttempt();
await this.delegate.disconnect();
await this.dispose();
await this.dispose(true);
await this.delegate.connect();
await this.connect();
}
Expand Down

0 comments on commit e6fe478

Please sign in to comment.