@yume-chan/adb-daemon-webusb
Version:
Adb daemon transport connection for `@yume-chan/adb` using WebUSB API.
67 lines • 2.66 kB
JavaScript
import { AdbDaemonWebUsbDevice, mergeDefaultAdbInterfaceFilter, } from "./device.js";
import { AdbDaemonWebUsbDeviceObserver } from "./observer.js";
import { isErrorName, matchFilters } from "./utils.js";
export class AdbDaemonWebUsbDeviceManager {
/**
* Gets the instance of {@link AdbDaemonWebUsbDeviceManager} using browser WebUSB implementation.
*
* May be `undefined` if current runtime does not support WebUSB.
*/
static BROWSER = /* #__PURE__ */ (() => typeof globalThis.navigator !== "undefined" && globalThis.navigator.usb
? new AdbDaemonWebUsbDeviceManager(globalThis.navigator.usb)
: undefined)();
#usbManager;
/**
* Create a new instance of {@link AdbDaemonWebUsbDeviceManager} using the specified WebUSB implementation.
* @param usbManager A WebUSB compatible interface.
*/
constructor(usbManager) {
this.#usbManager = usbManager;
}
/**
* Call `USB#requestDevice()` to prompt the user to select a device.
*/
async requestDevice(options = {}) {
const filters = mergeDefaultAdbInterfaceFilter(options.filters);
try {
const device = await this.#usbManager.requestDevice({
filters,
exclusionFilters: options.exclusionFilters,
});
const interface_ = matchFilters(device, filters, options.exclusionFilters);
if (!interface_) {
// `#usbManager` doesn't support `exclusionFilters`,
// selected device is invalid
return undefined;
}
return new AdbDaemonWebUsbDevice(device, interface_, this.#usbManager);
}
catch (e) {
// No device selected
if (isErrorName(e, "NotFoundError")) {
return undefined;
}
throw e;
}
}
/**
* Get all connected and requested devices that match the specified filters.
*/
async getDevices(options = {}) {
const filters = mergeDefaultAdbInterfaceFilter(options.filters);
const devices = await this.#usbManager.getDevices();
// filter map
const result = [];
for (const device of devices) {
const interface_ = matchFilters(device, filters, options.exclusionFilters);
if (interface_) {
result.push(new AdbDaemonWebUsbDevice(device, interface_, this.#usbManager));
}
}
return result;
}
trackDevices(options = {}) {
return AdbDaemonWebUsbDeviceObserver.create(this.#usbManager, options);
}
}
//# sourceMappingURL=manager.js.map