Skip to content

Commit

Permalink
perf(module:icon): do not run change detection when changing icon (#7071
Browse files Browse the repository at this point in the history
)

* perf(module:icon): do not run change detection when changing icon

* fix: do not run `markForCheck()` for the view event handler
  • Loading branch information
arturovt committed Dec 16, 2021
1 parent baf7f0a commit e998e4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
31 changes: 24 additions & 7 deletions components/icon/icon.directive.ts
Expand Up @@ -8,12 +8,16 @@ import {
Directive,
ElementRef,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Optional,
Renderer2,
SimpleChanges
} from '@angular/core';
import { from, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { IconDirective, ThemeType } from '@ant-design/icons-angular';

Expand All @@ -29,7 +33,7 @@ import { NzIconPatchService, NzIconService } from './icon.service';
'[class.anticon]': 'true'
}
})
export class NzIconDirective extends IconDirective implements OnInit, OnChanges, AfterContentChecked {
export class NzIconDirective extends IconDirective implements OnInit, OnChanges, AfterContentChecked, OnDestroy {
static ngAcceptInputType_nzSpin: BooleanInput;

cacheClassName: string | null = null;
Expand Down Expand Up @@ -67,7 +71,10 @@ export class NzIconDirective extends IconDirective implements OnInit, OnChanges,
private iconfont?: string;
private spin: boolean = false;

private destroy$ = new Subject<void>();

constructor(
private ngZone: NgZone,
elementRef: ElementRef,
public iconService: NzIconService,
public renderer: Renderer2,
Expand Down Expand Up @@ -116,17 +123,27 @@ export class NzIconDirective extends IconDirective implements OnInit, OnChanges,
}
}

ngOnDestroy(): void {
this.destroy$.next();
}

/**
* Replacement of `changeIcon` for more modifications.
*/
private changeIcon2(): void {
this.setClassName();
this._changeIcon().then(svgOrRemove => {
if (svgOrRemove) {
this.setSVGData(svgOrRemove);
this.handleSpin(svgOrRemove);
this.handleRotate(svgOrRemove);
}

// We don't need to re-enter the Angular zone for adding classes or attributes through the renderer.
this.ngZone.runOutsideAngular(() => {
from(this._changeIcon())
.pipe(takeUntil(this.destroy$))
.subscribe(svgOrRemove => {
if (svgOrRemove) {
this.setSVGData(svgOrRemove);
this.handleSpin(svgOrRemove);
this.handleRotate(svgOrRemove);
}
});
});
}

Expand Down
1 change: 0 additions & 1 deletion components/menu/submenu.component.ts
Expand Up @@ -194,7 +194,6 @@ export class NzSubMenuComponent implements OnInit, OnDestroy, AfterContentInit,
} else if (placement === 'leftTop' || placement === 'leftBottom' || placement === 'left') {
this.position = 'left';
}
this.cdr.markForCheck();
}

constructor(
Expand Down

0 comments on commit e998e4a

Please sign in to comment.