Skip to content

Commit e426ea9

Browse files
committed
change getDevices Order, ask permissions first and then get devices
1 parent 461d133 commit e426ea9

2 files changed

Lines changed: 5 additions & 13 deletions

File tree

src/usb-device-webusb.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,18 @@ async function getUsbDevices(filters, { prompt = true } = {}) {
190190
filters = validateFilters(filters);
191191
let devs = [];
192192
try {
193-
devs = await navigator.usb.getDevices();
194-
devs = devs.filter(dev => matchesFilters(dev, filters));
195193
if (prompt) {
196-
let newDev = null;
197194
try {
198-
newDev = await navigator.usb.requestDevice({ filters });
195+
await navigator.usb.requestDevice({ filters });
199196
} catch (e) {
200197
// Ignore NotFoundError which means that the user has cancelled the request
201198
if (e.name !== 'NotFoundError') {
202199
throw e;
203200
}
204201
}
205-
if (newDev) {
206-
// Avoid listing the same device twice
207-
const hasNewDev = devs.some(dev => dev.vendorId === newDev.vendorId && dev.productId === newDev.productId &&
208-
dev.serialNumber === newDev.serialNumber);
209-
if (!hasNewDev) {
210-
devs.push(newDev);
211-
}
212-
}
213202
}
203+
devs = await navigator.usb.getDevices();
204+
devs = devs.filter(dev => matchesFilters(dev, filters));
214205
} catch (err) {
215206
throw new UsbError('Unable to enumerate USB devices', { cause: err });
216207
}

src/usb-device-webusb.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ describe('usb-device-webusb', () => {
9090
});
9191

9292
it('includes the device the user selected', async () => {
93-
usb.getDevices.resolves([PHOTON]);
9493
usb.requestDevice.resolves(ARGON);
94+
// Selecting a device in the prompt adds it to the permitted devices
95+
usb.getDevices.resolves([PHOTON, ARGON]);
9596
const devs = await getUsbDevices([{ vendorId: PHOTON.vendorId }]);
9697
expect(devs.map(d => d.internalObject)).to.deep.equal([PHOTON, ARGON]);
9798
});

0 commit comments

Comments
 (0)