Skip to content

Commit

Permalink
More robust radio bridge connection
Browse files Browse the repository at this point in the history
Only reconnect serial session on tab change if consumer is already
listening.
  • Loading branch information
microbit-robert committed Jul 25, 2024
1 parent b2b2c0a commit 6f80305
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 24 additions & 4 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class MicrobitRadioBridgeConnection
private logging: Logging;
private serialSession: RadioBridgeSerialSession | undefined;
private remoteDeviceId: number | undefined;
private disconnectPromise: Promise<void> | undefined;
private serialSessionOpen = false;

private delegateStatusListner = (e: ConnectionStatusEvent) => {
const currentStatus = this.status;
Expand All @@ -53,7 +55,11 @@ export class MicrobitRadioBridgeConnection
this.serialSession?.dispose();
} else {
this.status = ConnectionStatus.NOT_CONNECTED;
if (currentStatus === ConnectionStatus.NOT_CONNECTED) {
if (
currentStatus === ConnectionStatus.NOT_CONNECTED &&
this.serialSessionOpen
) {
console.log("connecting via listener");
this.serialSession?.connect();
}
}
Expand Down Expand Up @@ -96,6 +102,9 @@ export class MicrobitRadioBridgeConnection
}

async connect(): Promise<ConnectionStatus> {
if (this.disconnectPromise) {
await this.disconnectPromise;
}
// TODO: previously this skipped overlapping connect attempts but that seems awkward
// can we... just not do that? or wait?

Expand All @@ -108,7 +117,10 @@ export class MicrobitRadioBridgeConnection
message: "Serial connect start",
});

await this.delegate.connect();
if (this.delegate.status !== ConnectionStatus.CONNECTED) {
await this.delegate.connect();
}

try {
this.serialSession = new RadioBridgeSerialSession(
this.logging,
Expand Down Expand Up @@ -137,6 +149,7 @@ export class MicrobitRadioBridgeConnection
);

await this.serialSession.connect();
this.serialSessionOpen = true;

this.logging.event({
type: "Connect",
Expand All @@ -154,7 +167,14 @@ export class MicrobitRadioBridgeConnection
}

async disconnect(): Promise<void> {
await this.serialSession?.dispose();
if (this.disconnectPromise) {
return this.disconnectPromise;
}
this.disconnectPromise = (async () => {
this.serialSessionOpen = false;
await this.serialSession?.dispose();
this.disconnectPromise = undefined;
})();
}

private setStatus(status: ConnectionStatus) {
Expand Down Expand Up @@ -260,7 +280,6 @@ class RadioBridgeSerialSession {
this.delegate.addEventListener("serialerror", this.serialErrorListener);

try {
await this.delegate.softwareReset();
await this.handshake();
this.onStatusChanged(ConnectionStatus.CONNECTED);

Expand Down Expand Up @@ -318,6 +337,7 @@ class RadioBridgeSerialSession {
this.responseMap.clear();
this.delegate.removeEventListener("serialdata", this.serialDataListener);
this.delegate.removeEventListener("serialerror", this.serialErrorListener);
await this.delegate.softwareReset();

this.onStatusChanged(ConnectionStatus.NOT_CONNECTED);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export class MicrobitWebUSBConnection
};
this.removeEventListener = (type, ...args) => {
this._removeEventListener(type, ...args);
if (--this.addedListeners[type] === 0) {
if (--this.addedListeners[type] <= 0) {
this.addedListeners[type] = 0;
this.stopNotifications(type);
}
};
Expand Down

0 comments on commit 6f80305

Please sign in to comment.