From 4e6ceb48cdb9544dbf1f82e064a9d7fa7f235783 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Fri, 29 Jan 2016 19:13:08 +0900 Subject: [PATCH] Add a warning for adding a property in the SyntheticEvent object --- .../client/syntheticEvents/SyntheticEvent.js | 25 +++++++++++++++++-- .../__tests__/SyntheticEvent-test.js | 15 +++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js b/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js index 4a6c0a841276d..e086fb54e6045 100644 --- a/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js +++ b/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js @@ -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/ @@ -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; diff --git a/src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js b/src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js index e7e3d29fe36ef..13b0c9b3c636a 100644 --- a/src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js +++ b/src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js @@ -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.' + ); + }); });