From bc8793eb56b2ce985f2e5dc0a9fd270f98fbf17a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 13:58:19 -0400 Subject: Add a context object for all calls to fetch options --- ext/bg/js/api.js | 26 ++++++++++++++------------ ext/bg/js/backend.js | 20 ++++++++++---------- ext/bg/js/search-frontend.js | 3 ++- ext/bg/js/search.js | 8 ++++++-- 4 files changed, 32 insertions(+), 25 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index b56b3449..0b80f099 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,16 +17,16 @@ */ -function apiOptionsGetSync() { +function apiOptionsGetSync(optionsContext) { return utilBackend().options; } -async function apiOptionsGet() { - return apiOptionsGetSync(); +async function apiOptionsGet(optionsContext) { + return apiOptionsGetSync(optionsContext); } -async function apiTermsFind(text) { - const options = apiOptionsGetSync(); +async function apiTermsFind(text, optionsContext) { + const options = apiOptionsGetSync(optionsContext); const translator = utilBackend().translator; const searcher = { @@ -48,14 +48,14 @@ async function apiTermsFind(text) { }; } -async function apiKanjiFind(text) { - const options = apiOptionsGetSync(); +async function apiKanjiFind(text, optionsContext) { + const options = apiOptionsGetSync(optionsContext); const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } -async function apiDefinitionAdd(definition, mode, context) { - const options = apiOptionsGetSync(); +async function apiDefinitionAdd(definition, mode, context, optionsContext) { + const options = apiOptionsGetSync(optionsContext); if (mode !== 'kanji') { await audioInject( @@ -77,14 +77,15 @@ async function apiDefinitionAdd(definition, mode, context) { return utilBackend().anki.addNote(note); } -async function apiDefinitionsAddable(definitions, modes) { +async function apiDefinitionsAddable(definitions, modes, optionsContext) { + const options = apiOptionsGetSync(optionsContext); const states = []; try { const notes = []; for (const definition of definitions) { for (const mode of modes) { - const note = await dictNoteFormat(definition, mode, apiOptionsGetSync()); + const note = await dictNoteFormat(definition, mode, options); notes.push(note); } } @@ -132,7 +133,8 @@ async function apiCommandExec(command) { }, toggle: async () => { - const options = apiOptionsGetSync(); + const optionsContext = {depth: 0}; + const options = apiOptionsGetSync(optionsContext); options.general.enable = !options.general.enable; await optionsSave(options); } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 0394c4ec..6afa9617 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -78,24 +78,24 @@ class Backend { }; const handlers = { - optionsGet: ({callback}) => { - forward(apiOptionsGet(), callback); + optionsGet: ({optionsContext, callback}) => { + forward(apiOptionsGet(optionsContext), callback); }, - kanjiFind: ({text, callback}) => { - forward(apiKanjiFind(text), callback); + kanjiFind: ({text, optionsContext, callback}) => { + forward(apiKanjiFind(text, optionsContext), callback); }, - termsFind: ({text, callback}) => { - forward(apiTermsFind(text), callback); + termsFind: ({text, optionsContext, callback}) => { + forward(apiTermsFind(text, optionsContext), callback); }, - definitionAdd: ({definition, mode, context, callback}) => { - forward(apiDefinitionAdd(definition, mode, context), callback); + definitionAdd: ({definition, mode, context, optionsContext, callback}) => { + forward(apiDefinitionAdd(definition, mode, context, optionsContext), callback); }, - definitionsAddable: ({definitions, modes, callback}) => { - forward(apiDefinitionsAddable(definitions, modes), callback); + definitionsAddable: ({definitions, modes, optionsContext, callback}) => { + forward(apiDefinitionsAddable(definitions, modes, optionsContext), callback); }, noteView: ({noteId}) => { diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 840a1ea8..df5ccf81 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -18,7 +18,8 @@ async function searchFrontendSetup() { - const options = await apiOptionsGet(); + const optionsContext = {depth: 0}; + const options = await apiOptionsGet(optionsContext); if (!options.scanning.enableOnSearchPage) { return; } const scriptSrcs = [ diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index a3382398..6bdc47d8 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -21,6 +21,10 @@ class DisplaySearch extends Display { constructor() { super($('#spinner'), $('#content')); + this.optionsContext = { + depth: 0 + }; + this.search = $('#search').click(this.onSearch.bind(this)); this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.intro = $('#intro'); @@ -46,8 +50,8 @@ class DisplaySearch extends Display { try { e.preventDefault(); this.intro.slideUp(); - const {length, definitions} = await apiTermsFind(this.query.val()); - super.termsShow(definitions, await apiOptionsGet()); + const {length, definitions} = await apiTermsFind(this.query.val(), this.optionsContext); + super.termsShow(definitions, await apiOptionsGet(this.optionsContext)); } catch (e) { this.onError(e); } -- cgit v1.2.3