Merge pull request #358 from toasted-nutbread/general-refactoring

General refactoring
This commit is contained in:
toasted-nutbread 2020-02-14 21:25:25 -05:00 committed by GitHub
commit 853faaf48c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 56 deletions

View File

@ -17,26 +17,18 @@
*/ */
function apiTemplateRender(template, data, dynamic) { function apiTemplateRender(template, data) {
return _apiInvoke('templateRender', {data, template, dynamic}); return _apiInvoke('templateRender', {data, template});
} }
function apiAudioGetUrl(definition, source, optionsContext) { function apiAudioGetUrl(definition, source, optionsContext) {
return _apiInvoke('audioGetUrl', {definition, source, optionsContext}); return _apiInvoke('audioGetUrl', {definition, source, optionsContext});
} }
function apiGetDisplayTemplatesHtml() {
return _apiInvoke('getDisplayTemplatesHtml');
}
function apiClipboardGet() { function apiClipboardGet() {
return _apiInvoke('clipboardGet'); return _apiInvoke('clipboardGet');
} }
function apiGetQueryParserTemplatesHtml() {
return _apiInvoke('getQueryParserTemplatesHtml');
}
function _apiInvoke(action, params={}) { function _apiInvoke(action, params={}) {
const data = {action, params}; const data = {action, params};
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -18,7 +18,7 @@
/*global optionsSave, utilIsolate /*global optionsSave, utilIsolate
conditionsTestValue, profileConditionsDescriptor, profileOptionsGetDefaultFieldTemplates conditionsTestValue, profileConditionsDescriptor, profileOptionsGetDefaultFieldTemplates
handlebarsRenderDynamic, handlebarsRenderStatic handlebarsRenderDynamic
requestText, requestJson, optionsLoad requestText, requestJson, optionsLoad
dictConfigured, dictTermsSort, dictEnabledSet, dictNoteFormat dictConfigured, dictTermsSort, dictEnabledSet, dictNoteFormat
audioGetUrl, audioInject audioGetUrl, audioInject
@ -459,12 +459,8 @@ class Backend {
return this.anki.guiBrowse(`nid:${noteId}`); return this.anki.guiBrowse(`nid:${noteId}`);
} }
async _onApiTemplateRender({template, data, dynamic}) { async _onApiTemplateRender({template, data}) {
return ( return handlebarsRenderDynamic(template, data);
dynamic ?
handlebarsRenderDynamic(template, data) :
handlebarsRenderStatic(template, data)
);
} }
async _onApiCommandExec({command, params}) { async _onApiCommandExec({command, params}) {

View File

@ -335,7 +335,7 @@ async function dictFieldFormat(field, definition, mode, options, templates, exce
} }
data.marker = marker; data.marker = marker;
try { try {
return await apiTemplateRender(templates, data, true); return await apiTemplateRender(templates, data);
} catch (e) { } catch (e) {
if (exceptions) { exceptions.push(e); } if (exceptions) { exceptions.push(e); }
return `{${marker}-render-error}`; return `{${marker}-render-error}`;

View File

@ -135,11 +135,6 @@ function handlebarsRegisterHelpers() {
} }
} }
function handlebarsRenderStatic(name, data) {
handlebarsRegisterHelpers();
return Handlebars.templates[name](data).trim();
}
function handlebarsRenderDynamic(template, data) { function handlebarsRenderDynamic(template, data) {
handlebarsRegisterHelpers(); handlebarsRegisterHelpers();
const cache = handlebarsRenderDynamic._cache; const cache = handlebarsRenderDynamic._cache;

View File

@ -27,7 +27,7 @@ class Translator {
constructor() { constructor() {
this.database = null; this.database = null;
this.deinflector = null; this.deinflector = null;
this.tagCache = {}; this.tagCache = new Map();
} }
async prepare() { async prepare() {
@ -44,12 +44,12 @@ class Translator {
} }
async purgeDatabase() { async purgeDatabase() {
this.tagCache = {}; this.tagCache.clear();
await this.database.purge(); await this.database.purge();
} }
async deleteDictionary(dictionaryName) { async deleteDictionary(dictionaryName) {
this.tagCache = {}; this.tagCache.clear();
await this.database.deleteDictionary(dictionaryName); await this.database.deleteDictionary(dictionaryName);
} }
@ -537,22 +537,22 @@ class Translator {
async getTagMetaList(names, title) { async getTagMetaList(names, title) {
const tagMetaList = []; const tagMetaList = [];
const cache = ( let cache = this.tagCache.get(title);
hasOwn(this.tagCache, title) ? if (typeof cache === 'undefined') {
this.tagCache[title] : cache = new Map();
(this.tagCache[title] = {}) this.tagCache.set(title, cache);
); }
for (const name of names) { for (const name of names) {
const base = Translator.getNameBase(name); const base = Translator.getNameBase(name);
if (hasOwn(cache, base)) { let tagMeta = cache.get(base);
tagMetaList.push(cache[base]); if (typeof tagMeta === 'undefined') {
} else { tagMeta = await this.database.findTagForTitle(base, title);
const tagMeta = await this.database.findTagForTitle(base, title); cache.set(base, tagMeta);
cache[base] = tagMeta;
tagMetaList.push(tagMeta);
} }
tagMetaList.push(tagMeta);
} }
return tagMetaList; return tagMetaList;

View File

@ -35,7 +35,7 @@
<h1>Yomichan Updated!</h1> <h1>Yomichan Updated!</h1>
<p> <p>
The Yomichan extension has been updated to a new version! In order to continue The Yomichan extension has been updated to a new version! In order to continue
viewing definitions on this page you must reload this tab or restart your browser. viewing definitions on this page, you must reload this tab or restart your browser.
</p> </p>
</div> </div>
</div> </div>

View File

@ -65,8 +65,8 @@ function apiNoteView(noteId) {
return _apiInvoke('noteView', {noteId}); return _apiInvoke('noteView', {noteId});
} }
function apiTemplateRender(template, data, dynamic) { function apiTemplateRender(template, data) {
return _apiInvoke('templateRender', {data, template, dynamic}); return _apiInvoke('templateRender', {data, template});
} }
function apiAudioGetUrl(definition, source, optionsContext) { function apiAudioGetUrl(definition, source, optionsContext) {

View File

@ -114,8 +114,11 @@ function audioGetFromUrl(url, willDownload) {
async function audioGetFromSources(expression, sources, optionsContext, willDownload, cache=null) { async function audioGetFromSources(expression, sources, optionsContext, willDownload, cache=null) {
const key = `${expression.expression}:${expression.reading}`; const key = `${expression.expression}:${expression.reading}`;
if (cache !== null && hasOwn(cache, expression)) { if (cache !== null) {
return cache[key]; const cacheValue = cache.get(expression);
if (typeof cacheValue !== 'undefined') {
return cacheValue;
}
} }
for (let i = 0, ii = sources.length; i < ii; ++i) { for (let i = 0, ii = sources.length; i < ii; ++i) {
@ -133,7 +136,7 @@ async function audioGetFromSources(expression, sources, optionsContext, willDown
} }
const result = {audio, url, source}; const result = {audio, url, source};
if (cache !== null) { if (cache !== null) {
cache[key] = result; cache.set(key, result);
} }
return result; return result;
} catch (e) { } catch (e) {

View File

@ -113,11 +113,7 @@ function toIterable(value) {
if (value !== null && typeof value === 'object') { if (value !== null && typeof value === 'object') {
const length = value.length; const length = value.length;
if (typeof length === 'number' && Number.isFinite(length)) { if (typeof length === 'number' && Number.isFinite(length)) {
const array = []; return Array.from(value);
for (let i = 0; i < length; ++i) {
array.push(value[i]);
}
return array;
} }
} }

View File

@ -32,7 +32,7 @@ class Display {
this.index = 0; this.index = 0;
this.audioPlaying = null; this.audioPlaying = null;
this.audioFallback = null; this.audioFallback = null;
this.audioCache = {}; this.audioCache = new Map();
this.styleNode = null; this.styleNode = null;
this.eventListeners = []; this.eventListeners = [];
@ -186,13 +186,15 @@ class Display {
e.preventDefault(); e.preventDefault();
const link = e.currentTarget; const link = e.currentTarget;
const entry = link.closest('.entry'); const entry = link.closest('.entry');
const definitionIndex = this.entryIndexFind(entry); const index = this.entryIndexFind(entry);
if (index < 0 || index >= this.definitions.length) { return; }
const expressionIndex = Display.indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link); const expressionIndex = Display.indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link);
this.audioPlay( this.audioPlay(
this.definitions[definitionIndex], this.definitions[index],
// expressionIndex is used in audioPlay to detect result output mode // expressionIndex is used in audioPlay to detect result output mode
Math.max(expressionIndex, this.options.general.resultOutputMode === 'merge' ? 0 : -1), Math.max(expressionIndex, this.options.general.resultOutputMode === 'merge' ? 0 : -1),
definitionIndex index
); );
} }
@ -200,6 +202,8 @@ class Display {
e.preventDefault(); e.preventDefault();
const link = e.currentTarget; const link = e.currentTarget;
const index = this.entryIndexFind(link); const index = this.entryIndexFind(link);
if (index < 0 || index >= this.definitions.length) { return; }
this.noteAdd(this.definitions[index], link.dataset.mode); this.noteAdd(this.definitions[index], link.dataset.mode);
} }
@ -498,6 +502,8 @@ class Display {
} }
autoPlayAudio() { autoPlayAudio() {
if (this.definitions.length === 0) { return; }
this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0); this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0);
} }
@ -597,9 +603,12 @@ class Display {
} }
noteTryAdd(mode) { noteTryAdd(mode) {
const button = this.adderButtonFind(this.index, mode); const index = this.index;
if (index < 0 || index >= this.definitions.length) { return; }
const button = this.adderButtonFind(index, mode);
if (button !== null && !button.classList.contains('disabled')) { if (button !== null && !button.classList.contains('disabled')) {
this.noteAdd(this.definitions[this.index], mode); this.noteAdd(this.definitions[index], mode);
} }
} }
@ -901,9 +910,12 @@ Display._onKeyDownHandlers = new Map([
['P', (self, e) => { ['P', (self, e) => {
if (e.altKey) { if (e.altKey) {
const entry = self.getEntry(self.index); const index = self.index;
if (index < 0 || index >= self.definitions.length) { return; }
const entry = self.getEntry(index);
if (entry !== null && entry.dataset.type === 'term') { if (entry !== null && entry.dataset.type === 'term') {
self.audioPlay(self.definitions[self.index], self.firstExpressionIndex, self.index); self.audioPlay(self.definitions[index], self.firstExpressionIndex, index);
} }
return true; return true;
} }