@@ -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,44 @@ SyntheticEvent.augmentClass = function(Class, Interface) {
195
198
PooledClass . addPoolingTo ( SyntheticEvent , PooledClass . fourArgumentPooler ) ;
196
199
197
200
module . exports = SyntheticEvent ;
201
+
202
+
203
+ // Utility functions
204
+
205
+ /**
206
+ * Helper to nullify syntheticEvent instance properties when destructing
207
+ *
208
+ * @param {object } SyntheticEvent
209
+ * @param {String } propName
210
+ */
211
+ function getPooledWarningPropertyDefinition ( propName , getVal ) {
212
+ var setTo = typeof getVal === 'function' ? 'a no-op function' : 'set to null' ;
213
+ return {
214
+ configurable : true ,
215
+ set : function ( val ) {
216
+ var warningCondition = false ;
217
+ warning (
218
+ warningCondition ,
219
+ 'This synthetic event is reused for performance reasons. If you\'re ' +
220
+ 'seeing this, you\'re setting property `%s` on a ' +
221
+ 'released/nullified synthetic event. This is effectively a no-op. See ' +
222
+ 'https://fb.me/react-event-pooling for more information.' ,
223
+ propName
224
+ ) ;
225
+ return val ;
226
+ } ,
227
+ get : function ( ) {
228
+ var warningCondition = false ;
229
+ warning (
230
+ warningCondition ,
231
+ 'This synthetic event is reused for performance reasons. If you\'re ' +
232
+ 'seeing this, you\'re accessing property `%s` on a ' +
233
+ 'released/nullified synthetic event. This is %s. See ' +
234
+ 'https://fb.me/react-event-pooling for more information.' ,
235
+ propName ,
236
+ setTo
237
+ ) ;
238
+ return getVal ;
239
+ } ,
240
+ } ;
241
+ }
0 commit comments