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
This commit is contained in:
George Kalpakas 2020-10-14 16:57:00 +03:00 committed by atscott
parent aee2d3f994
commit eb9eebbb45

View File

@ -151,18 +151,19 @@ export function createCustomElement<P>(
const strategy = this._ngElementStrategy = const strategy = this._ngElementStrategy =
strategyFactory.create(this.injector || config.injector); strategyFactory.create(this.injector || config.injector);
// Collect pre-existing values on the element to re-apply through the strategy. // Re-apply pre-existing input values (set as properties on the element) through the
const preExistingValues = // strategy.
inputs.filter(({propName}) => this.hasOwnProperty(propName)).map(({propName}): [ inputs.forEach(({propName}) => {
string, any if (!this.hasOwnProperty(propName)) {
] => [propName, (this as any)[propName]]); // No pre-existing value for `propName`.
return;
}
// Delete the property from the instance, so that it can go through the getters/setters // Delete the property from the instance and re-apply it through the strategy.
// set on `NgElementImpl.prototype`. const value = (this as any)[propName];
preExistingValues.forEach(([propName]) => delete (this as any)[propName]); delete (this as any)[propName];
strategy.setInputValue(propName, value);
// Re-apply pre-existing values through the strategy. });
preExistingValues.forEach(([propName, value]) => strategy.setInputValue(propName, value));
} }
return this._ngElementStrategy!; return this._ngElementStrategy!;
@ -229,17 +230,8 @@ export function createCustomElement<P>(
} }
// Add getters and setters to the prototype for each property input. // Add getters and setters to the prototype for each property input.
defineInputGettersSetters(inputs, NgElementImpl.prototype);
return (NgElementImpl as any) as NgElementConstructor<P>;
}
// Helpers
function defineInputGettersSetters(
inputs: {propName: string, templateName: string}[], target: object): void {
// Add getters and setters for each property input.
inputs.forEach(({propName}) => { inputs.forEach(({propName}) => {
Object.defineProperty(target, propName, { Object.defineProperty(NgElementImpl.prototype, propName, {
get(): any { get(): any {
return this.ngElementStrategy.getInputValue(propName); return this.ngElementStrategy.getInputValue(propName);
}, },
@ -250,4 +242,6 @@ function defineInputGettersSetters(
enumerable: true, enumerable: true,
}); });
}); });
return (NgElementImpl as any) as NgElementConstructor<P>;
} }