From 82b3656d4163b2d1987e46a6536d041acce4daae Mon Sep 17 00:00:00 2001 From: Jason Manuel Date: Fri, 18 Dec 2020 21:18:51 -0700 Subject: [PATCH] Don't crash when there are no existing settings Let's piggyback off the default config in ./lib/main when AppEnv.config gives us undefined. --- lib/main.es6 | 20 ++++++++++++++++--- lib/settings/settings.service.jsx | 32 ++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/main.es6 b/lib/main.es6 index fc36c7e..f996bb0 100644 --- a/lib/main.es6 +++ b/lib/main.es6 @@ -1,4 +1,17 @@ -import { Actions, ComponentRegistry, DatabaseStore, TaskQueue, Thread, WorkspaceStore, PreferencesUIStore, React } from 'mailspring-exports'; +let Actions, ComponentRegistry, DatabaseStore, TaskQueue, Thread, WorkspaceStore, PreferencesUIStore, React; +try { + const mailspringExports = require('mailspring-exports'); + Actions = mailspringExports.Actions; + ComponentRegistry = mailspringExports.ComponentRegistry; + DatabaseStore = mailspringExports.DatabaseStore; + TaskQueue = mailspringExports.TaskQueue; + Thread = mailspringExports.Thread; + WorkspaceStore = mailspringExports.WorkspaceStore; + PreferencesUIStore = mailspringExports.PreferencesUIStore; + React = mailspringExports.React; +} catch { + // do nothing +} // for async/await import 'babel-polyfill'; @@ -41,13 +54,14 @@ export const config = { // saved state using `serialize` it is provided. // export async function activate() { - settingsService = new SettingsService(AppEnv.config); + settingsService = new SettingsService(AppEnv.config, 'todoer', config); // back up the user's todo before we do anything that might damage it // TODO: Back up on every modification // TODO: Keep previous revisions (timestamp?) const todoFilePath = settingsService.todoFilePath; - await backup(todoFilePath, todoFilePath + '.backup'); + if (todoFilePath) + await backup(todoFilePath, todoFilePath + '.backup'); preferencesTab = new PreferencesUIStore.TabItem({ tabId: 'Todoer', diff --git a/lib/settings/settings.service.jsx b/lib/settings/settings.service.jsx index 3a2232b..9933c18 100644 --- a/lib/settings/settings.service.jsx +++ b/lib/settings/settings.service.jsx @@ -1,4 +1,5 @@ import { GeneralizedLabel, starredLabel } from '../models/generalized-label'; +import debug from '../dev/debug'; export default class SettingsService { injectInto(Component, React) { @@ -13,8 +14,8 @@ export default class SettingsService { } } - constructor(config, keypath = 'todoer') { - console.log('CURRENT INSTANCE', this.constructor._instance); + constructor(config, keypath = 'todoer', defaultConfig) { + debug('CURRENT INSTANCE', this.constructor._instance); if (this.constructor._instance !== undefined) { return this.constructor._instance; } @@ -24,10 +25,27 @@ export default class SettingsService { this._subscribers = []; this._configSubscription = config.observe( keypath, - ({todoFilePath: path, emailLabel, useStarsForLabel}) => { - path && (this.todoFilePath = path); - emailLabel && (this.emailLabel = new GeneralizedLabel(emailLabel)); - useStarsForLabel && (this.emailLabel = starredLabel); + settings => { + if (settings) { + settings.todoFilePath && (this.todoFilePath = settings.todoFilePath); + settings.emailLabel && (this.emailLabel = new GeneralizedLabel(settings.emailLabel)); + settings.useStarsForLabel && (this.emailLabel = starredLabel); + } + // We might try to access the config before Mailspring has set it up. So, + // let's take the defaults from ./main. + if (defaultConfig) { + if (!settings.todoFilePath) { + settings.todoFilePath = defaultConfig.todoFilePath.default; + } + if (!settings.emailLabel) { + settings.emailLabel = new GeneralizedLabel(defaultConfig.emailLabel.default); + } + if (settings.useStarsForLabel === undefined) { + if (defaultConfig.useStarsForLabel.default) { + settings.emailLabel = starredLabel; + } + } + } }); } @@ -90,4 +108,4 @@ export default class SettingsService { } }; } -} \ No newline at end of file +}