Skip to content

Commit

Permalink
refactor(common): do not run CD when verifying image distortion (angu…
Browse files Browse the repository at this point in the history
…lar#50723)

This commit wraps the `assertNonZeroRenderedHeight` and `assertNoImageDistortion`
with `runOutsideAngular` to setup the `load` event listener outside of the Angular zone.
This was previously causing extra change detection cycles in development mode and
interfered debugging stuff.

PR Close angular#50723
  • Loading branch information
arturovt authored and pkozlowski-opensource committed Jun 20, 2023
1 parent 43a80ca commit 076d449
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
/** @nodoc */
ngOnInit() {
if (ngDevMode) {
const ngZone = this.injector.get(NgZone);
assertNonEmptyInput(this, 'ngSrc', this.ngSrc);
assertValidNgSrcset(this, this.ngSrcset);
assertNoConflictingSrc(this);
Expand All @@ -341,7 +342,10 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
assertNotBlobUrl(this);
if (this.fill) {
assertEmptyWidthAndHeight(this);
assertNonZeroRenderedHeight(this, this.imgElement, this.renderer);
// This leaves the Angular zone to avoid triggering unnecessary change detection cycles when
// `load` tasks are invoked on images.
ngZone.runOutsideAngular(
() => assertNonZeroRenderedHeight(this, this.imgElement, this.renderer));
} else {
assertNonEmptyWidthAndHeight(this);
if (this.height !== undefined) {
Expand All @@ -352,7 +356,8 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
}
// Only check for distorted images when not in fill mode, where
// images may be intentionally stretched, cropped or letterboxed.
assertNoImageDistortion(this, this.imgElement, this.renderer);
ngZone.runOutsideAngular(
() => assertNoImageDistortion(this, this.imgElement, this.renderer));
}
assertValidLoadingInput(this);
if (!this.ngSrcset) {
Expand All @@ -369,7 +374,6 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
// the `priority` attribute is missing. Otherwise, an image
// has the necessary settings and no extra checks are required.
if (this.lcpObserver !== null) {
const ngZone = this.injector.get(NgZone);
ngZone.runOutsideAngular(() => {
this.lcpObserver!.registerImage(this.getRewrittenSrc(), this.ngSrc);
});
Expand Down

0 comments on commit 076d449

Please sign in to comment.