@@ -55,6 +55,13 @@ var EventInterface = {
55
55
* @param {DOMEventTarget } nativeEventTarget Target node.
56
56
*/
57
57
function SyntheticEvent ( dispatchConfig , targetInst , nativeEvent , nativeEventTarget ) {
58
+ if ( __DEV__ ) {
59
+ // these have a getter/setter for warnings
60
+ delete this . nativeEvent ;
61
+ delete this . preventDefault ;
62
+ delete this . stopPropagation ;
63
+ }
64
+
58
65
this . dispatchConfig = dispatchConfig ;
59
66
this . _targetInst = targetInst ;
60
67
this . nativeEvent = nativeEvent ;
@@ -64,6 +71,9 @@ function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarg
64
71
if ( ! Interface . hasOwnProperty ( propName ) ) {
65
72
continue ;
66
73
}
74
+ if ( __DEV__ ) {
75
+ delete this [ propName ] ; // this has a getter/setter for warnings
76
+ }
67
77
var normalize = Interface [ propName ] ;
68
78
if ( normalize ) {
69
79
this [ propName ] = normalize ( nativeEvent ) ;
@@ -92,15 +102,6 @@ assign(SyntheticEvent.prototype, {
92
102
preventDefault : function ( ) {
93
103
this . defaultPrevented = true ;
94
104
var event = this . nativeEvent ;
95
- if ( __DEV__ ) {
96
- warning (
97
- event ,
98
- 'This synthetic event is reused for performance reasons. If you\'re ' +
99
- 'seeing this, you\'re calling `preventDefault` on a ' +
100
- 'released/nullified synthetic event. This is a no-op. See ' +
101
- 'https://fb.me/react-event-pooling for more information.'
102
- ) ;
103
- }
104
105
if ( ! event ) {
105
106
return ;
106
107
}
@@ -115,15 +116,6 @@ assign(SyntheticEvent.prototype, {
115
116
116
117
stopPropagation : function ( ) {
117
118
var event = this . nativeEvent ;
118
- if ( __DEV__ ) {
119
- warning (
120
- event ,
121
- 'This synthetic event is reused for performance reasons. If you\'re ' +
122
- 'seeing this, you\'re calling `stopPropagation` on a ' +
123
- 'released/nullified synthetic event. This is a no-op. See ' +
124
- 'https://fb.me/react-event-pooling for more information.'
125
- ) ;
126
- }
127
119
if ( ! event ) {
128
120
return ;
129
121
}
@@ -158,11 +150,22 @@ assign(SyntheticEvent.prototype, {
158
150
destructor : function ( ) {
159
151
var Interface = this . constructor . Interface ;
160
152
for ( var propName in Interface ) {
161
- this [ propName ] = null ;
153
+ if ( __DEV__ ) {
154
+ Object . defineProperty ( this , propName , getPooledWarningPropertyDefinition ( propName , Interface [ propName ] ) ) ;
155
+ } else {
156
+ this [ propName ] = null ;
157
+ }
158
+ }
159
+ if ( __DEV__ ) {
160
+ var noop = require ( 'emptyFunction' ) ;
161
+ Object . defineProperty ( this , 'nativeEvent' , getPooledWarningPropertyDefinition ( 'nativeEvent' , null ) ) ;
162
+ Object . defineProperty ( this , 'preventDefault' , getPooledWarningPropertyDefinition ( 'preventDefault' , noop ) ) ;
163
+ Object . defineProperty ( this , 'stopPropagation' , getPooledWarningPropertyDefinition ( 'stopPropagation' , noop ) ) ;
164
+ } else {
165
+ this . nativeEvent = null ;
162
166
}
163
167
this . dispatchConfig = null ;
164
168
this . _targetInst = null ;
165
- this . nativeEvent = null ;
166
169
} ,
167
170
168
171
} ) ;
@@ -195,3 +198,46 @@ SyntheticEvent.augmentClass = function(Class, Interface) {
195
198
PooledClass . addPoolingTo ( SyntheticEvent , PooledClass . fourArgumentPooler ) ;
196
199
197
200
module . exports = SyntheticEvent ;
201
+
202
+ /**
203
+ * Helper to nullify syntheticEvent instance properties when destructing
204
+ *
205
+ * @param {object } SyntheticEvent
206
+ * @param {String } propName
207
+ * @return {object } defineProperty object
208
+ */
209
+ function getPooledWarningPropertyDefinition ( propName , getVal ) {
210
+ var isFunction = typeof getVal === 'function' ;
211
+ return {
212
+ configurable : true ,
213
+ set : set ,
214
+ get : get ,
215
+ } ;
216
+
217
+ function set ( val ) {
218
+ var action = isFunction ? 'setting the method' : 'setting the property' ;
219
+ warn ( action , 'This is effectively a no-op' ) ;
220
+ return val ;
221
+ }
222
+
223
+ function get ( ) {
224
+ var action = isFunction ? 'accessing the method' : 'accessing the property' ;
225
+ var result = isFunction ? 'This is a no-op function' : 'This is set to null' ;
226
+ warn ( action , result ) ;
227
+ return getVal ;
228
+ }
229
+
230
+ function warn ( action , result ) {
231
+ var warningCondition = false ;
232
+ warning (
233
+ warningCondition ,
234
+ 'This synthetic event is reused for performance reasons. If you\'re seeing this,' +
235
+ 'you\'re %s `%s` on a released/nullified synthetic event. %s.' +
236
+ 'If you must keep the original synthetic event around use event.persist().' +
237
+ 'See https://fb.me/react-event-pooling for more information.' ,
238
+ action ,
239
+ propName ,
240
+ result
241
+ ) ;
242
+ }
243
+ }
0 commit comments