feat(webusb): support exclusionFilters option

This commit is contained in:
Simon Chan 2023-09-25 21:52:15 +08:00
parent 85d082f9e7
commit bde5e2c7fc
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD

View file

@ -2,6 +2,14 @@ import { ADB_DEFAULT_DEVICE_FILTER, AdbDaemonWebUsbDevice } from "./device.js";
import type { AdbDeviceFilter } from "./utils.js"; import type { AdbDeviceFilter } from "./utils.js";
import { findUsbAlternateInterface, isErrorName } from "./utils.js"; import { findUsbAlternateInterface, isErrorName } from "./utils.js";
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace AdbDaemonWebUsbDeviceManager {
export interface RequestDeviceOptions {
filters?: AdbDeviceFilter[] | undefined;
exclusionFilters?: USBDeviceFilter[] | undefined;
}
}
export class AdbDaemonWebUsbDeviceManager { export class AdbDaemonWebUsbDeviceManager {
/** /**
* Gets the instance of {@link AdbDaemonWebUsbDeviceManager} using browser WebUSB implementation. * Gets the instance of {@link AdbDaemonWebUsbDeviceManager} using browser WebUSB implementation.
@ -38,17 +46,23 @@ export class AdbDaemonWebUsbDeviceManager {
* or `undefined` if the user cancelled the device picker. * or `undefined` if the user cancelled the device picker.
*/ */
async requestDevice( async requestDevice(
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER], options: AdbDaemonWebUsbDeviceManager.RequestDeviceOptions = {},
): Promise<AdbDaemonWebUsbDevice | undefined> { ): Promise<AdbDaemonWebUsbDevice | undefined> {
if (filters.length === 0) { if (!options.filters) {
options.filters = [ADB_DEFAULT_DEVICE_FILTER];
} else if (options.filters.length === 0) {
throw new TypeError("filters must not be empty"); throw new TypeError("filters must not be empty");
} }
try { try {
const device = await this.#usbManager.requestDevice({ const device = await this.#usbManager.requestDevice(
filters, options as USBDeviceRequestOptions,
}); );
return new AdbDaemonWebUsbDevice(device, filters, this.#usbManager); return new AdbDaemonWebUsbDevice(
device,
options.filters,
this.#usbManager,
);
} catch (e) { } catch (e) {
// No device selected // No device selected
if (isErrorName(e, "NotFoundError")) { if (isErrorName(e, "NotFoundError")) {