From eb9eebbb45352da6000560ee0988d9d59dce1254 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Wed, 14 Oct 2020 16:57:00 +0300 Subject: [PATCH] refactor(elements): simplify code after IE<11 support removal (#39265) Support for IE<11 is being removed in v11. PR #39090 removed some code that was no longer needed. Now that there are no longer multiple code-paths (which was previously needed for IE<11 support), this commit simplifies the code further (for example, to avoid unnecessary functions calls and to avoid iterating over a component's inputs multiple times). PR Close #39265 --- .../elements/src/create-custom-element.ts | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/packages/elements/src/create-custom-element.ts b/packages/elements/src/create-custom-element.ts index ffe0f00409..3198d07f79 100644 --- a/packages/elements/src/create-custom-element.ts +++ b/packages/elements/src/create-custom-element.ts @@ -151,18 +151,19 @@ export function createCustomElement

( const strategy = this._ngElementStrategy = strategyFactory.create(this.injector || config.injector); - // Collect pre-existing values on the element to re-apply through the strategy. - const preExistingValues = - inputs.filter(({propName}) => this.hasOwnProperty(propName)).map(({propName}): [ - string, any - ] => [propName, (this as any)[propName]]); + // Re-apply pre-existing input values (set as properties on the element) through the + // strategy. + inputs.forEach(({propName}) => { + if (!this.hasOwnProperty(propName)) { + // No pre-existing value for `propName`. + return; + } - // Delete the property from the instance, so that it can go through the getters/setters - // set on `NgElementImpl.prototype`. - preExistingValues.forEach(([propName]) => delete (this as any)[propName]); - - // Re-apply pre-existing values through the strategy. - preExistingValues.forEach(([propName, value]) => strategy.setInputValue(propName, value)); + // Delete the property from the instance and re-apply it through the strategy. + const value = (this as any)[propName]; + delete (this as any)[propName]; + strategy.setInputValue(propName, value); + }); } return this._ngElementStrategy!; @@ -229,17 +230,8 @@ export function createCustomElement

( } // Add getters and setters to the prototype for each property input. - defineInputGettersSetters(inputs, NgElementImpl.prototype); - - return (NgElementImpl as any) as NgElementConstructor

; -} - -// Helpers -function defineInputGettersSetters( - inputs: {propName: string, templateName: string}[], target: object): void { - // Add getters and setters for each property input. inputs.forEach(({propName}) => { - Object.defineProperty(target, propName, { + Object.defineProperty(NgElementImpl.prototype, propName, { get(): any { return this.ngElementStrategy.getInputValue(propName); }, @@ -250,4 +242,6 @@ function defineInputGettersSetters( enumerable: true, }); }); + + return (NgElementImpl as any) as NgElementConstructor

; }