aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-11-08 16:48:15 -0500
committerGitHub <noreply@github.com>2020-11-08 16:48:15 -0500
commit6232e3efc204037e25f39ec5d70f31011b042b58 (patch)
treee4861038434587bdf8401eeeb14732ba7e7c0aed /ext/mixed/js
parent16321a1f8c9902be100b01067e1593694fa51341 (diff)
Optimize Anki duplicate checks (#1013)
* Move/rename functions * Prevent overlapping calls to check addable definitions
Diffstat (limited to 'ext/mixed/js')
-rw-r--r--ext/mixed/js/display.js58
1 files changed, 35 insertions, 23 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index 07e4f37a..39bd7732 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -86,6 +86,7 @@ class Display extends EventDispatcher {
this._ankiNoteBuilder = new AnkiNoteBuilder({
renderTemplate: this._renderTemplate.bind(this)
});
+ this._updateAdderButtonsPromise = Promise.resolve();
this.registerActions([
['close', () => { this.onEscape(); }],
@@ -892,32 +893,11 @@ class Display extends EventDispatcher {
this.autoPlayAudio();
}
- this._setContentTermsOrKanjiUpdateAdderButtons(token, isTerms, definitions);
+ this._updateAdderButtons(token, isTerms, definitions);
return true;
}
- async _setContentTermsOrKanjiUpdateAdderButtons(token, isTerms, definitions) {
- const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji'];
- let states;
- try {
- if (this._options.anki.checkForDuplicates) {
- const noteContext = await this._getNoteContext();
- states = await this._areDefinitionsAddable(definitions, modes, noteContext);
- } else {
- if (!await api.isAnkiConnected()) {
- throw new Error('Anki not connected');
- }
- states = this._areDefinitionsAddableForcedValue(definitions, modes, true);
- }
- } catch (e) {
- return;
- }
- if (this._setContentToken !== token) { return; }
-
- this._updateAdderButtons(states, modes);
- }
-
_setContentExtensionUnloaded() {
const errorExtensionUnloaded = document.querySelector('#error-extension-unloaded');
@@ -976,7 +956,39 @@ class Display extends EventDispatcher {
this._navigationHeader.dataset.hasNext = `${!!next}`;
}
- _updateAdderButtons(states, modes) {
+ async _updateAdderButtons(token, isTerms, definitions) {
+ await this._updateAdderButtonsPromise;
+ if (this._setContentToken !== token) { return; }
+
+ const {promise, resolve} = deferPromise();
+ try {
+ this._updateAdderButtonsPromise = promise;
+
+ const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji'];
+ let states;
+ try {
+ if (this._options.anki.checkForDuplicates) {
+ const noteContext = await this._getNoteContext();
+ states = await this._areDefinitionsAddable(definitions, modes, noteContext);
+ } else {
+ if (!await api.isAnkiConnected()) {
+ throw new Error('Anki not connected');
+ }
+ states = this._areDefinitionsAddableForcedValue(definitions, modes, true);
+ }
+ } catch (e) {
+ return;
+ }
+
+ if (this._setContentToken !== token) { return; }
+
+ this._updateAdderButtons2(states, modes);
+ } finally {
+ resolve();
+ }
+ }
+
+ _updateAdderButtons2(states, modes) {
for (let i = 0, ii = states.length; i < ii; ++i) {
const infos = states[i];
let noteId = null;