Skip to content

Commit e2f0b05

Browse files
committed
Attempt to fix promise event propagation...
1 parent 72f4396 commit e2f0b05

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/Blazor.Extensions.WebUSB.JS/src/USBManager.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class USBManager {
2626
return found;
2727
}
2828

29-
public RequestDevice = (options: USBRequestDeviceOptions): Promise<USBDeviceFound> => {
29+
public RequestDevice = async (options: USBRequestDeviceOptions): Promise<USBDeviceFound> => {
3030
function isEmpty(obj) {
3131
for (var prop in obj) {
3232
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
@@ -59,15 +59,10 @@ export class USBManager {
5959
reqOptions = { filters: [] };
6060
}
6161

62-
return new Promise((resolve, reject) => {
63-
this.usb.requestDevice(reqOptions)
64-
.then(d => {
65-
let usbDevice = ParseUSBDevice(d);
66-
this._foundDevices.push(d);
67-
resolve(usbDevice);
68-
})
69-
.catch(err => reject(err));
70-
});
62+
let authorizedDevice = await this.usb.requestDevice(reqOptions);
63+
let usbDevice = ParseUSBDevice(authorizedDevice);
64+
this._foundDevices.push(authorizedDevice);
65+
return usbDevice;
7166
}
7267

7368
public OpenDevice = (device: USBDeviceFound): Promise<USBDeviceFound> => {

test/Blazor.Extensions.WebUSB.Test/Pages/BCTest.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ protected override Task OnAfterRenderAsync()
3434
return Task.CompletedTask;
3535
}
3636

37-
protected async Task Run()
37+
private async Task<USBDevice> GetDevice()
3838
{
3939
USBDevice device = await this._usb.RequestDevice(new USBDeviceRequestOptions
4040
{
4141
Filters = _filters
4242
});
43+
return device;
44+
}
45+
46+
protected async Task Run()
47+
{
48+
USBDevice device = await this.GetDevice();
4349

4450
if (device != null)
4551
{

0 commit comments

Comments
 (0)