Add a context object for all calls to fetch options

This commit is contained in:
toasted-nutbread 2019-09-07 13:58:19 -04:00
parent 99ca60d4c1
commit bc8793eb56
9 changed files with 62 additions and 44 deletions

View File

@ -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);
}

View File

@ -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}) => {

View File

@ -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 = [

View File

@ -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);
}

View File

@ -17,24 +17,24 @@
*/
function apiOptionsGet() {
return utilInvoke('optionsGet');
function apiOptionsGet(optionsContext) {
return utilInvoke('optionsGet', {optionsContext});
}
function apiTermsFind(text) {
return utilInvoke('termsFind', {text});
function apiTermsFind(text, optionsContext) {
return utilInvoke('termsFind', {text, optionsContext});
}
function apiKanjiFind(text) {
return utilInvoke('kanjiFind', {text});
function apiKanjiFind(text, optionsContext) {
return utilInvoke('kanjiFind', {text, optionsContext});
}
function apiDefinitionAdd(definition, mode, context) {
return utilInvoke('definitionAdd', {definition, mode, context});
function apiDefinitionAdd(definition, mode, context, optionsContext) {
return utilInvoke('definitionAdd', {definition, mode, context, optionsContext});
}
function apiDefinitionsAddable(definitions, modes) {
return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null);
function apiDefinitionsAddable(definitions, modes, optionsContext) {
return utilInvoke('definitionsAddable', {definitions, modes, optionsContext}).catch(() => null);
}
function apiNoteView(noteId) {

View File

@ -23,6 +23,10 @@ class DisplayFloat extends Display {
this.autoPlayAudioTimer = null;
this.styleNode = null;
this.optionsContext = {
depth: 0
};
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
$(window).on('message', utilAsync(this.onMessage.bind(this)));
@ -75,6 +79,7 @@ class DisplayFloat extends Display {
},
popupNestedInitialize: ({id, depth, parentFrameId}) => {
this.optionsContext.depth = depth;
popupNestedInitialize(id, depth, parentFrameId);
}
};

View File

@ -28,6 +28,10 @@ class Frontend {
this.options = null;
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
this.optionsContext = {
depth: popup.depth
};
this.primaryTouchIdentifier = null;
this.contextMenuChecking = false;
this.contextMenuPrevent = false;
@ -50,7 +54,7 @@ class Frontend {
async prepare() {
try {
this.options = await apiOptionsGet();
this.options = await apiOptionsGet(this.optionsContext);
window.addEventListener('message', this.onFrameMessage.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this));
@ -282,7 +286,7 @@ class Frontend {
}
async updateOptions() {
this.options = await apiOptionsGet();
this.options = await apiOptionsGet(this.optionsContext);
if (!this.options.enable) {
this.searchClear();
}
@ -351,7 +355,7 @@ class Frontend {
return;
}
const {definitions, length} = await apiTermsFind(searchText);
const {definitions, length} = await apiTermsFind(searchText, this.optionsContext);
if (definitions.length === 0) {
return false;
}
@ -384,7 +388,7 @@ class Frontend {
return;
}
const definitions = await apiKanjiFind(searchText);
const definitions = await apiKanjiFind(searchText, this.optionsContext);
if (definitions.length === 0) {
return false;
}

View File

@ -25,7 +25,8 @@ async function popupNestedInitialize(id, depth, parentFrameId) {
}
popupNestedInitialized = true;
const options = await apiOptionsGet();
const optionsContext = {depth};
const options = await apiOptionsGet(optionsContext);
const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth;
if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) {

View File

@ -27,6 +27,7 @@ class Display {
this.sequence = 0;
this.index = 0;
this.audioCache = {};
this.optionsContext = {};
this.dependencies = {};
@ -66,7 +67,7 @@ class Display {
context.source.source = this.context.source;
}
const kanjiDefs = await apiKanjiFind(link.text());
const kanjiDefs = await apiKanjiFind(link.text(), this.optionsContext);
this.kanjiShow(kanjiDefs, this.options, context);
} catch (e) {
this.onError(e);
@ -89,7 +90,7 @@ class Display {
try {
textSource.setEndOffset(this.options.scanning.length);
({definitions, length} = await apiTermsFind(textSource.text()));
({definitions, length} = await apiTermsFind(textSource.text(), this.optionsContext));
if (definitions.length === 0) {
return false;
}
@ -379,7 +380,7 @@ class Display {
async adderButtonUpdate(modes, sequence) {
try {
const states = await apiDefinitionsAddable(this.definitions, modes);
const states = await apiDefinitionsAddable(this.definitions, modes, this.optionsContext);
if (!states || sequence !== this.sequence) {
return;
}
@ -453,7 +454,7 @@ class Display {
}
}
const noteId = await apiDefinitionAdd(definition, mode, context);
const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext);
if (noteId) {
const index = this.definitions.indexOf(definition);
Display.adderButtonFind(index, mode).addClass('disabled');