diff --git a/app/index.js b/app/index.js index 4515ce2aeed..e2c5cb98bc0 100644 --- a/app/index.js +++ b/app/index.js @@ -64,6 +64,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') if (!process.env.BRAVE_USER_DATA_DIR && ['development', 'test'].includes(process.env.NODE_ENV)) { process.env.BRAVE_USER_DATA_DIR = path.join(app.getPath('appData'), app.getName() + '-' + process.env.NODE_ENV) @@ -94,6 +95,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. */ @@ -173,24 +178,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() + } + }) } /** @@ -288,7 +303,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 f02cba5b29f..4c21e35d9dd 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.58", "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",