From ecdb06cbd6912e3bc51a4296025f3f7a76f1dbe5 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Dec 2020 13:32:04 +0100 Subject: [PATCH 1/5] Use async/await syntax in _updateQa --- qt/aqt/data/web/js/reviewer.ts | 58 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index de5d63b65..d4c3011db 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -21,7 +21,7 @@ function _runHook(arr: () => Promise[]): Promise { return Promise.all(promises); } -function _updateQA(html, fadeTime, onupdate, onshown) { +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) { @@ -39,35 +39,35 @@ function _updateQA(html, fadeTime, onupdate, onshown) { var qa = $("#qa"); // fade out current text - new Promise((resolve) => qa.fadeTo(fadeTime, 0, () => resolve())) - // update text - .then(() => { - try { - qa.html(html); - } catch (err) { - qa.html( - ( - `Invalid HTML on card: ${String(err).substring(0, 2000)}\n` + - String(err.stack).substring(0, 2000) - ).replace(/\n/g, "
") - ); - } - }) - .then(() => _runHook(onUpdateHook)) - .then(() => - // @ts-ignore wait for mathjax to ready - MathJax.startup.promise.then(() => { - // @ts-ignore clear MathJax buffer - MathJax.typesetClear(); + await qa.fadeTo(fadeTime, 0).promise(); - // @ts-ignore typeset - return MathJax.typesetPromise(qa.slice(0, 1)); - }) - ) - // and reveal when processing is done - .then(() => new Promise((resolve) => qa.fadeTo(fadeTime, 1, () => resolve()))) - .then(() => _runHook(onShownHook)) - .then(() => (_updatingQA = false)); + // update text + try { + qa.html(html); + } catch (err) { + qa.html( + ( + `Invalid HTML on card: ${String(err).substring(0, 2000)}\n` + + String(err.stack).substring(0, 2000) + ).replace(/\n/g, "
") + ); + }; + await _runHook(onUpdateHook); + + // @ts-ignore wait for mathjax to ready + await MathJax.startup.promise.then(() => { + // @ts-ignore clear MathJax buffer + MathJax.typesetClear(); + + // @ts-ignore typeset + return MathJax.typesetPromise(qa.slice(0, 1)); + }); + + // and reveal when processing is done + await qa.fadeTo(fadeTime, 1).promise(); + await _runHook(onShownHook); + + _updatingQA = false; } function _showQuestion(q, bodyclass) { From 53f77346d8b4092d50b5de5854d799a95a986d03 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Dec 2020 13:42:30 +0100 Subject: [PATCH 2/5] Fix typing of reviewer hooks --- qt/aqt/data/web/js/reviewer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index d4c3011db..e251142f1 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -8,10 +8,10 @@ var _updatingQA = false; var qFade = 50; var aFade = 0; -var onUpdateHook; -var onShownHook; +var onUpdateHook: Array<() => Promise>; +var onShownHook: Array<() => Promise>; -function _runHook(arr: () => Promise[]): Promise { +function _runHook(arr: Array<() => Promise>): Promise { var promises = []; for (var i = 0; i < arr.length; i++) { From 8f39c746d9f91e6d7c7041b2abfdb4e467db5f69 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Dec 2020 13:43:47 +0100 Subject: [PATCH 3/5] Replace ts-ignore with `declare var` --- qt/aqt/data/web/js/reviewer.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index e251142f1..fe44b7aa7 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -1,6 +1,8 @@ /* Copyright: Ankitects Pty Ltd and contributors * License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */ +declare var MathJax: any; + var ankiPlatform = "desktop"; var typeans; var _updatingQA = false; @@ -54,12 +56,11 @@ async function _updateQA(html, fadeTime, onupdate, onshown) { }; await _runHook(onUpdateHook); - // @ts-ignore wait for mathjax to ready + // wait for mathjax to ready await MathJax.startup.promise.then(() => { - // @ts-ignore clear MathJax buffer + // clear MathJax buffers from previous typesets MathJax.typesetClear(); - // @ts-ignore typeset return MathJax.typesetPromise(qa.slice(0, 1)); }); From 7ef41ce5735a2f5b2f204451c3002efe40815a3c Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Dec 2020 14:01:09 +0100 Subject: [PATCH 4/5] Hook functions may also return void --- qt/aqt/data/web/js/reviewer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index fe44b7aa7..ee6754205 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -10,10 +10,10 @@ var _updatingQA = false; var qFade = 50; var aFade = 0; -var onUpdateHook: Array<() => Promise>; -var onShownHook: Array<() => Promise>; +var onUpdateHook: Array<() => void | Promise>; +var onShownHook: Array<() => void | Promise>; -function _runHook(arr: Array<() => Promise>): Promise { +function _runHook(arr: Array<() => void | Promise>): Promise { var promises = []; for (var i = 0; i < arr.length; i++) { From ff4780e0057031eb4de3c16b46c7b4202f3f3afe Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Dec 2020 14:45:20 +0100 Subject: [PATCH 5/5] Satisfy prettier --- qt/aqt/data/web/js/reviewer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index ee6754205..d1b34389c 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -53,7 +53,7 @@ async function _updateQA(html, fadeTime, onupdate, onshown) { String(err.stack).substring(0, 2000) ).replace(/\n/g, "
") ); - }; + } await _runHook(onUpdateHook); // wait for mathjax to ready