summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-07 16:15:18 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-10 20:09:33 -0400
commit05ce350792fd60c1721bff4d0fb971e2bec24818 (patch)
treee9f55d8dd038fd3beaacae98a2a4c748aff1400b
parent4686a31a0a5d30a1b01e3cd2b689d1950c79c940 (diff)
Use apiOptionsSave instead of optionsSave
-rw-r--r--ext/bg/js/api.js9
-rw-r--r--ext/bg/js/backend.js7
-rw-r--r--ext/bg/js/options.js13
-rw-r--r--ext/bg/js/settings.js10
4 files changed, 29 insertions, 10 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 53e25348..a50353c1 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -21,6 +21,13 @@ function apiOptionsGet(optionsContext) {
return utilBackend().getOptions(optionsContext);
}
+async function apiOptionsSave() {
+ const backend = utilBackend();
+ const options = await backend.getFullOptions();
+ await optionsSave(options);
+ backend.onOptionsUpdated(options);
+}
+
async function apiTermsFind(text, optionsContext) {
const options = await apiOptionsGet(optionsContext);
const translator = utilBackend().translator;
@@ -132,7 +139,7 @@ async function apiCommandExec(command) {
const optionsContext = {depth: 0};
const options = await apiOptionsGet(optionsContext);
options.general.enable = !options.general.enable;
- await optionsSave(options);
+ await apiOptionsSave();
}
};
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 6dcf8e4d..1f00f788 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -150,6 +150,13 @@ class Backend {
this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull();
}
+ async getFullOptions() {
+ if (this.isPreparedPromise !== null) {
+ await this.isPreparedPromise;
+ }
+ return this.options;
+ }
+
async getOptions(optionsContext) {
if (this.isPreparedPromise !== null) {
await this.isPreparedPromise;
diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js
index 69c662e6..ea8f56d5 100644
--- a/ext/bg/js/options.js
+++ b/ext/bg/js/options.js
@@ -343,9 +343,14 @@ function optionsLoad() {
}
function optionsSave(options) {
- return new Promise((resolve) => {
- chrome.storage.local.set({options: JSON.stringify(options)}, resolve);
- }).then(() => {
- utilBackend().onOptionsUpdated(options);
+ return new Promise((resolve, reject) => {
+ chrome.storage.local.set({options: JSON.stringify(options)}, () => {
+ const error = chrome.runtime.lastError;
+ if (error) {
+ reject(error);
+ } else {
+ resolve();
+ }
+ });
});
}
diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js
index 1c9198dd..e5786804 100644
--- a/ext/bg/js/settings.js
+++ b/ext/bg/js/settings.js
@@ -148,7 +148,7 @@ async function onFormOptionsChanged(e) {
const optionsAnkiServerOld = options.anki.server;
await formRead(options);
- await optionsSave(options);
+ await apiOptionsSave();
formUpdateVisibility(options);
try {
@@ -385,7 +385,7 @@ async function onDictionaryPurge(e) {
const options = await apiOptionsGet(optionsContext);
options.dictionaries = {};
options.general.mainDictionary = '';
- await optionsSave(options);
+ await apiOptionsSave();
await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
@@ -435,7 +435,7 @@ async function onDictionaryImport(e) {
dictionaryErrorsShow(exceptions);
}
- await optionsSave(options);
+ await apiOptionsSave();
await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
@@ -579,7 +579,7 @@ async function onAnkiModelChanged(e) {
const options = await apiOptionsGet(optionsContext);
await formRead(options);
options.anki[tabId].fields = {};
- await optionsSave(options);
+ await apiOptionsSave();
ankiSpinnerShow(true);
await ankiFieldsPopulate(element, options);
@@ -599,7 +599,7 @@ async function onAnkiFieldTemplatesReset(e) {
const fieldTemplates = optionsFieldTemplates();
options.anki.fieldTemplates = fieldTemplates;
$('#field-templates').val(fieldTemplates);
- await optionsSave(options);
+ await apiOptionsSave();
} catch (e) {
ankiErrorShow(e);
}