Store anki note details (#1798)
* Update error handling of _areDictionaryEntriesAddable * Store note details * Remove promise after use * Rename function * Clear stored details * Rename function * Rename function
This commit is contained in:
parent
b8478857aa
commit
0491de12d4
@ -31,8 +31,9 @@ class DisplayAnki {
|
|||||||
this._ankiNoteNotificationEventListeners = null;
|
this._ankiNoteNotificationEventListeners = null;
|
||||||
this._ankiTagNotification = null;
|
this._ankiTagNotification = null;
|
||||||
this._updateAdderButtonsPromise = Promise.resolve();
|
this._updateAdderButtonsPromise = Promise.resolve();
|
||||||
this._updateAdderButtonsToken = null;
|
this._updateDictionaryEntryDetailsToken = null;
|
||||||
this._eventListeners = new EventListenerCollection();
|
this._eventListeners = new EventListenerCollection();
|
||||||
|
this._dictionaryEntryDetails = null;
|
||||||
this._noteContext = null;
|
this._noteContext = null;
|
||||||
this._checkForDuplicates = false;
|
this._checkForDuplicates = false;
|
||||||
this._suspendNewCards = false;
|
this._suspendNewCards = false;
|
||||||
@ -66,7 +67,8 @@ class DisplayAnki {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cleanupEntries() {
|
cleanupEntries() {
|
||||||
this._updateAdderButtonsToken = null;
|
this._updateDictionaryEntryDetailsToken = null;
|
||||||
|
this._dictionaryEntryDetails = null;
|
||||||
this._hideAnkiNoteErrors(false);
|
this._hideAnkiNoteErrors(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ class DisplayAnki {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setupEntriesComplete() {
|
setupEntriesComplete() {
|
||||||
this._updateAdderButtons();
|
this._updateDictionaryEntryDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getLogData(dictionaryEntry) {
|
async getLogData(dictionaryEntry) {
|
||||||
@ -258,47 +260,45 @@ class DisplayAnki {
|
|||||||
return {type, term, reading};
|
return {type, term, reading};
|
||||||
}
|
}
|
||||||
|
|
||||||
async _updateAdderButtons() {
|
async _updateDictionaryEntryDetails() {
|
||||||
const {dictionaryEntries} = this._display;
|
const {dictionaryEntries} = this._display;
|
||||||
const token = {};
|
const token = {};
|
||||||
this._updateAdderButtonsToken = token;
|
this._updateDictionaryEntryDetailsToken = token;
|
||||||
await this._updateAdderButtonsPromise;
|
if (this._updateAdderButtonsPromise !== null) {
|
||||||
if (this._updateAdderButtonsToken !== token) { return; }
|
await this._updateAdderButtonsPromise;
|
||||||
|
}
|
||||||
|
if (this._updateDictionaryEntryDetailsToken !== token) { return; }
|
||||||
|
|
||||||
const {promise, resolve} = deferPromise();
|
const {promise, resolve} = deferPromise();
|
||||||
try {
|
try {
|
||||||
this._updateAdderButtonsPromise = promise;
|
this._updateAdderButtonsPromise = promise;
|
||||||
|
const dictionaryEntryDetails = await this._getDictionaryEntryDetails(dictionaryEntries);
|
||||||
let states;
|
if (this._updateDictionaryEntryDetailsToken !== token) { return; }
|
||||||
try {
|
this._dictionaryEntryDetails = dictionaryEntryDetails;
|
||||||
states = await this._areDictionaryEntriesAddable(dictionaryEntries);
|
this._updateAdderButtons();
|
||||||
} catch (e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._updateAdderButtonsToken !== token) { return; }
|
|
||||||
|
|
||||||
this._updateAdderButtons2(states);
|
|
||||||
} finally {
|
} finally {
|
||||||
resolve();
|
resolve();
|
||||||
|
if (this._updateAdderButtonsPromise === promise) {
|
||||||
|
this._updateAdderButtonsPromise = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateAdderButtons2(states) {
|
_updateAdderButtons() {
|
||||||
const displayTags = this._displayTags;
|
const displayTags = this._displayTags;
|
||||||
for (let i = 0, ii = states.length; i < ii; ++i) {
|
const dictionaryEntryDetails = this._dictionaryEntryDetails;
|
||||||
|
for (let i = 0, ii = dictionaryEntryDetails.length; i < ii; ++i) {
|
||||||
let noteId = null;
|
let noteId = null;
|
||||||
for (const {mode, canAdd, noteIds, noteInfos} of states[i]) {
|
for (const {mode, canAdd, noteIds, noteInfos, ankiError} of dictionaryEntryDetails[i]) {
|
||||||
const button = this._adderButtonFind(i, mode);
|
const button = this._adderButtonFind(i, mode);
|
||||||
if (button === null) {
|
if (button !== null) {
|
||||||
continue;
|
button.disabled = !canAdd;
|
||||||
|
button.hidden = (ankiError !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(noteIds) && noteIds.length > 0) {
|
if (Array.isArray(noteIds) && noteIds.length > 0) {
|
||||||
noteId = noteIds[0];
|
noteId = noteIds[0];
|
||||||
}
|
}
|
||||||
button.disabled = !canAdd;
|
|
||||||
button.hidden = false;
|
|
||||||
|
|
||||||
if (displayTags !== 'never' && Array.isArray(noteInfos)) {
|
if (displayTags !== 'never' && Array.isArray(noteInfos)) {
|
||||||
this._setupTagsIndicator(i, noteInfos);
|
this._setupTagsIndicator(i, noteInfos);
|
||||||
@ -459,7 +459,7 @@ class DisplayAnki {
|
|||||||
return templates;
|
return templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _areDictionaryEntriesAddable(dictionaryEntries) {
|
async _getDictionaryEntryDetails(dictionaryEntries) {
|
||||||
const forceCanAddValue = (this._checkForDuplicates ? null : true);
|
const forceCanAddValue = (this._checkForDuplicates ? null : true);
|
||||||
const fetchAdditionalInfo = (this._displayTags !== 'never');
|
const fetchAdditionalInfo = (this._displayTags !== 'never');
|
||||||
|
|
||||||
@ -481,13 +481,19 @@ class DisplayAnki {
|
|||||||
const notes = noteInfoList.map(({note}) => note);
|
const notes = noteInfoList.map(({note}) => note);
|
||||||
|
|
||||||
let infos;
|
let infos;
|
||||||
if (forceCanAddValue !== null) {
|
let ankiError = null;
|
||||||
if (!await yomichan.api.isAnkiConnected()) {
|
try {
|
||||||
throw new Error('Anki not connected');
|
if (forceCanAddValue !== null) {
|
||||||
|
if (!await yomichan.api.isAnkiConnected()) {
|
||||||
|
throw new Error('Anki not connected');
|
||||||
|
}
|
||||||
|
infos = this._getAnkiNoteInfoForceValue(notes, forceCanAddValue);
|
||||||
|
} else {
|
||||||
|
infos = await yomichan.api.getAnkiNoteInfo(notes, fetchAdditionalInfo);
|
||||||
}
|
}
|
||||||
infos = this._getAnkiNoteInfoForceValue(notes, forceCanAddValue);
|
} catch (e) {
|
||||||
} else {
|
infos = this._getAnkiNoteInfoForceValue(notes, false);
|
||||||
infos = await yomichan.api.getAnkiNoteInfo(notes, fetchAdditionalInfo);
|
ankiError = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = [];
|
const results = [];
|
||||||
@ -498,7 +504,7 @@ class DisplayAnki {
|
|||||||
while (index >= results.length) {
|
while (index >= results.length) {
|
||||||
results.push([]);
|
results.push([]);
|
||||||
}
|
}
|
||||||
results[index].push({mode, note, errors, canAdd, valid, noteIds, noteInfos});
|
results[index].push({mode, note, errors, canAdd, valid, noteIds, noteInfos, ankiError});
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user