@@ -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 ;
@@ -1462,7 +1448,7 @@ function web_editor(config) {
14621448 $ ( '#flashing-overlay' ) . keydown ( function ( e ) {
14631449 if ( e . which == 27 ) {
14641450 flashErrorClose ( ) ;
1465- }
1451+ }
14661452 } ) ;
14671453
14681454 // Send event
@@ -1478,22 +1464,13 @@ 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
14861471 if ( $ ( "#repl" ) . css ( 'display' ) != 'none' ) {
1487- $ ( "#repl" ) . hide ( ) ;
1488- $ ( "#request-repl" ) . hide ( ) ;
1489- $ ( "#request-serial" ) . hide ( ) ;
1490- $ ( "#editor-container" ) . show ( ) ;
1472+ closeSerial ( ) ;
14911473 }
1492- $ ( "#command-serial" ) . attr ( "title" , config [ "translate" ] [ "static-strings" ] [ "buttons" ] [ "command-serial" ] [ "title" ] ) ;
1493- $ ( "#command-serial > .roundlabel" ) . text ( config [ "translate" ] [ "static-strings" ] [ "buttons" ] [ "command-serial" ] [ "label" ] ) ;
1494-
1495- $ ( "#repl" ) . empty ( ) ;
1496- REPL = null ;
14971474
14981475 // Change button to connect
14991476 $ ( "#command-disconnect" ) . hide ( ) ;
@@ -1506,22 +1483,23 @@ function web_editor(config) {
15061483
15071484 var p = Promise . resolve ( ) ;
15081485
1509- if ( usePartialFlashing ) {
1510- if ( window . dapwrapper ) {
1511- console . log ( "Disconnecting: Using Quick Flash" ) ;
1512- p = p . then ( function ( ) { window . dapwrapper . daplink . stopSerialRead ( ) } )
1513- . then ( function ( ) { window . dapwrapper . disconnectAsync ( ) } ) ;
1514- }
1486+ if ( usePartialFlashing && window . dapwrapper ) {
1487+ console . log ( 'Disconnecting: Using Quick Flash' ) ;
1488+ p = p . then ( function ( ) { return window . dapwrapper . disconnectAsync ( ) } ) ;
15151489 }
1516- else {
1517- if ( window . daplink ) {
1518- console . log ( "Disconnecting: Using Full Flash" ) ;
1519- p = p . then ( function ( ) { window . daplink . stopSerialRead ( ) } )
1520- . then ( function ( ) { window . daplink . disconnect ( ) } ) ;
1521- }
1490+ else if ( window . daplink ) {
1491+ console . log ( 'Disconnecting: Using Full Flash' ) ;
1492+ p = p . then ( function ( ) { return window . daplink . disconnect ( ) } ) ;
15221493 }
15231494
1524- 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 ( ) {
15251503 console . log ( 'Disconnection Complete' ) ;
15261504 document . dispatchEvent ( new CustomEvent ( 'webusb' , { 'detail' : {
15271505 'flash-type' : 'webusb' ,
@@ -1538,21 +1516,7 @@ function web_editor(config) {
15381516
15391517 // Hide serial and disconnect if open
15401518 if ( $ ( "#repl" ) . css ( 'display' ) != 'none' ) {
1541- $ ( "#repl" ) . hide ( ) ;
1542- $ ( "#request-repl" ) . hide ( ) ;
1543- $ ( "#request-serial" ) . hide ( ) ;
1544- $ ( "#editor-container" ) . show ( ) ;
1545- $ ( "#command-serial" ) . attr ( "title" , config [ "translate" ] [ "static-strings" ] [ "buttons" ] [ "command-serial" ] [ "title" ] ) ;
1546- $ ( "#command-serial > .roundlabel" ) . text ( config [ "translate" ] [ "static-strings" ] [ "buttons" ] [ "command-serial" ] [ "label" ] ) ;
1547-
1548- if ( usePartialFlashing ) {
1549- if ( window . dapwrapper ) {
1550- window . dapwrapper . daplink . stopSerialRead ( ) ;
1551- }
1552- }
1553- else {
1554- window . daplink . stopSerialRead ( ) ;
1555- }
1519+ closeSerial ( ) ;
15561520 }
15571521
15581522 // Get the hex to flash in bytes format, exit if there is an error
@@ -1580,9 +1544,6 @@ function web_editor(config) {
15801544
15811545 var p = Promise . resolve ( ) ;
15821546 if ( usePartialFlashing ) {
1583- REPL = null ;
1584- $ ( "#repl" ) . empty ( ) ;
1585-
15861547 p = window . dapwrapper . disconnectAsync ( )
15871548 . then ( function ( ) {
15881549 return PartialFlashing . connectDapAsync ( ) ;
@@ -1640,28 +1601,35 @@ function web_editor(config) {
16401601 } ) ;
16411602 }
16421603
1604+ function closeSerial ( keepSession ) {
1605+ console . log ( "Closing Serial Terminal" ) ;
1606+ $ ( '#repl' ) . empty ( ) ;
1607+ $ ( '#repl' ) . hide ( ) ;
1608+ $ ( '#request-repl' ) . hide ( ) ;
1609+ $ ( '#request-serial' ) . hide ( ) ;
1610+ $ ( '#editor-container' ) . show ( ) ;
1611+
1612+ var serialButton = config [ 'translate' ] [ 'static-strings' ] [ 'buttons' ] [ 'command-serial' ] ;
1613+ $ ( '#command-serial' ) . attr ( 'title' , serialButton [ 'title' ] ) ;
1614+ $ ( '#command-serial > .roundlabel' ) . text ( serialButton [ 'label' ] ) ;
1615+
1616+ var daplink = usePartialFlashing ? window . dapwrapper . daplink : window . daplink ;
1617+ daplink . stopSerialRead ( ) ;
1618+ daplink . removeAllListeners ( DAPjs . DAPLink . EVENT_SERIAL_DATA ) ;
1619+ REPL . uninstallKeyboard ( ) ;
1620+ REPL . io . pop ( ) ;
1621+ REPL = null ;
1622+ }
1623+
16431624 function doSerial ( ) {
1644- console . log ( "Setting Up Serial Terminal" ) ;
1645- // Hide terminal
1625+ // Hide terminal if it is currently shown
16461626 var serialButton = config [ "translate" ] [ "static-strings" ] [ "buttons" ] [ "command-serial" ] ;
16471627 if ( $ ( "#repl" ) . css ( 'display' ) != 'none' ) {
1648- $ ( "#repl" ) . hide ( ) ;
1649- $ ( "#request-repl" ) . hide ( ) ;
1650- $ ( "#request-serial" ) . hide ( ) ;
1651- $ ( "#editor-container" ) . show ( ) ;
1652- $ ( "#command-serial" ) . attr ( "title" , serialButton [ "label" ] ) ;
1653- $ ( "#command-serial > .roundlabel" ) . text ( serialButton [ "label" ] ) ;
1654- if ( usePartialFlashing ) {
1655- if ( window . dapwrapper ) {
1656- window . dapwrapper . daplink . stopSerialRead ( ) ;
1657- }
1658- }
1659- else {
1660- window . daplink . stopSerialRead ( ) ;
1661- }
1628+ closeSerial ( ) ;
16621629 return ;
16631630 }
16641631
1632+ console . log ( "Setting Up Serial Terminal" ) ;
16651633 // Check if we need to connect
16661634 if ( $ ( "#command-connect" ) . is ( ":visible" ) ) {
16671635 doConnect ( true ) ;
@@ -1757,8 +1725,8 @@ function web_editor(config) {
17571725 $ ( overlayContainer ) . keydown ( function ( e ) {
17581726 if ( e . which == 27 ) {
17591727 modalMsgClose ( ) ;
1760- }
1761- } ) ;
1728+ }
1729+ } ) ;
17621730 }
17631731
17641732 function formatMenuContainer ( parentButtonId , containerId ) {
0 commit comments