Skip to content

Commit cc5a8ee

Browse files
committed
Decouple UI and logic of disconnection event
1 parent 42f8f9c commit cc5a8ee

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

backend/serial.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const Serial = {
6666
renameFile: async (oldName, newName) => {
6767
return board.fs_rename(oldName, newName)
6868
},
69-
onConnectionLost: async (fn) => {
69+
onConnectionClosed: async (fn) => {
7070
board.serial.on('close', fn)
7171
},
7272
createFolder: async (folder) => {

ui/arduino/store.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ async function store(state, emitter) {
114114
// CONNECTION DIALOG
115115
emitter.on('open-connection-dialog', async () => {
116116
log('open-connection-dialog')
117-
emitter.emit('disconnect')
117+
// UI should be in disconnected state, no need to update
118+
await serial.disconnect()
118119
state.availablePorts = await getAvailablePorts()
119120
state.isConnectionDialogOpen = true
120121
emitter.emit('render')
@@ -180,14 +181,16 @@ async function store(state, emitter) {
180181
term.write(data)
181182
term.scrollToBottom()
182183
})
183-
serial.onConnectionLost(() => emitter.emit('disconnect'))
184+
185+
// Update the UI when the conncetion is closed
186+
// This may happen when unplugging the board
187+
serial.onConnectionClosed(() => emitter.emit('disconnected'))
184188

185189
emitter.emit('close-connection-dialog')
186190
emitter.emit('refresh-files')
187191
emitter.emit('render')
188192
})
189-
emitter.on('disconnect', async () => {
190-
await serial.disconnect()
193+
emitter.on('disconnected', () => {
191194
state.isConnected = false
192195
state.panelHeight = PANEL_CLOSED
193196
state.boardFiles = []
@@ -196,6 +199,11 @@ async function store(state, emitter) {
196199
emitter.emit('render')
197200
updateMenu()
198201
})
202+
emitter.on('disconnect', async () => {
203+
await serial.disconnect()
204+
// Update the UI after closing the connection
205+
emitter.emit('disconnected')
206+
})
199207
emitter.on('connection-timeout', async () => {
200208
state.isConnected = false
201209
state.isConnecting = false

ui/arduino/views/components/toolbar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Toolbar(state, emit) {
1616
${Button({
1717
icon: state.isConnected ? 'connect.svg' : 'disconnect.svg',
1818
tooltip: state.isConnected ? `Disconnect (${metaKeyString}+Shift+D)` : `Connect (${metaKeyString}+Shift+C)`,
19-
onClick: () => emit('open-connection-dialog'),
19+
onClick: () => state.isConnected ? emit('disconnect') : emit('open-connection-dialog'),
2020
active: state.isConnected
2121
})}
2222

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy