8000 Testing disconnect before manual reload. · arduino/lab-micropython-editor@afadd3f · GitHub
[go: up one dir, main page]

Skip to content

Commit afadd3f

Browse files
committed
Testing disconnect before manual reload.
Signed-off-by: ubi de feo <me@ubidefeo.com>
1 parent 861b658 commit afadd3f

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

backend/ipc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,9 @@ module.exports = function registerIPCHandlers(win, ipcMain, app, dialog) {
139139
event.preventDefault()
140140
win.webContents.send('check-before-close')
141141
})
142+
143+
// handle disconnection before reload
144+
ipcMain.handle('prepare-reload', async (event) => {
145+
return win.webContents.send('before-reload')
146+
})
142147
}

backend/menu.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@ module.exports = function registerMenu(win, state = {}) {
8686
{
8787
label: 'View',
8888
submenu: [
89-
{ role: 'reload', accelerator: '' },
89+
{
90+
label: 'Reload',
91+
accelerator: '',
92+
click: async () => {
93+
try {
94+
win.webContents.send('cleanup-before-reload')
95+
setTimeout(() => {
96+
win.reload()
97+
}, 500)
98+
} catch(e) {
99+
console.error('Reload from menu failed:', e)
100+
}
101+
}
102+
},
90103
{ role: 'toggleDevTools', accelerator: ''},
91104
{ type: 'separator' },
92105
{ role: 'resetZoom' },

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ function createWindow () {
4949
win.show()
5050
})
5151

52+
win.webContents.on('before-reload', async (event) => {
53+
// Prevent the default reload behavior
54+
event.preventDefault()
55+
56+
try {
57+
// Tell renderer to do cleanup
58+
win.webContents.send('cleanup-before-reload')
59+
60+
// Wait for cleanup then reload
61+
setTimeout(() => {
62+
// This will trigger a page reload, but won't trigger 'before-reload' again
63+
win.reload()
64+
}, 500)
65+
} catch(e) {
66+
console.error('Reload preparation failed:', e)
67+
}
68+
})
69+
5270
const initialMenuState = {
5371
isConnected: false,
5472
view: 'editor'

preload.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ const Window = {
174174
})
175175
},
176176

177+
onBeforeReload: (callback) => {
178+
ipcRenderer.on('cleanup-before-reload', async () => {
179+
try {
180+
await callback()
181+
} catch(e) {
182+
console.error('Cleanup before reload failed:', e)
183+
}
184+
})
185+
},
177186

178187
beforeClose: (callback) => ipcRenderer.on('check-before-close', callback),
179188
confirmClose: () => ipcRenderer.invoke(& C4B9 #39;confirm-close'),

ui/arduino/store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,18 @@ async function store(state, emitter) {
13641364
emitter.emit('render')
13651365
})
13661366

1367+
win.onBeforeReload(async () => {
1368+
// Perform any cleanup needed
1369+
if (state.isConnected) {
1370+
await serial.disconnect()
1371+
state.isConnected = false
1372+
state.panelHeight = PANEL_CLOSED
1373+
state.boardFiles = []
1374+
state.boardNavigationPath = '/'
1375+
}
1376+
// Any other cleanup needed
1377+
})
1378+
13671379
win.beforeClose(async () => {
13681380
const hasChanges = !!state.openFiles.find(f => f.hasChanges)
13691381
if (hasChanges) {

0 commit comments

Comments
 (0)
0