@@ -112,7 +112,8 @@ async function store(state, emitter) {
112112 // CONNECTION DIALOG
113113 emitter . on ( 'open-connection-dialog' , async ( ) => {
114114 log ( 'open-connection-dialog' )
115- emitter . emit ( 'disconnect' )
115+ // UI should be in disconnected state, no need to update
116+ await serial . disconnect ( )
116117 state . availablePorts = await getAvailablePorts ( )
117118 state . isConnectionDialogOpen = true
118119 emitter . emit ( 'render' )
@@ -178,14 +179,16 @@ async function store(state, emitter) {
178179 term . write ( data )
179180 term . scrollToBottom ( )
180181 } )
181- serial . onConnectionLost ( ( ) => emitter . emit ( 'disconnect' ) )
182+
183+ // Update the UI when the conncetion is closed
184+ // This may happen when unplugging the board
185+ serial . onConnectionClosed ( ( ) => emitter . emit ( 'disconnected' ) )
182186
183187 emitter . emit ( 'close-connection-dialog' )
184188 emitter . emit ( 'refresh-files' )
185189 emitter . emit ( 'render' )
186190 } )
187- emitter . on ( 'disconnect' , async ( ) => {
188- await serial . disconnect ( )
191+ emitter . on ( 'disconnected' , ( ) => {
189192 state . isConnected = false
190193 state . panelHeight = PANEL_CLOSED
191194 state . boardFiles = [ ]
@@ -194,6 +197,11 @@ async function store(state, emitter) {
194197 emitter . emit ( 'render' )
195198 updateMenu ( )
196199 } )
200+ emitter . on ( 'disconnect' , async ( ) => {
201+ await serial . disconnect ( )
202+ // Update the UI after closing the connection
203+ emitter . emit ( 'disconnected' )
204+ } )
197205 emitter . on ( 'connection-timeout' , async ( ) => {
198206 state . isConnected = false
199207 state . isConnecting = false
0 commit comments