Skip to content

Remove serial event state that's available from super class. #56

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class MicrobitWebUSBConnectionImpl
*/
private connection: DAPWrapper | undefined;

/**
* Whether the serial read loop is running.
*
* This is false even if we have serial listeners when we're disconnected or flashing.
* The serial reads interfere with the flash process.
*/
private serialState: boolean = false;

private serialStateChangeQueue = new PromiseQueue();
Expand Down Expand Up @@ -165,7 +171,7 @@ class MicrobitWebUSBConnectionImpl
setTimeout(() => {
if (this.status === ConnectionStatus.CONNECTED) {
this.unloading = false;
if (this.addedListeners.serialdata) {
if (this.hasSerialEventListeners()) {
this.startSerialInternal();
}
}
Expand All @@ -177,10 +183,6 @@ class MicrobitWebUSBConnectionImpl

private logging: Logging;

private addedListeners: Record<string, number> = {
serialdata: 0,
};

constructor(
options: MicrobitWebUSBConnectionOptions = { logging: new NullLogging() },
) {
Expand Down Expand Up @@ -312,7 +314,7 @@ class MicrobitWebUSBConnectionImpl
await this.disconnect();
this.visibilityReconnect = true;
} else {
if (this.addedListeners.serialdata) {
if (this.hasSerialEventListeners()) {
this.log("Reinstating serial after flash");
if (this.connection.daplink) {
await this.connection.daplink.connect();
Expand Down Expand Up @@ -461,7 +463,7 @@ class MicrobitWebUSBConnectionImpl
this.connection = new DAPWrapper(device, this.logging);
}
await withTimeout(this.connection.reconnectAsync(), 10_000);
if (this.addedListeners.serialdata && !this.flashing) {
if (this.hasSerialEventListeners() && !this.flashing) {
this.startSerialInternal();
}
this.setStatus(ConnectionStatus.CONNECTED);
Expand All @@ -483,12 +485,10 @@ class MicrobitWebUSBConnectionImpl
protected eventActivated(type: string): void {
switch (type as keyof SerialConnectionEventMap) {
case "serialdata": {
// Prevent starting serial when flashing.
// Prevent starting serial when flashing. We'll reinstate later.
if (!this.flashing) {
this.startSerialInternal();
}
// Allows for reinstating serial after flashing.
this.addedListeners.serialdata++;
break;
}
}
Expand All @@ -498,11 +498,13 @@ class MicrobitWebUSBConnectionImpl
switch (type as keyof SerialConnectionEventMap) {
case "serialdata": {
this.stopSerialInternal();
this.addedListeners.serialdata--;
break;
}
}
}
private hasSerialEventListeners() {
return this.getActiveEvents().includes("serialdata");
}
}

const genericErrorSuggestingReconnect = (e: any) =>
Expand Down