Skip to content

Commit

Permalink
Add onDisconnect back to webusb interop
Browse files Browse the repository at this point in the history
Signed-off-by: Shengqi Chen <[email protected]>
  • Loading branch information
Harry-Chen committed Dec 21, 2024
1 parent adcf850 commit 3febb2c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/webusb_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const int USB_CLASS_CODE_VENDOR_SPECIFIC = 0xFF;
@JS('navigator.usb')
extension type _USB._(JSObject _) implements JSObject {
external static JSObject requestDevice(_USBDeviceRequestOptions options);
external static set ondisconnect(JSFunction value);
}

@JS()
Expand Down Expand Up @@ -60,6 +61,11 @@ class WebUSB {
return _device != null && getProperty(_device, 'opened');
}

static void _onDisconnect() {
_device = null;
log.info('device is disconnected from WebUSB API');
}

static const USB_PROBE_MAGIC = '_NFC_IM_';

/// Try to poll a WebUSB device according to our protocol.
Expand All @@ -76,6 +82,7 @@ class WebUSB {
promiseToFuture(callMethod(device, 'claimInterface', [1])))
.timeout(Duration(milliseconds: timeout));
_device = device;
_USB.ondisconnect = _onDisconnect.toJS;
log.info("WebUSB device opened", _device);
} on TimeoutException catch (_) {
log.severe("Polling tag timeout");
Expand Down

0 comments on commit 3febb2c

Please sign in to comment.