Use apiOptionsSave instead of optionsSave

This commit is contained in:
toasted-nutbread 2019-09-07 16:15:18 -04:00
parent 4686a31a0a
commit 05ce350792
4 changed files with 29 additions and 10 deletions

View File

@ -21,6 +21,13 @@ function apiOptionsGet(optionsContext) {
return utilBackend().getOptions(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) { async function apiTermsFind(text, optionsContext) {
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
const translator = utilBackend().translator; const translator = utilBackend().translator;
@ -132,7 +139,7 @@ async function apiCommandExec(command) {
const optionsContext = {depth: 0}; const optionsContext = {depth: 0};
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
options.general.enable = !options.general.enable; options.general.enable = !options.general.enable;
await optionsSave(options); await apiOptionsSave();
} }
}; };

View File

@ -150,6 +150,13 @@ class Backend {
this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); 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) { async getOptions(optionsContext) {
if (this.isPreparedPromise !== null) { if (this.isPreparedPromise !== null) {
await this.isPreparedPromise; await this.isPreparedPromise;

View File

@ -343,9 +343,14 @@ function optionsLoad() {
} }
function optionsSave(options) { function optionsSave(options) {
return new Promise((resolve) => { return new Promise((resolve, reject) => {
chrome.storage.local.set({options: JSON.stringify(options)}, resolve); chrome.storage.local.set({options: JSON.stringify(options)}, () => {
}).then(() => { const error = chrome.runtime.lastError;
utilBackend().onOptionsUpdated(options); if (error) {
reject(error);
} else {
resolve();
}
});
}); });
} }

View File

@ -148,7 +148,7 @@ async function onFormOptionsChanged(e) {
const optionsAnkiServerOld = options.anki.server; const optionsAnkiServerOld = options.anki.server;
await formRead(options); await formRead(options);
await optionsSave(options); await apiOptionsSave();
formUpdateVisibility(options); formUpdateVisibility(options);
try { try {
@ -385,7 +385,7 @@ async function onDictionaryPurge(e) {
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
options.dictionaries = {}; options.dictionaries = {};
options.general.mainDictionary = ''; options.general.mainDictionary = '';
await optionsSave(options); await apiOptionsSave();
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options); await formMainDictionaryOptionsPopulate(options);
@ -435,7 +435,7 @@ async function onDictionaryImport(e) {
dictionaryErrorsShow(exceptions); dictionaryErrorsShow(exceptions);
} }
await optionsSave(options); await apiOptionsSave();
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options); await formMainDictionaryOptionsPopulate(options);
@ -579,7 +579,7 @@ async function onAnkiModelChanged(e) {
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
await formRead(options); await formRead(options);
options.anki[tabId].fields = {}; options.anki[tabId].fields = {};
await optionsSave(options); await apiOptionsSave();
ankiSpinnerShow(true); ankiSpinnerShow(true);
await ankiFieldsPopulate(element, options); await ankiFieldsPopulate(element, options);
@ -599,7 +599,7 @@ async function onAnkiFieldTemplatesReset(e) {
const fieldTemplates = optionsFieldTemplates(); const fieldTemplates = optionsFieldTemplates();
options.anki.fieldTemplates = fieldTemplates; options.anki.fieldTemplates = fieldTemplates;
$('#field-templates').val(fieldTemplates); $('#field-templates').val(fieldTemplates);
await optionsSave(options); await apiOptionsSave();
} catch (e) { } catch (e) {
ankiErrorShow(e); ankiErrorShow(e);
} }