Skip to content

Commit f73be80

Browse files
authored
fix(module:code-editor): always initialize outside of the Angular zone (#7151)
1 parent c20bb80 commit f73be80

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

components/code-editor/code-editor.component.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ export class NzCodeEditorComponent implements OnDestroy, AfterViewInit {
104104
if (!this.platform.isBrowser) {
105105
return;
106106
}
107-
this.nzCodeEditorService.requestToInit().subscribe(option => this.setup(option));
107+
108+
this.nzCodeEditorService
109+
.requestToInit()
110+
.pipe(takeUntil(this.destroy$))
111+
.subscribe(option => this.setup(option));
108112
}
109113

110114
ngOnDestroy(): void {
@@ -144,19 +148,30 @@ export class NzCodeEditorComponent implements OnDestroy, AfterViewInit {
144148
}
145149

146150
private setup(option: JoinedEditorOptions): void {
147-
inNextTick().subscribe(() => {
148-
this.editorOptionCached = option;
149-
this.registerOptionChanges();
150-
this.initMonacoEditorInstance();
151-
this.registerResizeChange();
152-
this.setValue();
153-
154-
if (!this.nzFullControl) {
155-
this.setValueEmitter();
156-
}
157-
158-
this.nzEditorInitialized.emit(this.editorInstance!);
159-
});
151+
// The `setup()` is invoked when the Monaco editor is loaded. This may happen asynchronously for the first
152+
// time, and it'll always happen synchronously afterwards. The first `setup()` invokation is outside the Angular
153+
// zone, but further invokations will happen within the Angular zone. We call the `setModel()` on the editor
154+
// instance, which tells Monaco to add event listeners lazily internally (`mousemove`, `mouseout`, etc.).
155+
// We should avoid adding them within the Angular zone since this will drastically affect the performance.
156+
this.ngZone.runOutsideAngular(() =>
157+
inNextTick()
158+
.pipe(takeUntil(this.destroy$))
159+
.subscribe(() => {
160+
this.editorOptionCached = option;
161+
this.registerOptionChanges();
162+
this.initMonacoEditorInstance();
163+
this.registerResizeChange();
164+
this.setValue();
165+
166+
if (!this.nzFullControl) {
167+
this.setValueEmitter();
168+
}
169+
170+
if (this.nzEditorInitialized.observers.length) {
171+
this.ngZone.run(() => this.nzEditorInitialized.emit(this.editorInstance!));
172+
}
173+
})
174+
);
160175
}
161176

162177
private registerOptionChanges(): void {

0 commit comments

Comments
 (0)