Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

async/awaitify remaining logic in webusb.ts, tweak error message on locked device #10146

Merged
merged 2 commits into from
Sep 3, 2024

Conversation

jwunderl
Copy link
Member

@jwunderl jwunderl commented Sep 3, 2024

Was walking through this code line by line on friday to try and figure out what was up with chromebook webusb, so async/await-ified the remaining bits along the way. Also added some type info to navigator to remove some unnecessary casting, & made the 'connection failure' dialog a bit more explicit in cases where user gave a device but it didn't work (as that most often comes down to them having it already open in another browser / application, e.g. incognito + normal browser at once).

@jwunderl jwunderl requested a review from a team September 3, 2024 20:37
Copy link
Contributor

@eanders-ms eanders-ms left a comment

Choose a reason for hiding this comment

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

Nice cleanup. Made a few minor suggestions.

pxtlib/webusb.ts Outdated
}

disable() {
if (!this.enabled) return;

this.enabled = false;
this.log(`unregistering webusb events`);
(navigator as any).usb.removeEventListener('disconnect', this.handleUSBDisconnected);
(navigator as any).usb.removeEventListener('connect', this.handleUSBConnected);
navigator?.usb.removeEventListener('disconnect', this.handleUSBDisconnected);
Copy link
Contributor

Choose a reason for hiding this comment

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

usb is declared optional in the interface. Do you want to honor that here?

Suggested change
navigator?.usb.removeEventListener('disconnect', this.handleUSBDisconnected);
navigator?.usb?.removeEventListener('disconnect', this.handleUSBDisconnected);

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, i intended to put them there yeah, usb feature will be disabled overall if navigator.usb is unset so this was for clarity but fixing, thanks!

pxtlib/webusb.ts Outdated
this.ready = true;
if (isHF2) {
// just starting, not waiting on it.
this.readLoop();
Copy link
Contributor

Choose a reason for hiding this comment

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

For extra clarity on intent here:

Suggested change
this.readLoop();
/*await*/ this.readLoop();

pxtlib/webusb.ts Outdated
})
export async function pairAsync(): Promise<boolean> {
try {
const dev = await navigator.usb.requestDevice({
Copy link
Contributor

Choose a reason for hiding this comment

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

Could safe continuation be needed here? There are a few other instances in the file as well.

Suggested change
const dev = await navigator.usb.requestDevice({
const dev = await navigator?.usb?.requestDevice({

Copy link
Member Author

Choose a reason for hiding this comment

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

https://github.com/microsoft/pxt/blob/dev/jwunderl/async-ify-webusb-cleanup/pxtlib/webusb.ts#L608 is where it'll be checked overall, but adding in here for clarity sake ~

@@ -197,40 +225,38 @@ namespace pxt.usb {

this.enabled = true;
this.log("registering webusb events");
(navigator as any).usb.addEventListener('disconnect', this.handleUSBDisconnected, false);
(navigator as any).usb.addEventListener('connect', this.handleUSBConnected, false);
navigator.usb?.addEventListener('disconnect', this.handleUSBDisconnected, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to Eric's questions about safe continuation. Does this need

Suggested change
navigator.usb?.addEventListener('disconnect', this.handleUSBDisconnected, false);
navigator?.usb?.addEventListener('disconnect', this.handleUSBDisconnected, false);

like there are in other places or is just having .usb fine in this case? Same question applies to the line below.

Copy link
Member Author

Choose a reason for hiding this comment

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

https://github.com/microsoft/pxt/blob/dev/jwunderl/async-ify-webusb-cleanup/pxtlib/webusb.ts#L608 is where we check for it being enabled for the pxt.usb.isEnabled() which we use to gate all other calls; but adding in for clarity sake~

@jwunderl jwunderl merged commit 5577469 into master Sep 3, 2024
6 checks passed
@jwunderl jwunderl deleted the dev/jwunderl/async-ify-webusb-cleanup branch September 3, 2024 21:30
abchatra pushed a commit that referenced this pull request Sep 4, 2024
…ocked device (#10146)

* async/awaitify remaining logic in webusb.ts, tweak error message on locked

* pr feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants