Skip to content

Commit 0efa8a8

Browse files
WebUSB: Clear the dapwrapper only after disconnection.
It also captures any possible error during WebUSB disconnection.
1 parent fc5842a commit 0efa8a8

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

python-main.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,22 +1258,11 @@ function web_editor(config) {
12581258
webusbErrorHandler(error);
12591259
}
12601260

1261-
function clearDapWrapper(event) {
1262-
if(window.dapwrapper || window.previousDapWrapper) {
1263-
window.dapwrapper = null;
1264-
window.previousDapWrapper = null;
1265-
}
1266-
}
1267-
12681261
function doConnect(serial) {
12691262
// Change button to connecting
12701263
$("#command-connect").hide();
12711264
$("#command-connecting").show();
12721265
$("#command-disconnect").hide();
1273-
1274-
// Device disconnect listener
1275-
// Clears dapwrapper
1276-
navigator.usb.addEventListener('disconnect', clearDapWrapper);
12771266

12781267
// Show error on WebUSB Disconnect Events
12791268
navigator.usb.addEventListener('disconnect', showDisconnectError);
@@ -1340,19 +1329,16 @@ function web_editor(config) {
13401329
console.log(err);
13411330
console.trace();
13421331

1343-
// If there was an error and quick flash is in use, then clear dapwrapper
1344-
if(usePartialFlashing) {
1345-
if(window.dapwrapper) {
1332+
// Disconnect from the microbit
1333+
doDisconnect().then(function() {
1334+
// As there has been an error clear the partial flashing DAPWrapper
1335+
if (window.dapwrapper) {
13461336
window.dapwrapper = null;
13471337
}
1348-
1349-
if(window.previousDapWrapper) {
1338+
if (window.previousDapWrapper) {
13501339
window.previousDapWrapper = null;
13511340
}
1352-
}
1353-
1354-
// Disconnect from the microbit
1355-
doDisconnect();
1341+
});
13561342

13571343
var errorType;
13581344
var errorTitle;
@@ -1478,8 +1464,7 @@ function web_editor(config) {
14781464
}
14791465

14801466
function doDisconnect() {
1481-
1482-
// Remove disconnect listenr
1467+
// Remove disconnect listener
14831468
navigator.usb.removeEventListener('disconnect', showDisconnectError);
14841469

14851470
// Hide serial and disconnect if open
@@ -1507,7 +1492,14 @@ function web_editor(config) {
15071492
p = p.then(function() { return window.daplink.disconnect() });
15081493
}
15091494

1510-
p.finally(function() {
1495+
p = p.catch(function() {
1496+
console.log('Error during disconnection');
1497+
document.dispatchEvent(new CustomEvent('webusb', { 'detail': {
1498+
'flash-type': 'webusb',
1499+
'event-type': 'error',
1500+
'message': 'error-disconnecting'
1501+
}}));
1502+
}).finally(function() {
15111503
console.log('Disconnection Complete');
15121504
document.dispatchEvent(new CustomEvent('webusb', { 'detail': {
15131505
'flash-type': 'webusb',

0 commit comments

Comments
 (0)