Skip to content

Commit dad24a6

Browse files
committed
MOBILE-4828 mathjax: Improve how MathJax is loaded
1 parent 119c609 commit dad24a6

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/addons/filter/mathjaxloader/services/handlers/mathjaxloader.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { CoreText } from '@singletons/text';
2121
import { CorePromiseUtils } from '@singletons/promise-utils';
2222
import { CoreSite } from '@classes/sites/site';
2323
import { makeSingleton } from '@singletons';
24-
import { CoreWait } from '@singletons/wait';
2524
import { CoreDom } from '@singletons/dom';
2625
import { CoreLogger } from '@singletons/logger';
2726

@@ -40,9 +39,11 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
4039
/**
4140
* Initialize MathJax.
4241
*/
43-
initialize(): void {
42+
async initialize(): Promise<void> {
4443
if (document.head.querySelector('#core-filter-mathjax-script')) {
4544
// Script already added, don't add it again.
45+
await this.window.MathJax?.startup.promise;
46+
4647
return;
4748
}
4849

@@ -58,14 +59,28 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
5859
};
5960

6061
// Add the script to the header.
61-
const script = document.createElement('script');
62-
script.id = 'core-filter-mathjax-script';
63-
script.src = 'assets/lib/mathjax/tex-mml-chtml.js';
64-
document.head.appendChild(script);
62+
await this.loadMathJax();
63+
64+
await this.window.MathJax?.startup.promise;
6565

6666
// @todo: Once MathJax supports locale, set current language and listen for CoreEvents.LANGUAGE_CHANGED events.
6767
}
6868

69+
/**
70+
* Load the MathJax script.
71+
*/
72+
protected loadMathJax(): Promise<void> {
73+
return new Promise((resolve, reject) => {
74+
const script = document.createElement('script');
75+
script.onload = () => resolve();
76+
script.onerror = (error) => reject(error);
77+
script.id = 'core-filter-mathjax-script';
78+
script.src = 'assets/lib/mathjax/tex-mml-chtml.js';
79+
script.type = 'text/javascript';
80+
document.head.appendChild(script);
81+
});
82+
}
83+
6984
/**
7085
* @inheritdoc
7186
*/
@@ -152,8 +167,7 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
152167
return;
153168
}
154169

155-
this.initialize();
156-
await this.waitForReady();
170+
await this.initialize();
157171

158172
await Promise.all(equations.map((node) => this.typesetNode(node)));
159173
}
@@ -183,22 +197,6 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
183197
}
184198
}
185199

186-
/**
187-
* Wait for the MathJax library and our JS object to be loaded.
188-
*
189-
* @param retries Number of times this has been retried.
190-
* @returns Promise resolved when ready or if it took too long to load.
191-
*/
192-
protected async waitForReady(retries: number = 0): Promise<void> {
193-
if (this.window.MathJax?.typesetPromise || retries >= 25) {
194-
// Loaded or too many retries, stop.
195-
return;
196-
}
197-
198-
await CoreWait.wait(20 + 10 * retries);
199-
await CorePromiseUtils.ignoreErrors(this.waitForReady(retries + 1));
200-
}
201-
202200
/**
203201
* Find math environments in the $text and wrap them in no link spans
204202
* (<span class="nolink"></span>). If math environments are nested, only

0 commit comments

Comments
 (0)