Skip to content

Commit

Permalink
feat(webusb): add unique error type for device busy
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Aug 12, 2024
1 parent 5bafa66 commit 33823df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
24 changes: 20 additions & 4 deletions libraries/adb-daemon-webusb/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class AdbDaemonWebUsbConnection
} catch (e) {
// On Windows, disconnecting the device will cause `NetworkError` to be thrown,
// even before the `disconnect` event is fired.
// We need to wait a little bit and check if the device is still connected.
// Wait a little while and check if the device is still connected.
// https://github.com/WICG/webusb/issues/219
if (isErrorName(e, "NetworkError")) {
await new Promise<void>((resolve) => {
Expand All @@ -253,8 +253,6 @@ export class AdbDaemonWebUsbConnection

if (closed) {
return undefined;
} else {
throw e;
}
}

Expand Down Expand Up @@ -318,7 +316,15 @@ export class AdbDaemonWebUsbDevice implements AdbDaemonDevice {
}

if (!interface_.claimed) {
await this.#raw.claimInterface(interface_.interfaceNumber);
try {
await this.#raw.claimInterface(interface_.interfaceNumber);
} catch (e) {
if (isErrorName(e, "NetworkError")) {
throw new AdbDaemonWebUsbDevice.DeviceBusyError(e);
}

throw e;
}
}

if (
Expand Down Expand Up @@ -350,3 +356,13 @@ export class AdbDaemonWebUsbDevice implements AdbDaemonDevice {
);
}
}

export namespace AdbDaemonWebUsbDevice {
export class DeviceBusyError extends Error {
constructor(cause?: Error) {
super("The device is already in used by another program", {
cause,
});
}
}
}
2 changes: 1 addition & 1 deletion libraries/adb-daemon-webusb/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function isErrorName(e: unknown, name: string): boolean {
export function isErrorName(e: unknown, name: string): e is Error {
// node-usb package doesn't use `DOMException`,
// so use a looser check
// https://github.com/node-usb/node-usb/issues/573
Expand Down
12 changes: 7 additions & 5 deletions libraries/adb/src/daemon/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ interface AdbDaemonSocketConnectorConstructionOptions {

/**
* The number of bytes the device can send before receiving an ack packet.
* Using delayed ack can improve the throughput,
* especially when the device is connected over Wi-Fi (so the latency is higher).
*
* On Android 14 and newer, the Delayed Acknowledgement feature is added to
* improve performance, especially for high-latency connections like ADB over Wi-Fi.
*
* When `features` doesn't include `AdbFeature.DelayedAck`, it must be set to 0. Otherwise,
* the value must be in the range of unsigned 32-bit integer. If the device enabled
* delayed ack but the client didn't, the device will throw an error when the client sends
* the first data packet. And vice versa.
* the value must be in the range of unsigned 32-bit integer.
*
* If the device enabled delayed ack but the client didn't, the device will throw an error
* when the client sends the first data packet. And vice versa.
*/
initialDelayedAckBytes: number;

Expand Down

0 comments on commit 33823df

Please sign in to comment.