Skip to content

Commit

Permalink
Fix AnkiCardController using stale data (FooSoft#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread authored Jan 6, 2021
1 parent 8c92c1c commit 3760b22
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ext/bg/js/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class AnkiController {
'clipboard-text'
]);
this._stringComparer = new Intl.Collator(); // Locale does not matter
this._ankiOptions = null;
this._getAnkiDataPromise = null;
this._ankiErrorContainer = null;
this._ankiErrorMessageNode = null;
Expand Down Expand Up @@ -174,7 +173,6 @@ class AnkiController {
// Private

async _onOptionsChanged({options: {anki}}) {
this._ankiOptions = anki;
this._ankiConnect.server = anki.server;
this._ankiConnect.enabled = anki.enable;

Expand All @@ -188,7 +186,7 @@ class AnkiController {
}

_onAnkiEnableChanged({detail: {value}}) {
if (this._ankiOptions === null) { return; }
if (this._ankiConnect.server === null) { return; }
this._ankiConnect.enabled = value;

for (const cardController of this._selectorObserver.datas()) {
Expand All @@ -215,7 +213,7 @@ class AnkiController {

_createCardController(node) {
const cardController = new AnkiCardController(this._settingsController, this, node);
cardController.prepare(this._ankiOptions);
cardController.prepare();
return cardController;
}

Expand Down Expand Up @@ -383,9 +381,14 @@ class AnkiCardController {
this._ankiCardDeckSelect = null;
this._ankiCardModelSelect = null;
this._ankiCardFieldsContainer = null;
this._cleaned = false;
}

async prepare(ankiOptions) {
async prepare() {
const options = await this._settingsController.getOptions();
const ankiOptions = options.anki;
if (this._cleaned) { return; }

const cardOptions = this._getCardOptions(ankiOptions, this._cardType);
if (cardOptions === null) { return; }
const {deck, model, fields} = cardOptions;
Expand All @@ -407,12 +410,14 @@ class AnkiCardController {
}

cleanup() {
this._cleaned = true;
this._eventListeners.removeAllEventListeners();
}

async updateAnkiState() {
if (this._fields === null) { return; }
const {deckNames, modelNames} = await this._ankiController.getAnkiData();
if (this._cleaned) { return; }
this._setupSelects(deckNames, modelNames);
}

Expand Down

0 comments on commit 3760b22

Please sign in to comment.