Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: "20.x"
registry-url: 'https://registry.npmjs.org'
registry-url: "https://registry.npmjs.org"
cache: npm
- uses: microbit-foundation/npm-package-versioner-action@v1
- run: npm ci
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: npm run ci
- run: npm publish
if: github.event_name == 'release' && github.event.action == 'created'
# if: github.event_name == 'release' && github.event.action == 'created'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncomment this line if merging

env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
64 changes: 5 additions & 59 deletions lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,12 @@ export class MicrobitWebUSBConnection
};

private flashing: boolean = false;
private disconnectAfterFlash: boolean = false;
private visibilityReconnect: boolean = false;
private visibilityChangeListener = () => {
if (document.visibilityState === "visible") {
if (
this.visibilityReconnect &&
this.status !== ConnectionStatus.CONNECTED
) {
this.disconnectAfterFlash = false;
this.visibilityReconnect = false;
if (!this.flashing) {
this.log("Reconnecting visible tab");
this.connect();
}
}
} else {
if (!this.unloading && this.status === ConnectionStatus.CONNECTED) {
if (!this.flashing) {
this.log("Disconnecting hidden tab");
this.disconnect().then(() => {
this.visibilityReconnect = true;
});
} else {
this.log("Scheduling disconnect of hidden tab for after flash");
this.disconnectAfterFlash = true;
}
}
}
};

private unloading = false;

private beforeUnloadListener = () => {
// If serial is in progress when the page unloads with V1 DAPLink 0254 or V2 0255
// then it'll fail to reconnect with mismatched command/response errors.
// Try hard to disconnect as a workaround.
// https://github.com/microbit-foundation/python-editor-v3/issues/89
this.unloading = true;
this.stopSerialInternal();
// The user might stay on the page if they have unsaved changes and there's another beforeunload listener.
window.addEventListener(
Expand All @@ -119,7 +87,6 @@ export class MicrobitWebUSBConnection
const assumePageIsStayingOpenDelay = 1000;
setTimeout(() => {
if (this.status === ConnectionStatus.CONNECTED) {
this.unloading = false;
if (this.addedListeners.serialdata) {
this.startSerialInternal();
}
Expand Down Expand Up @@ -153,12 +120,6 @@ export class MicrobitWebUSBConnection
}
if (typeof window !== "undefined") {
window.addEventListener("beforeunload", this.beforeUnloadListener);
if (window.document) {
window.document.addEventListener(
"visibilitychange",
this.visibilityChangeListener,
);
}
}
}

Expand All @@ -168,12 +129,6 @@ export class MicrobitWebUSBConnection
}
if (typeof window !== "undefined") {
window.removeEventListener("beforeunload", this.beforeUnloadListener);
if (window.document) {
window.document.removeEventListener(
"visibilitychange",
this.visibilityChangeListener,
);
}
}
}

Expand Down Expand Up @@ -260,19 +215,11 @@ export class MicrobitWebUSBConnection
}
} finally {
progress(undefined, wasPartial);

if (this.disconnectAfterFlash) {
this.log("Disconnecting after flash due to tab visibility");
this.disconnectAfterFlash = false;
await this.disconnect();
this.visibilityReconnect = true;
} else {
if (this.addedListeners.serialdata) {
this.log("Reinstating serial after flash");
if (this.connection.daplink) {
await this.connection.daplink.connect();
await this.startSerialInternal();
}
if (this.addedListeners.serialdata) {
this.log("Reinstating serial after flash");
if (this.connection.daplink) {
await this.connection.daplink.connect();
await this.startSerialInternal();
}
}
}
Expand Down Expand Up @@ -334,7 +281,6 @@ export class MicrobitWebUSBConnection

private setStatus(newStatus: ConnectionStatus) {
this.status = newStatus;
this.visibilityReconnect = false;
this.log("USB connection status " + newStatus);
this.dispatchTypedEvent("status", new ConnectionStatusEvent(newStatus));
}
Expand Down