Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/observers): Run content changed callback in NgZone #28870

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 27 additions & 25 deletions src/cdk/observers/observe-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
OnDestroy,
Output,
booleanAttribute,
inject,
} from '@angular/core';
import {Observable, Observer, Subject, Subscription} from 'rxjs';
import {debounceTime, filter, map} from 'rxjs/operators';
Expand Down Expand Up @@ -74,6 +75,8 @@ export class ContentObserver implements OnDestroy {
}
>();

private _ngZone = inject(NgZone);

constructor(private _mutationObserverFactory: MutationObserverFactory) {}

ngOnDestroy() {
Expand Down Expand Up @@ -102,7 +105,11 @@ export class ContentObserver implements OnDestroy {
map(records => records.filter(record => !shouldIgnoreRecord(record))),
filter(records => !!records.length),
)
.subscribe(observer);
.subscribe(records => {
this._ngZone.run(() => {
observer.next(records);
});
});

return () => {
subscription.unsubscribe();
Expand All @@ -116,21 +123,23 @@ export class ContentObserver implements OnDestroy {
* new one if not.
*/
private _observeElement(element: Element): Subject<MutationRecord[]> {
if (!this._observedElements.has(element)) {
const stream = new Subject<MutationRecord[]>();
const observer = this._mutationObserverFactory.create(mutations => stream.next(mutations));
if (observer) {
observer.observe(element, {
characterData: true,
childList: true,
subtree: true,
});
return this._ngZone.runOutsideAngular(() => {
if (!this._observedElements.has(element)) {
const stream = new Subject<MutationRecord[]>();
const observer = this._mutationObserverFactory.create(mutations => stream.next(mutations));
if (observer) {
observer.observe(element, {
characterData: true,
childList: true,
subtree: true,
});
}
this._observedElements.set(element, {observer, stream, count: 1});
} else {
this._observedElements.get(element)!.count++;
}
this._observedElements.set(element, {observer, stream, count: 1});
} else {
this._observedElements.get(element)!.count++;
}
return this._observedElements.get(element)!.stream;
return this._observedElements.get(element)!.stream;
});
}

/**
Expand Down Expand Up @@ -202,7 +211,6 @@ export class CdkObserveContent implements AfterContentInit, OnDestroy {
constructor(
private _contentObserver: ContentObserver,
private _elementRef: ElementRef<HTMLElement>,
private _ngZone: NgZone,
) {}

ngAfterContentInit() {
Expand All @@ -219,15 +227,9 @@ export class CdkObserveContent implements AfterContentInit, OnDestroy {
this._unsubscribe();
const stream = this._contentObserver.observe(this._elementRef);

// TODO(mmalerba): We shouldn't be emitting on this @Output() outside the zone.
// Consider brining it back inside the zone next time we're making breaking changes.
// Bringing it back inside can cause things like infinite change detection loops and changed
// after checked errors if people's code isn't handling it properly.
this._ngZone.runOutsideAngular(() => {
this._currentSubscription = (
this.debounce ? stream.pipe(debounceTime(this.debounce)) : stream
).subscribe(this.event);
});
this._currentSubscription = (
this.debounce ? stream.pipe(debounceTime(this.debounce)) : stream
).subscribe(this.event);
}

private _unsubscribe() {
Expand Down
3 changes: 1 addition & 2 deletions tools/public_api_guard/cdk/observers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import { AfterContentInit } from '@angular/core';
import { ElementRef } from '@angular/core';
import { EventEmitter } from '@angular/core';
import * as i0 from '@angular/core';
import { NgZone } from '@angular/core';
import { NumberInput } from '@angular/cdk/coercion';
import { Observable } from 'rxjs';
import { OnDestroy } from '@angular/core';

// @public
export class CdkObserveContent implements AfterContentInit, OnDestroy {
constructor(_contentObserver: ContentObserver, _elementRef: ElementRef<HTMLElement>, _ngZone: NgZone);
constructor(_contentObserver: ContentObserver, _elementRef: ElementRef<HTMLElement>);
get debounce(): number;
set debounce(value: NumberInput);
get disabled(): boolean;
Expand Down