Skip to content

Commit

Permalink
Add a warning for adding a property in the SyntheticEvent object
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Jan 30, 2016
1 parent 9c3f595 commit 4e6ceb4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/renderers/dom/client/syntheticEvents/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var warning = require('warning');

var didWarnAddedProperty = false;

/**
* @interface Event
* @see http://www.w3.org/TR/DOM-Level-3-Events/
Expand Down Expand Up @@ -157,14 +159,33 @@ assign(SyntheticEvent.prototype, {
*/
destructor: function() {
var Interface = this.constructor.Interface;
for (var propName in Interface) {
var propName;
for (propName in Interface) {
this[propName] = null;
}
this.dispatchConfig = null;
this._targetInst = null;
this.nativeEvent = null;
},
this.isDefaultPrevented = null;
this.isPropagationStopped = null;

if (__DEV__) {
if (!didWarnAddedProperty) {
for (propName in this) {
if (this.hasOwnProperty(propName) && this[propName] != null) {
warning(
didWarnAddedProperty,
'This synthetic event is reused for performance reasons. If you\'re ' +
'seeing this, you\'re adding a property in the synthetic event object.' +
'The property is never released. See ' +
'https://fb.me/react-event-pooling for more information.'
);
didWarnAddedProperty = true;
}
}
}
}
},
});

SyntheticEvent.Interface = EventInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,19 @@ describe('SyntheticEvent', function() {
'https://fb.me/react-event-pooling for more information.'
);
});

it('should warn if the synthetic event is added a property', function() {
spyOn(console, 'error');
var syntheticEvent = createEvent({});
syntheticEvent.foo = 'bar';
SyntheticEvent.release(syntheticEvent);
expect(syntheticEvent.foo).toBe('bar');
expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: This synthetic event is reused for performance reasons. If you\'re ' +
'seeing this, you\'re adding a property in the synthetic event object.' +
'The property is never released. See ' +
'https://fb.me/react-event-pooling for more information.'
);
});
});

0 comments on commit 4e6ceb4

Please sign in to comment.