From 6232e3efc204037e25f39ec5d70f31011b042b58 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 8 Nov 2020 16:48:15 -0500 Subject: Optimize Anki duplicate checks (#1013) * Move/rename functions * Prevent overlapping calls to check addable definitions --- ext/mixed/js/display.js | 58 +++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 23 deletions(-) (limited to 'ext/mixed') 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; -- cgit v1.2.3