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;