Skip to content

Commit

Permalink
save session data on window close rather than app quit
Browse files Browse the repository at this point in the history
on windows and linux, the window closes before the before-quit event
occurs, so the data needs to be saved earlier than that
  • Loading branch information
PalmerAL committed Mar 16, 2020
1 parent 1a52fd6 commit 87c587f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
30 changes: 14 additions & 16 deletions js/sessionRestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var browserUI = require('browserUI.js')
window.sessionRestore = {
savePath: userDataPath + (platformType === 'windows' ? '\\sessionRestore.json' : '/sessionRestore.json'),
previousState: null,
save: function (cb, forceSave) {
save: function (forceSave, sync) {
var stateString = JSON.stringify(tasks.getStringifyableState())
var data = {
version: 2,
Expand All @@ -20,14 +20,16 @@ window.sessionRestore = {
}

if (forceSave === true || stateString !== sessionRestore.previousState) {
fs.writeFile(sessionRestore.savePath, JSON.stringify(data), function (err) {
if (cb) {
cb(err)
}
})
if (sync === true) {
fs.writeFileSync(sessionRestore.savePath, JSON.stringify(data))
} else {
fs.writeFile(sessionRestore.savePath, JSON.stringify(data), function (err) {
if (err) {
console.warn(err)
}
})
}
sessionRestore.previousState = stateString
} else if (cb) {
cb()
}
},
restore: function () {
Expand Down Expand Up @@ -121,7 +123,7 @@ window.sessionRestore = {
} }, 200)
})
}
} catch (e) {
} catch (e) {
// an error occured while restoring the session data

console.error('restoring session failed: ', e)
Expand Down Expand Up @@ -151,10 +153,6 @@ sessionRestore.restore()

setInterval(sessionRestore.save, 30000)

ipc.on('before-quit', function () {
sessionRestore.save(function (err) {
if (!err) {
ipc.send('can-quit')
}
}, true)
})
window.onbeforeunload = function (e) {
sessionRestore.save(true, true)
}
17 changes: 0 additions & 17 deletions main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,23 +249,6 @@ function createWindowWithBounds (bounds, shouldMaximize) {
return mainWindow
}

/* allow for open tabs to be saved before quitting */
var hasSavedData = false;
app.on('before-quit', function (e) {
if (mainWindow && !hasSavedData) {
e.preventDefault();
ipc.once("can-quit", function() {
app.quit();
})
sendIPCToWindow(mainWindow, 'before-quit')
//force quit if the window isn't responsive
setTimeout(function() {
app.quit()
}, 1000)
}
hasSavedData = true;
});

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
Expand Down
5 changes: 3 additions & 2 deletions main/viewManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ function setBounds (id, bounds) {

function focusView (id) {
// empty views can't be focused because they won't propogate keyboard events correctly, see https://github.com/minbrowser/min/issues/616
if (viewMap[id].webContents.getURL() !== '' || viewMap[id].webContents.isLoading()) {
// also, make sure the view exists, since it might not if the app is shutting down
if (viewMap[id] && (viewMap[id].webContents.getURL() !== '' || viewMap[id].webContents.isLoading())) {
viewMap[id].webContents.focus()
} else {
} else if (mainWindow) {
mainWindow.webContents.focus()
}
}
Expand Down

0 comments on commit 87c587f

Please sign in to comment.