Skip to content

Commit 0436b6f

Browse files
authored
fix(content): do not run change detection when loading mermaid (#562)
1 parent ce348f6 commit 0436b6f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

packages/content/src/lib/markdown.component.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import {
1010
ViewEncapsulation,
1111
inject,
1212
} from '@angular/core';
13+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
1314
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
1415
import { ActivatedRoute, Data } from '@angular/router';
15-
import { Observable, of } from 'rxjs';
16+
import { Observable, from, of } from 'rxjs';
1617
import { catchError, map, mergeMap } from 'rxjs/operators';
1718

1819
import { AnchorNavigationDirective } from './anchor-navigation.directive';
@@ -54,15 +55,7 @@ export default class AnalogMarkdownComponent
5455
}
5556
}
5657

57-
async loadMermaid(mermaidImport: Promise<typeof import('mermaid')>) {
58-
this.mermaid = await mermaidImport;
59-
this.mermaid.default.initialize({ startOnLoad: false });
60-
// Explicitly running mermaid as ngAfterViewChecked
61-
// has probably already been called
62-
this.zone.runOutsideAngular(() => this.mermaid?.default.run());
63-
}
64-
65-
ngOnInit() {
58+
ngOnInit(): void {
6659
this.updateContent();
6760
}
6861

@@ -87,4 +80,20 @@ export default class AnalogMarkdownComponent
8780
this.contentRenderer.enhance();
8881
this.zone.runOutsideAngular(() => this.mermaid?.default.run());
8982
}
83+
84+
private loadMermaid(mermaidImport: Promise<typeof import('mermaid')>) {
85+
this.zone.runOutsideAngular(() =>
86+
// Wrap into an observable to avoid redundant initialization once
87+
// the markdown component is destroyed before the promise is resolved.
88+
from(mermaidImport)
89+
.pipe(takeUntilDestroyed())
90+
.subscribe((mermaid) => {
91+
this.mermaid = mermaid;
92+
this.mermaid.default.initialize({ startOnLoad: false });
93+
// Explicitly running mermaid as ngAfterViewChecked
94+
// has probably already been called
95+
this.mermaid?.default.run();
96+
})
97+
);
98+
}
9099
}

0 commit comments

Comments
 (0)