@@ -1258,22 +1258,11 @@ function web_editor(config) {
1258
1258
webusbErrorHandler ( error ) ;
1259
1259
}
1260
1260
1261
- function clearDapWrapper ( event ) {
1262
- if ( window . dapwrapper || window . previousDapWrapper ) {
1263
- window . dapwrapper = null ;
1264
- window . previousDapWrapper = null ;
1265
- }
1266
- }
1267
-
1268
1261
function doConnect ( serial ) {
1269
1262
// Change button to connecting
1270
1263
$ ( "#command-connect" ) . hide ( ) ;
1271
1264
$ ( "#command-connecting" ) . show ( ) ;
1272
1265
$ ( "#command-disconnect" ) . hide ( ) ;
1273
-
1274
- // Device disconnect listener
1275
- // Clears dapwrapper
1276
- navigator . usb . addEventListener ( 'disconnect' , clearDapWrapper ) ;
1277
1266
1278
1267
// Show error on WebUSB Disconnect Events
1279
1268
navigator . usb . addEventListener ( 'disconnect' , showDisconnectError ) ;
@@ -1340,19 +1329,16 @@ function web_editor(config) {
1340
1329
console . log ( err ) ;
1341
1330
console . trace ( ) ;
1342
1331
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 ) {
1346
1336
window . dapwrapper = null ;
1347
1337
}
1348
-
1349
- if ( window . previousDapWrapper ) {
1338
+ if ( window . previousDapWrapper ) {
1350
1339
window . previousDapWrapper = null ;
1351
1340
}
1352
- }
1353
-
1354
- // Disconnect from the microbit
1355
- doDisconnect ( ) ;
1341
+ } ) ;
1356
1342
1357
1343
var errorType ;
1358
1344
var errorTitle ;
@@ -1478,8 +1464,7 @@ function web_editor(config) {
1478
1464
}
1479
1465
1480
1466
function doDisconnect ( ) {
1481
-
1482
- // Remove disconnect listenr
1467
+ // Remove disconnect listener
1483
1468
navigator . usb . removeEventListener ( 'disconnect' , showDisconnectError ) ;
1484
1469
1485
1470
// Hide serial and disconnect if open
@@ -1507,7 +1492,14 @@ function web_editor(config) {
1507
1492
p = p . then ( function ( ) { return window . daplink . disconnect ( ) } ) ;
1508
1493
}
1509
1494
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 ( ) {
1511
1503
console . log ( 'Disconnection Complete' ) ;
1512
1504
document . dispatchEvent ( new CustomEvent ( 'webusb' , { 'detail' : {
1513
1505
'flash-type' : 'webusb' ,
0 commit comments