From 7a3600ab308f8172fe78572822c02cd34fb504c5 Mon Sep 17 00:00:00 2001 From: bridiver Date: Wed, 31 Aug 2016 21:21:22 -0700 Subject: [PATCH] serialize app state saves fixes #3543 --- app/index.js | 49 ++++++++++++++++++++++++++++++++----------------- package.json | 3 ++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app/index.js b/app/index.js index cdb776870fa..520213c00d1 100644 --- a/app/index.js +++ b/app/index.js @@ -63,6 +63,7 @@ const flash = require('../js/flash') const contentSettings = require('../js/state/contentSettings') const privacy = require('../js/state/privacy') const basicAuth = require('./browser/basicAuth') +const async = require('async') // Used to collect the per window state when shutting down the application let perWindowState = [] @@ -85,6 +86,10 @@ const prefsRestartLastValue = {} const unsafeTestMasterKey = 'c66af15fc6555ebecf7cee3a5b82c108fd3cb4b587ab0b299d28e39c79ecc708' +const sessionStoreQueue = async.queue((task, callback) => { + task(callback) +}, 1) + /** * Gets the master key for encrypting login credentials from the OS keyring. */ @@ -164,24 +169,34 @@ const saveIfAllCollected = (forceSave) => { } } } + sessionStoreQueue.push(saveAppState.bind(null, appState)) + } +} - const logSaveAppStateError = (e) => { - console.error('Error saving app state: ', e) - } - SessionStore.saveAppState(appState, shuttingDown).catch(logSaveAppStateError).then(() => { - if (shuttingDown) { - sessionStateStoreCompleteOnQuit = true - // If there's an update to apply, then do it here. - // Otherwise just quit. - if (appState.updates && (appState.updates.status === UpdateStatus.UPDATE_APPLYING_NO_RESTART || - appState.updates.status === UpdateStatus.UPDATE_APPLYING_RESTART)) { - Updater.quitAndInstall() - } else { - app.quit() - } +const logSaveAppStateError = (e) => { + console.error('Error saving app state: ', e) +} + +const saveAppState = (appState, cb) => { + SessionStore.saveAppState(appState, shuttingDown).catch((e) => { + logSaveAppStateError(e) + cb() + }).then(() => { + if (shuttingDown) { + sessionStateStoreCompleteOnQuit = true + // If there's an update to apply, then do it here. + // Otherwise just quit. + if (appState.updates && (appState.updates.status === UpdateStatus.UPDATE_APPLYING_NO_RESTART || + appState.updates.status === UpdateStatus.UPDATE_APPLYING_RESTART)) { + Updater.quitAndInstall() + } else { + app.quit() } - }) - } + // no callback here because we don't want to get a partial write during shutdown + } else { + cb() + } + }) } /** @@ -279,7 +294,7 @@ app.on('ready', () => { e.preventDefault() - clearTimeout(initiateSessionStateSave) + clearInterval(initiateSessionStateSave) initiateSessionStateSave(true) // Just in case a window is not responsive, we don't want to wait forever. diff --git a/package.json b/package.json index e1b73f26a10..b72bcef1343 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "dependencies": { "abp-filter-parser-cpp": "1.1.x", "acorn": "3.2.0", + "async": "^2.0.1", "electron-localshortcut": "^0.6.0", "electron-prebuilt": "brave/electron-prebuilt", "electron-squirrel-startup": "^1.0.0", @@ -92,8 +93,8 @@ "ledger-client": "^0.8.55", "ledger-publisher": "^0.8.57", "lru_cache": "^1.0.0", - "random-lib": "2.1.0", "qr-image": "^3.1.0", + "random-lib": "2.1.0", "react": "^15.0.1", "react-dom": "^15.0.1", "react-stickynode": "1.1.x",