From aaa30a5d491b649e41f9feff2d58653a6d02dbb9 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Wed, 30 Dec 2020 14:45:27 +0100 Subject: [PATCH] Replace _updatingQa synchronization with promise-based _updatingQueue --- qt/aqt/data/web/js/reviewer.ts | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index d1b34389c..5c6187dec 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -5,7 +5,7 @@ declare var MathJax: any; var ankiPlatform = "desktop"; var typeans; -var _updatingQA = false; +var _updatingQueue: Promise = Promise.resolve(); var qFade = 50; var aFade = 0; @@ -23,18 +23,11 @@ function _runHook(arr: Array<() => void | Promise>): Promise { return Promise.all(promises); } -async function _updateQA(html, fadeTime, onupdate, onshown) { - // if a request to update q/a comes in before the previous content - // has been loaded, wait a while and try again - if (_updatingQA) { - setTimeout(function () { - _updateQA(html, fadeTime, onupdate, onshown); - }, 50); - return; - } - - _updatingQA = true; +function _queueAction(action: () => Promise): void { + _updatingQueue = _updatingQueue.then(action); +} +async function _updateQA(html, fadeTime, onupdate, onshown): Promise { onUpdateHook = [onupdate]; onShownHook = [onshown]; @@ -67,12 +60,10 @@ async function _updateQA(html, fadeTime, onupdate, onshown) { // and reveal when processing is done await qa.fadeTo(fadeTime, 1).promise(); await _runHook(onShownHook); - - _updatingQA = false; } function _showQuestion(q, bodyclass) { - _updateQA( + _queueAction(() => _updateQA( q, qFade, function () { @@ -88,11 +79,11 @@ function _showQuestion(q, bodyclass) { typeans.focus(); } } - ); + )); } function _showAnswer(a, bodyclass) { - _updateQA( + _queueAction(() => _updateQA( a, aFade, function () { @@ -108,7 +99,7 @@ function _showAnswer(a, bodyclass) { } }, function () {} - ); + )); } const _flagColours = {