From fdf4309b50e7ff4823ec8125a48ed58b80031beb Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 12 Oct 2016 15:20:22 -0700 Subject: [PATCH] perf(common): optimize NgSwitch default case relates to #11297 --- .../common/src/directives/ng_switch.ts | 28 +++++++++++-------- .../common/test/directives/ng_switch_spec.ts | 22 ++------------- .../benchmarks/src/tree/ng2_switch/tree.ts | 6 +++- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/modules/@angular/common/src/directives/ng_switch.ts b/modules/@angular/common/src/directives/ng_switch.ts index 32b3254d05a15..380be01cf0542 100644 --- a/modules/@angular/common/src/directives/ng_switch.ts +++ b/modules/@angular/common/src/directives/ng_switch.ts @@ -10,7 +10,7 @@ import {Directive, Host, Input, TemplateRef, ViewContainerRef} from '@angular/co import {ListWrapper} from '../facade/collection'; -const _CASE_DEFAULT = new Object(); +const _CASE_DEFAULT = {}; export class SwitchView { constructor( @@ -53,8 +53,7 @@ export class SwitchView { * root elements. * * Elements within `NgSwitch` but outside of a `NgSwitchCase` or `NgSwitchDefault` directives will - * be - * preserved at the location. + * be preserved at the location. * * The `ngSwitchCase` directive informs the parent `NgSwitch` of which view to display when the * expression is evaluated. @@ -72,18 +71,23 @@ export class NgSwitch { @Input() set ngSwitch(value: any) { - // Empty the currently active ViewContainers - this._emptyAllActiveViews(); - - // Add the ViewContainers matching the value (with a fallback to default) - this._useDefault = false; + // Set of views to display for this value let views = this._valueViews.get(value); - if (!views) { + + if (views) { + this._useDefault = false; + } else { + // No view to display for the current value -> default case + // Nothing to do if the default case was already active + if (this._useDefault) { + return; + } this._useDefault = true; - views = this._valueViews.get(_CASE_DEFAULT) || null; + views = this._valueViews.get(_CASE_DEFAULT); } - this._activateViews(views); + this._emptyAllActiveViews(); + this._activateViews(views); this._switchValue = value; } @@ -119,7 +123,7 @@ export class NgSwitch { this._activeViews = []; } - private _activateViews(views: SwitchView[]): void { + private _activateViews(views?: SwitchView[]): void { if (views) { for (var i = 0; i < views.length; i++) { views[i].create(); diff --git a/modules/@angular/common/test/directives/ng_switch_spec.ts b/modules/@angular/common/test/directives/ng_switch_spec.ts index 978a367f166bc..25011460b96c7 100644 --- a/modules/@angular/common/test/directives/ng_switch_spec.ts +++ b/modules/@angular/common/test/directives/ng_switch_spec.ts @@ -50,25 +50,6 @@ export function main() { detectChangesAndExpectText('when b'); })); - // TODO(robwormald): deprecate and remove - it('should switch amongst when values using switchCase', async(() => { - const template = '
' + - '
'; - - fixture = createTestComponent(template); - - detectChangesAndExpectText(''); - - getComponent().switchValue = 'a'; - detectChangesAndExpectText('when a'); - - getComponent().switchValue = 'b'; - detectChangesAndExpectText('when b'); - })); - it('should switch amongst when values with fallback to default', async(() => { const template = '
' + '