Skip to content

Commit bd9d4df

Browse files
committed
refactor(ivy): remove inputsPropertyName (angular#22716)
Closes angular#22591 PR Close angular#22716
1 parent 34e355a commit bd9d4df

File tree

7 files changed

+74
-66
lines changed

7 files changed

+74
-66
lines changed

packages/core/src/render3/definition.ts

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {Type} from '../type';
1515
import {resolveRendererType2} from '../view/util';
1616

1717
import {diPublic} from './di';
18-
import {ComponentDef, ComponentDefArgs, DirectiveDef, DirectiveDefArgs, PipeDef, PipeType} from './interfaces/definition';
18+
import {ComponentDef, ComponentDefArgs, DirectiveDef, DirectiveDefArgs, DirectiveDefFeature, PipeDef} from './interfaces/definition';
1919

2020

2121

@@ -45,7 +45,6 @@ export function defineComponent<T>(componentDefinition: ComponentDefArgs<T>): Co
4545
h: componentDefinition.hostBindings || noop,
4646
attributes: componentDefinition.attributes || null,
4747
inputs: invertObject(componentDefinition.inputs),
48-
inputsPropertyName: componentDefinition.inputsPropertyName || null,
4948
outputs: invertObject(componentDefinition.outputs),
5049
rendererType: resolveRendererType2(componentDefinition.rendererType) || null,
5150
exportAs: componentDefinition.exportAs,
@@ -72,48 +71,73 @@ type OnChangesExpando = OnChanges & {
7271
[key: string]: any;
7372
};
7473

75-
export function NgOnChangesFeature(definition: DirectiveDef<any>): void {
76-
const inputs = definition.inputs;
77-
const proto = definition.type.prototype;
78-
const inputsPropertyName = definition.inputsPropertyName;
79-
// Place where we will store SimpleChanges if there is a change
80-
Object.defineProperty(proto, PRIVATE_PREFIX, {value: undefined, writable: true});
81-
for (let pubKey in inputs) {
82-
const minKey = inputs[pubKey];
83-
const propertyName = inputsPropertyName && inputsPropertyName[minKey] || pubKey;
84-
const privateMinKey = PRIVATE_PREFIX + minKey;
85-
// Create a place where the actual value will be stored and make it non-enumerable
86-
Object.defineProperty(proto, privateMinKey, {value: undefined, writable: true});
87-
88-
const existingDesc = Object.getOwnPropertyDescriptor(proto, minKey);
89-
90-
// create a getter and setter for property
91-
Object.defineProperty(proto, minKey, {
92-
get: function(this: OnChangesExpando) {
93-
return (existingDesc && existingDesc.get) ? existingDesc.get.call(this) :
94-
this[privateMinKey];
95-
},
96-
set: function(this: OnChangesExpando, value: any) {
97-
let simpleChanges = this[PRIVATE_PREFIX];
98-
let isFirstChange = simpleChanges === undefined;
99-
if (simpleChanges == null) {
100-
simpleChanges = this[PRIVATE_PREFIX] = {};
74+
/**
75+
* Creates an NgOnChangesFeature function for a component's features list.
76+
*
77+
* It accepts an optional map of minified input property names to original property names,
78+
* if any input properties have a public alias.
79+
*
80+
* The NgOnChangesFeature function that is returned decorates a component with support for
81+
* the ngOnChanges lifecycle hook, so it should be included in any component that implements
82+
* that hook.
83+
*
84+
* Example usage:
85+
*
86+
* ```
87+
* static ngComponentDef = defineComponent({
88+
* ...
89+
* inputs: {name: 'publicName'},
90+
* features: [NgOnChangesFeature({name: 'name'})]
91+
* });
92+
* ```
93+
*
94+
* @param inputPropertyNames Map of input property names, if they are aliased
95+
* @returns DirectiveDefFeature
96+
*/
97+
export function NgOnChangesFeature(inputPropertyNames?: {[key: string]: string}):
98+
DirectiveDefFeature {
99+
return function(definition: DirectiveDef<any>): void {
100+
const inputs = definition.inputs;
101+
const proto = definition.type.prototype;
102+
// Place where we will store SimpleChanges if there is a change
103+
Object.defineProperty(proto, PRIVATE_PREFIX, {value: undefined, writable: true});
104+
for (let pubKey in inputs) {
105+
const minKey = inputs[pubKey];
106+
const propertyName = inputPropertyNames && inputPropertyNames[minKey] || pubKey;
107+
const privateMinKey = PRIVATE_PREFIX + minKey;
108+
// Create a place where the actual value will be stored and make it non-enumerable
109+
Object.defineProperty(proto, privateMinKey, {value: undefined, writable: true});
110+
111+
const existingDesc = Object.getOwnPropertyDescriptor(proto, minKey);
112+
113+
// create a getter and setter for property
114+
Object.defineProperty(proto, minKey, {
115+
get: function(this: OnChangesExpando) {
116+
return (existingDesc && existingDesc.get) ? existingDesc.get.call(this) :
117+
this[privateMinKey];
118+
},
119+
set: function(this: OnChangesExpando, value: any) {
120+
let simpleChanges = this[PRIVATE_PREFIX];
121+
let isFirstChange = simpleChanges === undefined;
122+
if (simpleChanges == null) {
123+
simpleChanges = this[PRIVATE_PREFIX] = {};
124+
}
125+
simpleChanges[propertyName] = new SimpleChange(this[privateMinKey], value, isFirstChange);
126+
(existingDesc && existingDesc.set) ? existingDesc.set.call(this, value) :
127+
this[privateMinKey] = value;
101128
}
102-
simpleChanges[propertyName] = new SimpleChange(this[privateMinKey], value, isFirstChange);
103-
(existingDesc && existingDesc.set) ? existingDesc.set.call(this, value) :
104-
this[privateMinKey] = value;
105-
}
106-
});
107-
}
129+
});
130+
}
108131

109-
// If an onInit hook is defined, it will need to wrap the ngOnChanges call
110-
// so the call order is changes-init-check in creation mode. In subsequent
111-
// change detection runs, only the check wrapper will be called.
112-
if (definition.onInit != null) {
113-
definition.onInit = onChangesWrapper(definition.onInit);
114-
}
132+
// If an onInit hook is defined, it will need to wrap the ngOnChanges call
133+
// so the call order is changes-init-check in creation mode. In subsequent
134+
// change detection runs, only the check wrapper will be called.
135+
if (definition.onInit != null) {
136+
definition.onInit = onChangesWrapper(definition.onInit);
137+
}
115138

116-
definition.doCheck = onChangesWrapper(definition.doCheck);
139+
definition.doCheck = onChangesWrapper(definition.doCheck);
140+
};
117141

118142
function onChangesWrapper(delegateHook: (() => void) | null) {
119143
return function(this: OnChangesExpando) {

packages/core/src/render3/interfaces/definition.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ export interface DirectiveDef<T> {
6868
*/
6969
readonly inputs: {[P in keyof T]: P};
7070

71-
/**
72-
* A dictionary mapping the inputs' minified property names to the original unminified property
73-
* names.
74-
*
75-
* An entry is added if and only if the alias is different from the property name.
76-
*/
77-
readonly inputsPropertyName: {[P in keyof T]: P};
78-
7971
/**
8072
* A dictionary mapping the outputs' minified property names to their public API names, which
8173
* are their aliases if any, or their original unminified property names
@@ -240,11 +232,6 @@ export interface DirectiveDefArgs<T> {
240232
*/
241233
inputs?: {[P in keyof T]?: string};
242234

243-
/**
244-
* TODO: Remove per https://github.com/angular/angular/issues/22591
245-
*/
246-
inputsPropertyName?: {[P in keyof T]?: string};
247-
248235
/**
249236
* A map of output names.
250237
*

packages/core/test/render3/common_with_def.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ NgForOf.ngDirectiveDef = defineDirective({
1919
factory: () => new NgForOfDef(
2020
injectViewContainerRef(), injectTemplateRef(),
2121
directiveInject(IterableDiffers, InjectFlags.Default, defaultIterableDiffers)),
22-
features: [NgOnChangesFeature],
22+
features: [NgOnChangesFeature()],
2323
inputs: {
2424
ngForOf: 'ngForOf',
2525
ngForTrackBy: 'ngForTrackBy',

packages/core/test/render3/compiler_canonical/life_cycle_spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ describe('lifecycle hooks', () => {
4545
factory: function LifecycleComp_Factory() { return new LifecycleComp(); },
4646
template: function LifecycleComp_Template(ctx: $LifecycleComp$, cm: $boolean$) {},
4747
inputs: {nameMin: 'name'},
48-
inputsPropertyName: {nameMin: 'nameMin'},
49-
features: [$r3$.ɵNgOnChangesFeature]
48+
features: [$r3$.ɵNgOnChangesFeature({nameMin: 'nameMin'})]
5049
});
5150
// /NORMATIVE
5251
}

packages/core/test/render3/define_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('define', () => {
3030
static ngDirectiveDef = defineDirective({
3131
type: MyDirective,
3232
factory: () => new MyDirective(),
33-
features: [NgOnChangesFeature],
33+
features: [NgOnChangesFeature()],
3434
inputs: {valA: 'valA', valB: 'valB'}
3535
});
3636
}

packages/core/test/render3/di_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ describe('di', () => {
420420
type: IfDirective,
421421
factory: () => new IfDirective(injectTemplateRef(), injectViewContainerRef()),
422422
inputs: {myIf: 'myIf'},
423-
features: [PublicFeature, NgOnChangesFeature]
423+
features: [PublicFeature, NgOnChangesFeature()]
424424
});
425425
}
426426

packages/core/test/render3/lifecycle_spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,9 +1987,8 @@ describe('lifecycles', () => {
19871987
type: Component,
19881988
tag: name,
19891989
factory: () => new Component(),
1990-
features: [NgOnChangesFeature],
1991-
inputs: {a: 'val1', b: 'publicName'},
1992-
inputsPropertyName: {b: 'val2'}, template
1990+
features: [NgOnChangesFeature({b: 'val2'})],
1991+
inputs: {a: 'val1', b: 'publicName'}, template
19931992
});
19941993
};
19951994
}
@@ -2007,9 +2006,8 @@ describe('lifecycles', () => {
20072006
static ngDirectiveDef = defineDirective({
20082007
type: Directive,
20092008
factory: () => new Directive(),
2010-
features: [NgOnChangesFeature],
2011-
inputs: {a: 'val1', b: 'publicName'},
2012-
inputsPropertyName: {b: 'val2'}
2009+
features: [NgOnChangesFeature({b: 'val2'})],
2010+
inputs: {a: 'val1', b: 'publicName'}
20132011
});
20142012
}
20152013

@@ -2397,7 +2395,7 @@ describe('lifecycles', () => {
23972395
tag: name,
23982396
factory: () => new Component(),
23992397
inputs: {val: 'val'}, template,
2400-
features: [NgOnChangesFeature]
2398+
features: [NgOnChangesFeature()]
24012399
});
24022400
};
24032401
}

0 commit comments

Comments
 (0)