From 0b9ce9efe598852ed5bfeb7c2859f0868882de1c Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Sun, 5 Aug 2018 15:44:40 -0700 Subject: [PATCH] Fix bug where removal of last account would not clear database properly --- .../lib/tabs/preferences-general.jsx | 11 +--- app/src/browser/application.es6 | 64 +++++-------------- app/src/flux/stores/account-store.es6 | 4 +- app/src/flux/stores/database-store.es6 | 5 +- 4 files changed, 24 insertions(+), 60 deletions(-) diff --git a/app/internal_packages/preferences/lib/tabs/preferences-general.jsx b/app/internal_packages/preferences/lib/tabs/preferences-general.jsx index cc346abb8..acac70fe8 100644 --- a/app/internal_packages/preferences/lib/tabs/preferences-general.jsx +++ b/app/internal_packages/preferences/lib/tabs/preferences-general.jsx @@ -34,15 +34,8 @@ class PreferencesGeneral extends React.Component { }; _onResetEmailCache = () => { - rimraf(path.join(AppEnv.getConfigDirPath(), 'edgehill.*'), err => { - if (err) { - return AppEnv.showErrorDialog({ - title: `Could not delete the mail database.`, - message: `Please quit Mailspring and delete the "edgehill.db" file in ${AppEnv.getConfigDirPath()} manually.\n\n${err.toString()}`, - }); - } - this._onReboot(); - }); + const ipc = require('electron').ipcRenderer; + ipc.send('command', 'application:reset-database', {}); }; render() { diff --git a/app/src/browser/application.es6 b/app/src/browser/application.es6 index 737bd6204..d16001c6e 100644 --- a/app/src/browser/application.es6 +++ b/app/src/browser/application.es6 @@ -248,26 +248,25 @@ export default class Application extends EventEmitter { } } - _relaunchToInitialWindows = ({ resetConfig, resetDatabase } = {}) => { - // This will re-fetch the NylasID to update the feed url - this.autoUpdateManager.updateFeedURL(); - this.windowManager.destroyAllWindows(); + _resetDatabaseAndRelaunch = ({ errorMessage } = {}) => { + if (this._resettingAndRelaunching) return; + this._resettingAndRelaunching = true; - let fn = callback => callback(); - if (resetDatabase) { - fn = this._deleteDatabase; + if (errorMessage) { + dialog.showMessageBox({ + type: 'warning', + buttons: ['Okay'], + message: `We encountered a problem with your local email database. We will now attempt to rebuild it.`, + detail: errorMessage, + }); } - fn(async () => { - if (resetDatabase) { - // TODO BG Reset Database via helpers - } - if (resetConfig) { - this.config.set('nylas', null); - this.config.set('edgehill', null); - } - this.openWindowsForTokenState(); - }); + const done = () => { + app.relaunch(); + app.quit(); + }; + this.windowManager.destroyAllWindows(); + this._deleteDatabase(done); }; _deleteDatabase = callback => { @@ -276,35 +275,6 @@ export default class Application extends EventEmitter { this.deleteFileWithRetry(path.join(this.configDirPath, 'edgehill.db-shm')); }; - rebuildDatabase = ({ showErrorDialog = true, detail = '' } = {}) => { - if (this._rebuildingDatabase) { - return; - } - this._rebuildingDatabase = true; - if (showErrorDialog) { - dialog.showMessageBox({ - type: 'warning', - buttons: ['Okay'], - message: `We encountered a problem with your local email database. We will now attempt to rebuild it.`, - detail, - }); - } - - // We need to set a timeout so `rebuildDatabases` immediately returns. - // If we don't immediately return the main window caller wants to wait - // for this function to finish so it can get the return value via ipc. - // Unfortunately since this function destroys the main window - // immediately, an error will be thrown. - setTimeout(() => { - this.windowManager.destroyAllWindows(); - this._deleteDatabase(async () => { - // TODO BG Invoke db helepr - this._rebuildingDatabase = false; - this.openWindowsForTokenState(); - }); - }, 0); - }; - // Registers basic application commands, non-idempotent. // Note: If these events are triggered while an application window is open, the window // needs to manually bubble them up to the Application instance via IPC or they won't be @@ -342,7 +312,7 @@ export default class Application extends EventEmitter { ); }); - this.on('application:relaunch-to-initial-windows', this._relaunchToInitialWindows); + this.on('application:reset-database', this._resetDatabaseAndRelaunch); this.on('application:quit', () => { app.quit(); diff --git a/app/src/flux/stores/account-store.es6 b/app/src/flux/stores/account-store.es6 index 53a951ec4..4a139aa6c 100644 --- a/app/src/flux/stores/account-store.es6 +++ b/app/src/flux/stores/account-store.es6 @@ -194,9 +194,7 @@ class AccountStore extends MailspringStore { if (remainingAccounts.length === 0) { // Clear everything and logout const ipc = require('electron').ipcRenderer; - ipc.send('command', 'application:relaunch-to-initial-windows', { - resetDatabase: true, - }); + ipc.send('command', 'application:reset-database', {}); } else { // Clear the cached data for the account and reset secrets once that has completed AppEnv.mailsyncBridge.resetCacheForAccount(account, { silent: true }).then(() => { diff --git a/app/src/flux/stores/database-store.es6 b/app/src/flux/stores/database-store.es6 index 978a1d94d..6c679af5f 100644 --- a/app/src/flux/stores/database-store.es6 +++ b/app/src/flux/stores/database-store.es6 @@ -38,7 +38,10 @@ function handleUnrecoverableDatabaseError( if (!app) { throw new Error('handleUnrecoverableDatabaseError: `app` is not ready!'); } - app.rebuildDatabase({ detail: err.toString() }); + const ipc = require('electron').ipcRenderer; + ipc.send('command', 'application:reset-database', { + errorMessage: err.toString(), + }); } async function openDatabase(dbPath) {