Fix bug where removal of last account would not clear database properly

This commit is contained in:
Ben Gotow 2018-08-05 15:44:40 -07:00
parent 732322893c
commit 0b9ce9efe5
4 changed files with 24 additions and 60 deletions

View file

@ -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() {

View file

@ -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();

View file

@ -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(() => {

View file

@ -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) {