Simplify logic for how option updates are propagated

This commit is contained in:
toasted-nutbread 2019-09-06 21:23:00 -04:00
parent 7db2c66105
commit eb98dfb1a8
3 changed files with 6 additions and 13 deletions

View File

@ -17,10 +17,6 @@
*/ */
async function apiOptionsSet(options) {
utilBackend().onOptionsUpdated(options);
}
function apiOptionsGetSync() { function apiOptionsGetSync() {
return utilBackend().options; return utilBackend().options;
} }

View File

@ -28,7 +28,7 @@ class Backend {
async prepare() { async prepare() {
await this.translator.prepare(); await this.translator.prepare();
await apiOptionsSet(await optionsLoad()); this.onOptionsUpdated(await optionsLoad());
if (chrome.commands !== null && typeof chrome.commands === 'object') { if (chrome.commands !== null && typeof chrome.commands === 'object') {
chrome.commands.onCommand.addListener(this.onCommand.bind(this)); chrome.commands.onCommand.addListener(this.onCommand.bind(this));
@ -41,7 +41,8 @@ class Backend {
} }
onOptionsUpdated(options) { onOptionsUpdated(options) {
this.options = utilIsolate(options); options = utilIsolate(options);
this.options = options;
if (!options.general.enable) { if (!options.general.enable) {
this.setExtensionBadgeBackgroundColor('#555555'); this.setExtensionBadgeBackgroundColor('#555555');
@ -53,11 +54,7 @@ class Backend {
this.setExtensionBadgeText(''); this.setExtensionBadgeText('');
} }
if (options.anki.enable) { this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull();
this.anki = new AnkiConnect(options.anki.server);
} else {
this.anki = new AnkiNull();
}
const callback = () => this.checkLastError(chrome.runtime.lastError); const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.query({}, tabs => { chrome.tabs.query({}, tabs => {

View File

@ -346,6 +346,6 @@ function optionsSave(options) {
return new Promise((resolve) => { return new Promise((resolve) => {
chrome.storage.local.set({options: JSON.stringify(options)}, resolve); chrome.storage.local.set({options: JSON.stringify(options)}, resolve);
}).then(() => { }).then(() => {
apiOptionsSet(options); utilBackend().onOptionsUpdated(options);
}); });
} }