Skip to content

Commit 5ccf25d

Browse files
crisbetommalerba
authored andcommitted
perf(observe-content): run outside Angular zone (#6352)
* Runs the underlying `MutationObserver` outside the Angular zone. * Runs the debounce timeout outside the Angular zone. via angular/flex-layout#370.
1 parent 72c5d39 commit 5ccf25d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/cdk/observers/observe-content.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
OnDestroy,
1717
AfterContentInit,
1818
Injectable,
19+
NgZone,
1920
} from '@angular/core';
2021
import {Subject} from 'rxjs/Subject';
2122
import {RxChain, debounceTime} from '@angular/cdk/rxjs';
@@ -52,19 +53,24 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
5253

5354
constructor(
5455
private _mutationObserverFactory: MdMutationObserverFactory,
55-
private _elementRef: ElementRef) { }
56+
private _elementRef: ElementRef,
57+
private _ngZone: NgZone) { }
5658

5759
ngAfterContentInit() {
5860
if (this.debounce > 0) {
59-
RxChain.from(this._debouncer)
60-
.call(debounceTime, this.debounce)
61-
.subscribe((mutations: MutationRecord[]) => this.event.emit(mutations));
61+
this._ngZone.runOutsideAngular(() => {
62+
RxChain.from(this._debouncer)
63+
.call(debounceTime, this.debounce)
64+
.subscribe((mutations: MutationRecord[]) => this.event.emit(mutations));
65+
});
6266
} else {
6367
this._debouncer.subscribe(mutations => this.event.emit(mutations));
6468
}
6569

66-
this._observer = this._mutationObserverFactory.create((mutations: MutationRecord[]) => {
67-
this._debouncer.next(mutations);
70+
this._observer = this._ngZone.runOutsideAngular(() => {
71+
return this._mutationObserverFactory.create((mutations: MutationRecord[]) => {
72+
this._debouncer.next(mutations);
73+
});
6874
});
6975

7076
if (this._observer) {
@@ -79,8 +85,9 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
7985
ngOnDestroy() {
8086
if (this._observer) {
8187
this._observer.disconnect();
82-
this._debouncer.complete();
8388
}
89+
90+
this._debouncer.complete();
8491
}
8592
}
8693

0 commit comments

Comments
 (0)