@@ -15,7 +15,7 @@ import {Type} from '../type';
15
15
import { resolveRendererType2 } from '../view/util' ;
16
16
17
17
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' ;
19
19
20
20
21
21
@@ -45,7 +45,6 @@ export function defineComponent<T>(componentDefinition: ComponentDefArgs<T>): Co
45
45
h : componentDefinition . hostBindings || noop ,
46
46
attributes : componentDefinition . attributes || null ,
47
47
inputs : invertObject ( componentDefinition . inputs ) ,
48
- inputsPropertyName : componentDefinition . inputsPropertyName || null ,
49
48
outputs : invertObject ( componentDefinition . outputs ) ,
50
49
rendererType : resolveRendererType2 ( componentDefinition . rendererType ) || null ,
51
50
exportAs : componentDefinition . exportAs ,
@@ -72,48 +71,73 @@ type OnChangesExpando = OnChanges & {
72
71
[ key : string ] : any ;
73
72
} ;
74
73
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 ;
101
128
}
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
+ }
108
131
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
+ }
115
138
116
- definition . doCheck = onChangesWrapper ( definition . doCheck ) ;
139
+ definition . doCheck = onChangesWrapper ( definition . doCheck ) ;
140
+ } ;
117
141
118
142
function onChangesWrapper ( delegateHook : ( ( ) => void ) | null ) {
119
143
return function ( this : OnChangesExpando ) {
0 commit comments