From 0f0395441cc5986238654fdea5d6bcd252103ff8 Mon Sep 17 00:00:00 2001 From: arturovt Date: Sun, 27 Mar 2022 06:18:55 +0300 Subject: [PATCH] perf(material/progress-bar): do not run change detection if there are no `animationEnd` listeners --- .../mdc-progress-bar/progress-bar.spec.ts | 51 +++++++++++++++---- .../mdc-progress-bar/progress-bar.ts | 4 ++ .../progress-bar/progress-bar.spec.ts | 51 +++++++++++++++---- src/material/progress-bar/progress-bar.ts | 4 ++ 4 files changed, 90 insertions(+), 20 deletions(-) diff --git a/src/material-experimental/mdc-progress-bar/progress-bar.spec.ts b/src/material-experimental/mdc-progress-bar/progress-bar.spec.ts index 9af4edd53f34..5d398004df60 100644 --- a/src/material-experimental/mdc-progress-bar/progress-bar.spec.ts +++ b/src/material-experimental/mdc-progress-bar/progress-bar.spec.ts @@ -1,5 +1,5 @@ import {TestBed, ComponentFixture} from '@angular/core/testing'; -import {Component, DebugElement, Provider, Type} from '@angular/core'; +import {ApplicationRef, Component, DebugElement, Provider, Type} from '@angular/core'; import {By} from '@angular/platform-browser'; import {dispatchFakeEvent} from '../../cdk/testing/private'; import {MatProgressBarModule, MAT_PROGRESS_BAR_DEFAULT_OPTIONS} from './index'; @@ -231,14 +231,16 @@ describe('MDC-based MatProgressBar', () => { it('should trigger output event on primary value bar animation end', () => { fixture.detectChanges(); - spyOn(progressComponent.animationEnd, 'next'); + + const animationEndSpy = jasmine.createSpy(); + progressComponent.animationEnd.subscribe(animationEndSpy); progressComponent.value = 40; - expect(progressComponent.animationEnd.next).not.toHaveBeenCalled(); + expect(animationEndSpy).not.toHaveBeenCalled(); // On animation end, output should be emitted. dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); - expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({value: 40}); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); }); }); @@ -267,27 +269,56 @@ describe('MDC-based MatProgressBar', () => { it('should trigger output event on primary value bar animation end', () => { fixture.detectChanges(); - spyOn(progressComponent.animationEnd, 'next'); + + const animationEndSpy = jasmine.createSpy(); + progressComponent.animationEnd.subscribe(animationEndSpy); progressComponent.value = 40; - expect(progressComponent.animationEnd.next).not.toHaveBeenCalled(); + expect(animationEndSpy).not.toHaveBeenCalled(); // On animation end, output should be emitted. dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); - expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({value: 40}); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); }); it('should trigger output event with value not bufferValue', () => { fixture.detectChanges(); - spyOn(progressComponent.animationEnd, 'next'); + + const animationEndSpy = jasmine.createSpy(); + progressComponent.animationEnd.subscribe(animationEndSpy); progressComponent.value = 40; progressComponent.bufferValue = 70; - expect(progressComponent.animationEnd.next).not.toHaveBeenCalled(); + expect(animationEndSpy).not.toHaveBeenCalled(); // On animation end, output should be emitted. dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); - expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({value: 40}); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); + }); + + it('should not run change detection if there are no `animationEnd` observers', () => { + fixture.detectChanges(); + + const animationEndSpy = jasmine.createSpy(); + const appRef = TestBed.inject(ApplicationRef); + spyOn(appRef, 'tick'); + + progressComponent.value = 30; + progressComponent.bufferValue = 60; + // On animation end, output should be emitted. + dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); + + expect(appRef.tick).not.toHaveBeenCalled(); + + progressComponent.animationEnd.subscribe(animationEndSpy); + + progressComponent.value = 40; + progressComponent.bufferValue = 70; + // On animation end, output should be emitted. + dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); + + expect(appRef.tick).toHaveBeenCalled(); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); }); }); }); diff --git a/src/material-experimental/mdc-progress-bar/progress-bar.ts b/src/material-experimental/mdc-progress-bar/progress-bar.ts index 4fae16275599..ae5668609ef5 100644 --- a/src/material-experimental/mdc-progress-bar/progress-bar.ts +++ b/src/material-experimental/mdc-progress-bar/progress-bar.ts @@ -227,6 +227,10 @@ export class MatProgressBar ) .pipe(filter((e: TransitionEvent) => e.target === this._primaryBar)) .subscribe(() => { + if (this.animationEnd.observers.length === 0) { + return; + } + if (this.mode === 'determinate' || this.mode === 'buffer') { this._ngZone.run(() => this.animationEnd.next({value: this.value})); } diff --git a/src/material/progress-bar/progress-bar.spec.ts b/src/material/progress-bar/progress-bar.spec.ts index a2a0b0844d65..8c44999795d9 100644 --- a/src/material/progress-bar/progress-bar.spec.ts +++ b/src/material/progress-bar/progress-bar.spec.ts @@ -1,5 +1,5 @@ import {TestBed, ComponentFixture} from '@angular/core/testing'; -import {Component, DebugElement, Provider, Type} from '@angular/core'; +import {ApplicationRef, Component, DebugElement, Provider, Type} from '@angular/core'; import {By} from '@angular/platform-browser'; import {dispatchFakeEvent} from '../../cdk/testing/private'; import {MatProgressBarModule, MAT_PROGRESS_BAR_LOCATION} from './index'; @@ -270,14 +270,16 @@ describe('MatProgressBar', () => { it('should trigger output event on primary value bar animation end', () => { fixture.detectChanges(); - spyOn(progressComponent.animationEnd, 'next'); + + const animationEndSpy = jasmine.createSpy(); + progressComponent.animationEnd.subscribe(animationEndSpy); progressComponent.value = 40; - expect(progressComponent.animationEnd.next).not.toHaveBeenCalled(); + expect(animationEndSpy).not.toHaveBeenCalled(); // On animation end, output should be emitted. dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); - expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({value: 40}); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); }); }); @@ -306,27 +308,56 @@ describe('MatProgressBar', () => { it('should trigger output event on primary value bar animation end', () => { fixture.detectChanges(); - spyOn(progressComponent.animationEnd, 'next'); + + const animationEndSpy = jasmine.createSpy(); + progressComponent.animationEnd.subscribe(animationEndSpy); progressComponent.value = 40; - expect(progressComponent.animationEnd.next).not.toHaveBeenCalled(); + expect(animationEndSpy).not.toHaveBeenCalled(); // On animation end, output should be emitted. dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); - expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({value: 40}); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); }); it('should trigger output event with value not bufferValue', () => { fixture.detectChanges(); - spyOn(progressComponent.animationEnd, 'next'); + + const animationEndSpy = jasmine.createSpy(); + progressComponent.animationEnd.subscribe(animationEndSpy); progressComponent.value = 40; progressComponent.bufferValue = 70; - expect(progressComponent.animationEnd.next).not.toHaveBeenCalled(); + expect(animationEndSpy).not.toHaveBeenCalled(); // On animation end, output should be emitted. dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); - expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({value: 40}); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); + }); + + it('should not run change detection if there are no `animationEnd` observers', () => { + fixture.detectChanges(); + + const animationEndSpy = jasmine.createSpy(); + const appRef = TestBed.inject(ApplicationRef); + spyOn(appRef, 'tick'); + + progressComponent.value = 30; + progressComponent.bufferValue = 60; + // On animation end, output should be emitted. + dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); + + expect(appRef.tick).not.toHaveBeenCalled(); + + progressComponent.animationEnd.subscribe(animationEndSpy); + + progressComponent.value = 40; + progressComponent.bufferValue = 70; + // On animation end, output should be emitted. + dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend'); + + expect(appRef.tick).toHaveBeenCalled(); + expect(animationEndSpy).toHaveBeenCalledWith({value: 40}); }); }); }); diff --git a/src/material/progress-bar/progress-bar.ts b/src/material/progress-bar/progress-bar.ts index c35b625ab09f..ac40d65a6ede 100644 --- a/src/material/progress-bar/progress-bar.ts +++ b/src/material/progress-bar/progress-bar.ts @@ -250,6 +250,10 @@ export class MatProgressBar ) .pipe(filter((e: TransitionEvent) => e.target === element)) .subscribe(() => { + if (this.animationEnd.observers.length === 0) { + return; + } + if (this.mode === 'determinate' || this.mode === 'buffer') { this._ngZone.run(() => this.animationEnd.next({value: this.value})); }