Wait for windows' state to be saved before closing the app or any window

Previously, we used to save the window's state in the renderer process
`beforeunload` event handler: because of the synchronous nature of event
handlers and the asynchronous design of IndexedDB, this could
potentially not save anything if windows close fast enough to prevent
IndexedDB from committing the pending transaction containing the state.
(Ref.: https://mzl.la/2bXCXDn)

With this commit, we will intercept the `before-quit` events on
`electron.app` and the `close` event on `BrowserWindow` (which will fire
respectively before quitting the application and before closing a
window), and prevent them from performing the default action. We will
then ask each renderer process to save its state and, finally, close the
window and/or the app.
This commit is contained in:
Antonio Scandurra
2016-09-07 11:27:47 +02:00
parent db76b9fb39
commit 0f6eadcfce
6 changed files with 55 additions and 32 deletions

View File

@@ -125,11 +125,6 @@ buildAtomClient = (args, env) ->
@execute "atom.commands.dispatch(document.activeElement, '#{command}')"
.call(done)
.addCommand "simulateQuit", (done) ->
@execute -> atom.unloadEditorWindow()
.execute -> require("electron").remote.app.emit("before-quit")
.call(done)
module.exports = (args, env, fn) ->
[chromedriver, chromedriverLogs, chromedriverExit] = []
@@ -154,9 +149,7 @@ module.exports = (args, env, fn) ->
waitsFor("tests to run", (done) ->
finish = once ->
client
.simulateQuit()
.end()
client.end()
.then(-> chromedriver.kill())
.then(chromedriverExit.then(
(errorCode) ->