Refactor DisplayAnki.setupEntriesComplete params and implementation (#1794)

This commit is contained in:
toasted-nutbread 2021-07-04 11:11:05 -04:00 committed by GitHub
parent e6a9aef2d5
commit b8478857aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 23 deletions

View File

@ -45,6 +45,10 @@ class DisplayAnki {
this._screenshotQuality = 100; this._screenshotQuality = 100;
this._noteTags = []; this._noteTags = [];
this._modeOptions = new Map(); this._modeOptions = new Map();
this._dictionaryEntryTypeModeMap = new Map([
['kanji', ['kanji']],
['term', ['term-kanji', 'term-kana']]
]);
this._onShowTagsBind = this._onShowTags.bind(this); this._onShowTagsBind = this._onShowTags.bind(this);
this._onNoteAddBind = this._onNoteAdd.bind(this); this._onNoteAddBind = this._onNoteAdd.bind(this);
this._onNoteViewBind = this._onNoteView.bind(this); this._onNoteViewBind = this._onNoteView.bind(this);
@ -76,8 +80,8 @@ class DisplayAnki {
this._addMultipleEventListeners(entry, '.action-view-note', 'click', this._onNoteViewBind); this._addMultipleEventListeners(entry, '.action-view-note', 'click', this._onNoteViewBind);
} }
setupEntriesComplete(isTerms, dictionaryEntries) { // TODO : Don't pass (isTerms, dictionaryEntries) setupEntriesComplete() {
this._updateAdderButtons(isTerms, dictionaryEntries); this._updateAdderButtons();
} }
async getLogData(dictionaryEntry) { async getLogData(dictionaryEntry) {
@ -254,7 +258,8 @@ class DisplayAnki {
return {type, term, reading}; return {type, term, reading};
} }
async _updateAdderButtons(isTerms, dictionaryEntries) { async _updateAdderButtons() {
const {dictionaryEntries} = this._display;
const token = {}; const token = {};
this._updateAdderButtonsToken = token; this._updateAdderButtonsToken = token;
await this._updateAdderButtonsPromise; await this._updateAdderButtonsPromise;
@ -264,35 +269,26 @@ class DisplayAnki {
try { try {
this._updateAdderButtonsPromise = promise; this._updateAdderButtonsPromise = promise;
const modes = this._getModes(isTerms);
let states; let states;
try { try {
states = await this._areDictionaryEntriesAddable( states = await this._areDictionaryEntriesAddable(dictionaryEntries);
dictionaryEntries,
modes,
this._checkForDuplicates ? null : true,
this._displayTags !== 'never'
);
} catch (e) { } catch (e) {
return; return;
} }
if (this._updateAdderButtonsToken !== token) { return; } if (this._updateAdderButtonsToken !== token) { return; }
this._updateAdderButtons2(states, modes); this._updateAdderButtons2(states);
} finally { } finally {
resolve(); resolve();
} }
} }
_updateAdderButtons2(states, modes) { _updateAdderButtons2(states) {
const displayTags = this._displayTags; const displayTags = this._displayTags;
for (let i = 0, ii = states.length; i < ii; ++i) { for (let i = 0, ii = states.length; i < ii; ++i) {
const infos = states[i];
let noteId = null; let noteId = null;
for (let j = 0, jj = infos.length; j < jj; ++j) { for (const {mode, canAdd, noteIds, noteInfos} of states[i]) {
const {canAdd, noteIds, noteInfos} = infos[j];
const mode = modes[j];
const button = this._adderButtonFind(i, mode); const button = this._adderButtonFind(i, mode);
if (button === null) { if (button === null) {
continue; continue;
@ -463,16 +459,26 @@ class DisplayAnki {
return templates; return templates;
} }
async _areDictionaryEntriesAddable(dictionaryEntries, modes, forceCanAddValue, fetchAdditionalInfo) { async _areDictionaryEntriesAddable(dictionaryEntries) {
const modeCount = modes.length; const forceCanAddValue = (this._checkForDuplicates ? null : true);
const fetchAdditionalInfo = (this._displayTags !== 'never');
const notePromises = []; const notePromises = [];
for (const dictionaryEntry of dictionaryEntries) { const noteTargets = [];
for (let i = 0, ii = dictionaryEntries.length; i < ii; ++i) {
const dictionaryEntry = dictionaryEntries[i];
const {type} = dictionaryEntry;
const modes = this._dictionaryEntryTypeModeMap.get(type);
if (typeof modes === 'undefined') { continue; }
for (const mode of modes) { for (const mode of modes) {
const notePromise = this._createNote(dictionaryEntry, mode, false); const notePromise = this._createNote(dictionaryEntry, mode, false);
notePromises.push(notePromise); notePromises.push(notePromise);
noteTargets.push({index: i, mode});
} }
} }
const notes = (await Promise.all(notePromises)).map(({note}) => note);
const noteInfoList = await Promise.all(notePromises);
const notes = noteInfoList.map(({note}) => note);
let infos; let infos;
if (forceCanAddValue !== null) { if (forceCanAddValue !== null) {
@ -485,8 +491,14 @@ class DisplayAnki {
} }
const results = []; const results = [];
for (let i = 0, ii = infos.length; i < ii; i += modeCount) { for (let i = 0, ii = noteInfoList.length; i < ii; ++i) {
results.push(infos.slice(i, i + modeCount)); const {note, errors} = noteInfoList[i];
const {canAdd, valid, noteIds, noteInfos} = infos[i];
const {mode, index} = noteTargets[i];
while (index >= results.length) {
results.push([]);
}
results[index].push({mode, note, errors, canAdd, valid, noteIds, noteInfos});
} }
return results; return results;
} }

View File

@ -982,7 +982,7 @@ class Display extends EventDispatcher {
} }
this._displayAudio.setupEntriesComplete(); this._displayAudio.setupEntriesComplete();
this._displayAnki.setupEntriesComplete(isTerms, dictionaryEntries); this._displayAnki.setupEntriesComplete();
} }
_setContentExtensionUnloaded() { _setContentExtensionUnloaded() {