simplify backend prepare

This commit is contained in:
siikamiika 2020-03-01 23:06:37 +02:00
parent 46fee07d36
commit 2abf46b6fa
2 changed files with 24 additions and 47 deletions

View File

@ -39,9 +39,6 @@ class Backend {
url: window.location.href url: window.location.href
}; };
this.isPreparedResolve = null;
this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve));
this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target'); this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target');
this.popupWindow = null; this.popupWindow = null;
@ -110,15 +107,11 @@ class Backend {
} }
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
const options = this.getOptionsSync(this.optionsContext); const options = this.getOptions(this.optionsContext);
if (options.general.showGuide) { if (options.general.showGuide) {
chrome.tabs.create({url: chrome.runtime.getURL('/bg/guide.html')}); chrome.tabs.create({url: chrome.runtime.getURL('/bg/guide.html')});
} }
this.isPreparedResolve();
this.isPreparedResolve = null;
this.isPreparedPromise = null;
this.clipboardMonitor.onClipboardText = this._onClipboardText.bind(this); this.clipboardMonitor.onClipboardText = this._onClipboardText.bind(this);
} }
@ -160,7 +153,7 @@ class Backend {
} }
applyOptions() { applyOptions() {
const options = this.getOptionsSync(this.optionsContext); const options = this.getOptions(this.optionsContext);
if (!options.general.enable) { if (!options.general.enable) {
this.setExtensionBadgeBackgroundColor('#555555'); this.setExtensionBadgeBackgroundColor('#555555');
this.setExtensionBadgeText('off'); this.setExtensionBadgeText('off');
@ -186,24 +179,15 @@ class Backend {
} }
} }
async getOptionsSchema() { getOptionsSchema() {
if (this.isPreparedPromise !== null) {
await this.isPreparedPromise;
}
return this.optionsSchema; return this.optionsSchema;
} }
async getFullOptions() { getFullOptions() {
if (this.isPreparedPromise !== null) {
await this.isPreparedPromise;
}
return this.options; return this.options;
} }
async setFullOptions(options) { setFullOptions(options) {
if (this.isPreparedPromise !== null) {
await this.isPreparedPromise;
}
try { try {
this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, utilIsolate(options)); this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, utilIsolate(options));
} catch (e) { } catch (e) {
@ -212,18 +196,11 @@ class Backend {
} }
} }
async getOptions(optionsContext) { getOptions(optionsContext) {
if (this.isPreparedPromise !== null) { return this.getProfile(optionsContext).options;
await this.isPreparedPromise;
}
return this.getOptionsSync(optionsContext);
} }
getOptionsSync(optionsContext) { getProfile(optionsContext) {
return this.getProfileSync(optionsContext).options;
}
getProfileSync(optionsContext) {
const profiles = this.options.profiles; const profiles = this.options.profiles;
if (typeof optionsContext.index === 'number') { if (typeof optionsContext.index === 'number') {
return profiles[optionsContext.index]; return profiles[optionsContext.index];
@ -290,20 +267,20 @@ class Backend {
// Message handlers // Message handlers
_onApiOptionsSchemaGet() { async _onApiOptionsSchemaGet() {
return this.getOptionsSchema(); return this.getOptionsSchema();
} }
_onApiOptionsGet({optionsContext}) { async _onApiOptionsGet({optionsContext}) {
return this.getOptions(optionsContext); return this.getOptions(optionsContext);
} }
_onApiOptionsGetFull() { async _onApiOptionsGetFull() {
return this.getFullOptions(); return this.getFullOptions();
} }
async _onApiOptionsSet({changedOptions, optionsContext, source}) { async _onApiOptionsSet({changedOptions, optionsContext, source}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
function getValuePaths(obj) { function getValuePaths(obj) {
const valuePaths = []; const valuePaths = [];
@ -343,20 +320,20 @@ class Backend {
} }
async _onApiOptionsSave({source}) { async _onApiOptionsSave({source}) {
const options = await this.getFullOptions(); const options = this.getFullOptions();
await optionsSave(options); await optionsSave(options);
this.onOptionsUpdated(source); this.onOptionsUpdated(source);
} }
async _onApiKanjiFind({text, optionsContext}) { async _onApiKanjiFind({text, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
const definitions = await this.translator.findKanji(text, options); const definitions = await this.translator.findKanji(text, options);
definitions.splice(options.general.maxResults); definitions.splice(options.general.maxResults);
return definitions; return definitions;
} }
async _onApiTermsFind({text, details, optionsContext}) { async _onApiTermsFind({text, details, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
const mode = options.general.resultOutputMode; const mode = options.general.resultOutputMode;
const [definitions, length] = await this.translator.findTerms(mode, text, details, options); const [definitions, length] = await this.translator.findTerms(mode, text, details, options);
definitions.splice(options.general.maxResults); definitions.splice(options.general.maxResults);
@ -364,7 +341,7 @@ class Backend {
} }
async _onApiTextParse({text, optionsContext}) { async _onApiTextParse({text, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
const results = []; const results = [];
while (text.length > 0) { while (text.length > 0) {
const term = []; const term = [];
@ -394,7 +371,7 @@ class Backend {
} }
async _onApiTextParseMecab({text, optionsContext}) { async _onApiTextParseMecab({text, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
const results = []; const results = [];
const rawResults = await this.mecab.parseText(text); const rawResults = await this.mecab.parseText(text);
for (const [mecabName, parsedLines] of Object.entries(rawResults)) { for (const [mecabName, parsedLines] of Object.entries(rawResults)) {
@ -425,7 +402,7 @@ class Backend {
} }
async _onApiDefinitionAdd({definition, mode, context, optionsContext}) { async _onApiDefinitionAdd({definition, mode, context, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
const templates = this.defaultAnkiFieldTemplates; const templates = this.defaultAnkiFieldTemplates;
if (mode !== 'kanji') { if (mode !== 'kanji') {
@ -450,7 +427,7 @@ class Backend {
} }
async _onApiDefinitionsAddable({definitions, modes, optionsContext}) { async _onApiDefinitionsAddable({definitions, modes, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
const templates = this.defaultAnkiFieldTemplates; const templates = this.defaultAnkiFieldTemplates;
const states = []; const states = [];
@ -497,7 +474,7 @@ class Backend {
} }
async _onApiNoteView({noteId}) { async _onApiNoteView({noteId}) {
return this.anki.guiBrowse(`nid:${noteId}`); return await this.anki.guiBrowse(`nid:${noteId}`);
} }
async _onApiTemplateRender({template, data}) { async _onApiTemplateRender({template, data}) {
@ -509,7 +486,7 @@ class Backend {
} }
async _onApiAudioGetUrl({definition, source, optionsContext}) { async _onApiAudioGetUrl({definition, source, optionsContext}) {
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
return await audioGetUrl(definition, source, options); return await audioGetUrl(definition, source, options);
} }
@ -668,7 +645,7 @@ class Backend {
async _onCommandSearch(params) { async _onCommandSearch(params) {
const {mode='existingOrNewTab', query} = params || {}; const {mode='existingOrNewTab', query} = params || {};
const options = await this.getOptions(this.optionsContext); const options = this.getOptions(this.optionsContext);
const {popupWidth, popupHeight} = options.general; const {popupWidth, popupHeight} = options.general;
const baseUrl = chrome.runtime.getURL('/bg/search.html'); const baseUrl = chrome.runtime.getURL('/bg/search.html');
@ -752,7 +729,7 @@ class Backend {
}; };
const source = 'popup'; const source = 'popup';
const options = await this.getOptions(optionsContext); const options = this.getOptions(optionsContext);
options.general.enable = !options.general.enable; options.general.enable = !options.general.enable;
await this._onApiOptionsSave({source}); await this._onApiOptionsSave({source});
} }

View File

@ -120,7 +120,7 @@ async function _onSettingsExportClick() {
// Importing // Importing
async function _settingsImportSetOptionsFull(optionsFull) { async function _settingsImportSetOptionsFull(optionsFull) {
return utilIsolate(await utilBackend().setFullOptions( return utilIsolate(utilBackend().setFullOptions(
utilBackgroundIsolate(optionsFull) utilBackgroundIsolate(optionsFull)
)); ));
} }