Update settings if a different source triggers optionsUpdate

This commit is contained in:
toasted-nutbread 2019-09-10 20:46:30 -04:00
parent 71b700cd22
commit 84bd9ff93b
3 changed files with 44 additions and 11 deletions

View File

@ -21,11 +21,11 @@ function apiOptionsGet(optionsContext) {
return utilBackend().getOptions(optionsContext);
}
async function apiOptionsSave() {
async function apiOptionsSave(source) {
const backend = utilBackend();
const options = await backend.getFullOptions();
await optionsSave(options);
backend.onOptionsUpdated();
backend.onOptionsUpdated(source);
}
async function apiTermsFind(text, optionsContext) {
@ -139,7 +139,7 @@ async function apiCommandExec(command) {
const optionsContext = {depth: 0};
const options = await apiOptionsGet(optionsContext);
options.general.enable = !options.general.enable;
await apiOptionsSave();
await apiOptionsSave('popup');
}
};

View File

@ -35,7 +35,7 @@ class Backend {
async prepare() {
await this.translator.prepare();
this.options = await optionsLoad();
this.onOptionsUpdated();
this.onOptionsUpdated('background');
if (chrome.commands !== null && typeof chrome.commands === 'object') {
chrome.commands.onCommand.addListener(this.onCommand.bind(this));
@ -52,13 +52,13 @@ class Backend {
this.isPreparedPromise = null;
}
onOptionsUpdated() {
onOptionsUpdated(source) {
this.applyOptions();
const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {}}, callback);
chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {source}}, callback);
}
});
}

View File

@ -218,7 +218,7 @@ async function onFormOptionsChanged(e) {
const optionsAnkiServerOld = options.anki.server;
await formRead(options);
await apiOptionsSave();
await settingsSaveOptions();
formUpdateVisibility(options);
try {
@ -246,11 +246,44 @@ async function onReady() {
await formWrite(options);
storageInfoInitialize();
chrome.runtime.onMessage.addListener(onMessage);
}
$(document).ready(utilAsync(onReady));
/*
* Remote options updates
*/
function settingsGetSource() {
return new Promise((resolve) => {
chrome.tabs.getCurrent((tab) => resolve(`settings${tab ? tab.id : ''}`));
});
}
async function settingsSaveOptions() {
const source = await settingsGetSource();
await apiOptionsSave(source);
}
async function onOptionsUpdate({source}) {
const thisSource = await settingsGetSource();
if (source === thisSource) { return; }
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
await formWrite(options);
}
function onMessage({action, params}) {
if (action === 'optionsUpdate') {
onOptionsUpdate(params);
}
}
/*
* Dictionary
*/
@ -395,7 +428,7 @@ async function onDictionaryPurge(e) {
const options = await apiOptionsGet(optionsContext);
options.dictionaries = {};
options.general.mainDictionary = '';
await apiOptionsSave();
await settingsSaveOptions();
await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
@ -445,7 +478,7 @@ async function onDictionaryImport(e) {
dictionaryErrorsShow(exceptions);
}
await apiOptionsSave();
await settingsSaveOptions();
await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
@ -589,7 +622,7 @@ async function onAnkiModelChanged(e) {
const options = await apiOptionsGet(optionsContext);
await formRead(options);
options.anki[tabId].fields = {};
await apiOptionsSave();
await settingsSaveOptions();
ankiSpinnerShow(true);
await ankiFieldsPopulate(element, options);
@ -609,7 +642,7 @@ async function onAnkiFieldTemplatesReset(e) {
const fieldTemplates = optionsFieldTemplates();
options.anki.fieldTemplates = fieldTemplates;
$('#field-templates').val(fieldTemplates);
await apiOptionsSave();
await settingsSaveOptions();
} catch (e) {
ankiErrorShow(e);
}