Skip to content

Commit

Permalink
Don't crash when there are no existing settings
Browse files Browse the repository at this point in the history
Let's piggyback off the default config in ./lib/main when AppEnv.config
gives us undefined.
  • Loading branch information
jmanuel1 committed Dec 19, 2020
1 parent b39ddfc commit 82b3656
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
20 changes: 17 additions & 3 deletions lib/main.es6
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand Down
32 changes: 25 additions & 7 deletions lib/settings/settings.service.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GeneralizedLabel, starredLabel } from '../models/generalized-label';
import debug from '../dev/debug';

export default class SettingsService {
injectInto(Component, React) {
Expand All @@ -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;
}
Expand All @@ -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;
}
}
}
});
}

Expand Down Expand Up @@ -90,4 +108,4 @@ export default class SettingsService {
}
};
}
}
}

0 comments on commit 82b3656

Please # to comment.