Skip to content

Commit 8f0b538

Browse files
committed
MOBILE-4769 mathjax: Don't load MathJax lib until it's needed
1 parent 7b4501a commit 8f0b538

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/addons/filter/mathjaxloader/mathjaxloader.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import { AddonFilterMathJaxLoaderHandler } from './services/handlers/mathjaxload
2828
multi: true,
2929
useValue: async () => {
3030
CoreFilterDelegate.registerHandler(AddonFilterMathJaxLoaderHandler.instance);
31-
32-
await AddonFilterMathJaxLoaderHandler.initialize();
3331
},
3432
},
3533
],

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
4040
/**
4141
* Initialize MathJax.
4242
*/
43-
async initialize(): Promise<void> {
43+
initialize(): void {
4444
if (document.head.querySelector('#core-filter-mathjax-script')) {
4545
// Script already added, don't add it again.
4646
return;
@@ -111,8 +111,6 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
111111
* @inheritdoc
112112
*/
113113
async handleHtml(container: HTMLElement): Promise<void> {
114-
await this.waitForReady();
115-
116114
// Make sure the element is in DOM, otherwise some equations don't work.
117115
// Automatically timeout the promise after a certain time, we don't want to wait forever.
118116
await CorePromiseUtils.ignoreErrors(CorePromiseUtils.timeoutPromise(CoreDom.waitToBeInDOM(container), 15000));
@@ -150,6 +148,12 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
150148
*/
151149
protected async typeset(container: HTMLElement): Promise<void> {
152150
const equations = Array.from(container.getElementsByClassName('filter_mathjaxloader_equation'));
151+
if (!equations.length) {
152+
return;
153+
}
154+
155+
this.initialize();
156+
await this.waitForReady();
153157

154158
await Promise.all(equations.map((node) => this.typesetNode(node)));
155159
}
@@ -186,12 +190,12 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
186190
* @returns Promise resolved when ready or if it took too long to load.
187191
*/
188192
protected async waitForReady(retries: number = 0): Promise<void> {
189-
if (this.window.MathJax?.typesetPromise || retries >= 20) {
193+
if (this.window.MathJax?.typesetPromise || retries >= 25) {
190194
// Loaded or too many retries, stop.
191195
return;
192196
}
193197

194-
await CoreWait.wait(250);
198+
await CoreWait.wait(20 + 10 * retries);
195199
await CorePromiseUtils.ignoreErrors(this.waitForReady(retries + 1));
196200
}
197201

0 commit comments

Comments
 (0)