Replace _updatingQa synchronization with promise-based _updatingQueue

This commit is contained in:
Henrik Giesel 2020-12-30 14:45:27 +01:00
parent eea6bf963b
commit aaa30a5d49

View file

@ -5,7 +5,7 @@ declare var MathJax: any;
var ankiPlatform = "desktop"; var ankiPlatform = "desktop";
var typeans; var typeans;
var _updatingQA = false; var _updatingQueue: Promise<void> = Promise.resolve();
var qFade = 50; var qFade = 50;
var aFade = 0; var aFade = 0;
@ -23,18 +23,11 @@ function _runHook(arr: Array<() => void | Promise<void>>): Promise<void[]> {
return Promise.all(promises); return Promise.all(promises);
} }
async function _updateQA(html, fadeTime, onupdate, onshown) { function _queueAction(action: () => Promise<void>): void {
// if a request to update q/a comes in before the previous content _updatingQueue = _updatingQueue.then(action);
// has been loaded, wait a while and try again
if (_updatingQA) {
setTimeout(function () {
_updateQA(html, fadeTime, onupdate, onshown);
}, 50);
return;
} }
_updatingQA = true; async function _updateQA(html, fadeTime, onupdate, onshown): Promise<void> {
onUpdateHook = [onupdate]; onUpdateHook = [onupdate];
onShownHook = [onshown]; onShownHook = [onshown];
@ -67,12 +60,10 @@ async function _updateQA(html, fadeTime, onupdate, onshown) {
// and reveal when processing is done // and reveal when processing is done
await qa.fadeTo(fadeTime, 1).promise(); await qa.fadeTo(fadeTime, 1).promise();
await _runHook(onShownHook); await _runHook(onShownHook);
_updatingQA = false;
} }
function _showQuestion(q, bodyclass) { function _showQuestion(q, bodyclass) {
_updateQA( _queueAction(() => _updateQA(
q, q,
qFade, qFade,
function () { function () {
@ -88,11 +79,11 @@ function _showQuestion(q, bodyclass) {
typeans.focus(); typeans.focus();
} }
} }
); ));
} }
function _showAnswer(a, bodyclass) { function _showAnswer(a, bodyclass) {
_updateQA( _queueAction(() => _updateQA(
a, a,
aFade, aFade,
function () { function () {
@ -108,7 +99,7 @@ function _showAnswer(a, bodyclass) {
} }
}, },
function () {} function () {}
); ));
} }
const _flagColours = { const _flagColours = {