-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a warning for adding a property in the SyntheticEvent object #5947
Merged
jimfb
merged 1 commit into
facebook:master
from
koba04:add-warning-for-adding-synthetic-event-property
Feb 24, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,19 @@ var assign = require('Object.assign'); | |
var emptyFunction = require('emptyFunction'); | ||
var warning = require('warning'); | ||
|
||
var didWarnForAddedNewProperty = false; | ||
var isProxySupported = typeof Proxy === 'function'; | ||
|
||
var shouldBeReleasedProperties = [ | ||
'dispatchConfig', | ||
'_targetInst', | ||
'nativeEvent', | ||
'isDefaultPrevented', | ||
'isPropagationStopped', | ||
'_dispatchListeners', | ||
'_dispatchInstances', | ||
]; | ||
|
||
/** | ||
* @interface Event | ||
* @see http://www.w3.org/TR/DOM-Level-3-Events/ | ||
|
@@ -57,7 +70,7 @@ var EventInterface = { | |
function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarget) { | ||
if (__DEV__) { | ||
// these have a getter/setter for warnings | ||
delete this.nativeEvent; | ||
delete this.nativeEvent; | ||
delete this.preventDefault; | ||
delete this.stopPropagation; | ||
} | ||
|
@@ -95,6 +108,7 @@ function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarg | |
this.isDefaultPrevented = emptyFunction.thatReturnsFalse; | ||
} | ||
this.isPropagationStopped = emptyFunction.thatReturnsFalse; | ||
return this; | ||
} | ||
|
||
assign(SyntheticEvent.prototype, { | ||
|
@@ -156,22 +170,52 @@ assign(SyntheticEvent.prototype, { | |
this[propName] = null; | ||
} | ||
} | ||
for (var i = 0; i < shouldBeReleasedProperties.length; i++) { | ||
this[shouldBeReleasedProperties[i]] = null; | ||
} | ||
if (__DEV__) { | ||
var noop = require('emptyFunction'); | ||
Object.defineProperty(this, 'nativeEvent', getPooledWarningPropertyDefinition('nativeEvent', null)); | ||
Object.defineProperty(this, 'preventDefault', getPooledWarningPropertyDefinition('preventDefault', noop)); | ||
Object.defineProperty(this, 'stopPropagation', getPooledWarningPropertyDefinition('stopPropagation', noop)); | ||
} else { | ||
this.nativeEvent = null; | ||
} | ||
this.dispatchConfig = null; | ||
this._targetInst = null; | ||
}, | ||
|
||
}); | ||
|
||
SyntheticEvent.Interface = EventInterface; | ||
|
||
if (__DEV__) { | ||
if (isProxySupported) { | ||
/*eslint-disable no-func-assign */ | ||
SyntheticEvent = new Proxy(SyntheticEvent, { | ||
construct: function(target, args) { | ||
return this.apply(target, {}, args); | ||
}, | ||
apply: function(constructor, that, args) { | ||
return new Proxy(constructor.apply(that, args), { | ||
set: function(target, prop, value) { | ||
if (prop !== 'isPersistent' && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this condition is for |
||
!target.constructor.Interface.hasOwnProperty(prop) && | ||
shouldBeReleasedProperties.indexOf(prop) === -1) { | ||
warning( | ||
didWarnForAddedNewProperty || target.isPersistent(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is to avoid a warning in the persistent event. |
||
'This synthetic event is reused for performance reasons. If you\'re ' + | ||
'seeing this, you\'re adding a new property in the synthetic event object. ' + | ||
'The property is never released. See ' + | ||
'https://fb.me/react-event-pooling for more information.' | ||
); | ||
didWarnForAddedNewProperty = true; | ||
} | ||
target[prop] = value; | ||
return true; | ||
}, | ||
}); | ||
}, | ||
}); | ||
/*eslint-enable no-func-assign */ | ||
} | ||
} | ||
/** | ||
* Helper to reduce boilerplate when creating subclasses. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all these are fine, just noting this to myself (or others) because it's a potential change in the behavior of this class, but I don't think it has any impact on the overall program behavior.