Skip to content

Commit da37ed6

Browse files
committed
make more readable the if to prompt or not for permissions
1 parent 9190f56 commit da37ed6

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/usb-device-webusb.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ async function getUsbDevices(filters) {
179179
}
180180
let devs = [];
181181
try {
182-
// Fow now, always ask the user to grant access to the device, even if we already have a
183-
// permission to access it. The permissions API for USB is not yet implemented in Chrome,
184-
// and calling requestDevice() after getDevices() causes a SecurityError.
182+
// For now it will always prompt the user unless we pass a serialNumber filter
183+
// if we pass a serialNumber filter and the devices.legth is 0 then we will prompt it
185184
// TODO: Implement a separate API to request a permission from the user
186185
devs = await navigator.usb.getDevices();
187186
let newDev = null;
@@ -190,7 +189,10 @@ async function getUsbDevices(filters) {
190189
(!f.productId || dev.productId === f.productId) &&
191190
(!f.serialNumber || dev.serialNumber === f.serialNumber))));
192191
}
193-
if (!filters.some(f => f.serialNumber) || devs.length === 0) {
192+
193+
const filterById = filters.some(f => f.serialNumber);
194+
const alreadyPermitted = (filterById && devs.length > 0);
195+
if (!alreadyPermitted) {
194196
try {
195197
newDev = await navigator.usb.requestDevice({ filters });
196198
} catch (e) {

0 commit comments

Comments
 (0)