Skip to content

Commit

Permalink
feat(webusb): support exclusionFilters option
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Sep 25, 2023
1 parent 85d082f commit bde5e2c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions libraries/adb-daemon-webusb/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { ADB_DEFAULT_DEVICE_FILTER, AdbDaemonWebUsbDevice } from "./device.js";
import type { AdbDeviceFilter } 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 {
/**
* Gets the instance of {@link AdbDaemonWebUsbDeviceManager} using browser WebUSB implementation.
Expand Down Expand Up @@ -38,17 +46,23 @@ export class AdbDaemonWebUsbDeviceManager {
* or `undefined` if the user cancelled the device picker.
*/
async requestDevice(
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER],
options: AdbDaemonWebUsbDeviceManager.RequestDeviceOptions = {},
): 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");
}

try {
const device = await this.#usbManager.requestDevice({
filters,
});
return new AdbDaemonWebUsbDevice(device, filters, this.#usbManager);
const device = await this.#usbManager.requestDevice(
options as USBDeviceRequestOptions,
);
return new AdbDaemonWebUsbDevice(
device,
options.filters,
this.#usbManager,
);
} catch (e) {
// No device selected
if (isErrorName(e, "NotFoundError")) {
Expand Down

0 comments on commit bde5e2c

Please sign in to comment.