-
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 warning when reading from event which has been returned to the pool #5940
Conversation
Rather than throw, I think it should generate a warning. |
I think these lines might be relevant: react/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js Lines 160 to 162 in 9c3f595
|
Ah, yes, that's right. Thanks for the reference 👍 |
cc49323
to
024c001
Compare
@kentcdodds updated the pull request. |
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 accessing a propertie on a ' + |
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.
"property" :)
https://github.com/facebook/react/blob/master/src/shared/utils/PooledClass.js#L102-L111 is another place to look. That's what get's run to add pooling to a class, generating a new class with a static |
Might be tricky as a "devtool" since you need to add getters, which doesn't fit so well into the devtool event framework (at least as I understand it). Definitely work looking into though |
024c001
to
e808985
Compare
Updated. This is technically working, but there are some potential issues that I'll add some inline comments about. |
it('should warn if the synthetic event has been released when calling `preventDefault`', function() { | ||
spyOn(console, 'error'); | ||
var syntheticEvent = createEvent({}); | ||
SyntheticEvent.release(syntheticEvent); | ||
syntheticEvent.preventDefault(); | ||
expect(console.error.calls.length).toBe(1); | ||
expect(console.error.argsForCall[0][0]).toBe( | ||
expect(console.error.calls.length).toBe(3); // once each for setting `defaultPrevented`, accessing `nativeEvent`, and accessing `preventDefault` |
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.
When calling preventDefault
, we set the defaultPrevented
access the nativeEvent
properties. This leads to three warnings even if the developer only called preventDefault
.
On possible suggestion is to set a property on the event called |
@kentcdodds updated the pull request. |
Heh... Still got some work on this, I've got quite a few failing tests in the full test suite and some odd behavior in the |
I think the problem is that when we restore an event, we need to re- Let me know if that sounds wrong. I'll push what I've got so far for review and keep working on it |
e808985
to
2c688f2
Compare
@kentcdodds updated the pull request. |
2c688f2
to
fa5582e
Compare
Great. I'm ready for feedback now. All tests are passing. I have a linting question I'll add as an inline comment. I'm not solid on this approach, so definitely willing to make changes to how things work or the style of the code. 👍 |
warning( | ||
warningCondition, | ||
'This synthetic event is reused for performance reasons. If you\'re ' + | ||
'seeing this, you\'re setting property `' + propName + '` on a ' + |
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'm getting a linting error:
190:13 error The second argument to warning must be a string literal react-internal/warning-and-invariant-args
I think it's because of this line. Is there a reason I can't provide the propName
here? I feel like it would be useful to have it.
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.
Perhaps you're supposed to use %s
there. Check out other warnings in the codebase.
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'd add a note about persist()
just before the link so the user doesn't overlook the solution they likely need.
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.
👍
@kentcdodds updated the pull request. |
* These are additional properties not in the EventInterface | ||
* which are nulled when destructoring a syntheticEvent instance | ||
*/ | ||
var otherNullableProps = ['dispatchConfig', '_targetInst', 'nativeEvent']; |
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.
dispatchConfig
and _targetInst
are both implementation details and are considered private fields.
I think we don't have to warn on accessing those.
This leaves us with nativeEvent
which can be hardcoded as a special case below.
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.
👍
@@ -195,3 +198,44 @@ SyntheticEvent.augmentClass = function(Class, Interface) { | |||
PooledClass.addPoolingTo(SyntheticEvent, PooledClass.fourArgumentPooler); | |||
|
|||
module.exports = SyntheticEvent; | |||
|
|||
|
|||
// Utility functions |
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.
Nit: I don't think this comment is necessary. Do we use similar comments in the codebase?
At this point I’ve given all feedback I could give, and what I see so far looks good, apart from minor nits above. Let’s wait for the maintainers to give their further comments. Thank you for contributing! cc @jimfb |
105c774
to
69770b3
Compare
@kentcdodds updated the pull request. |
69770b3
to
113facd
Compare
@kentcdodds updated the pull request. |
@@ -73,6 +73,7 @@ describe('SyntheticEvent', function() { | |||
}); | |||
|
|||
it('should be nullified if the synthetic event has called destructor', function() { | |||
spyOn(console, 'error'); // accessing properties on destructored events logs warnings (tested elsewhere) |
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.
We should still assert the warnings here. The reason being that we want to know if we start emitting some unexpected warnings. Right now, this test just swallows all warnings.
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.
That make sense. I felt odd spying on it and not asserting anything. Will do.
@kentcdodds Overall, this looks great to me. A couple of nitpicks. Also, I think it would be good to add an "integration" test (ie. render a component, simulate a click event, save the event, read from the event at the end of the test, and assert the warning fires). Just to sanity check that things are working. Otherwise, I think we're good to merge. |
Happy to write the integration test. I haven't looked into how to do that yet, but I'd appreciate it if you could point me in the right direction to do that :-) Thanks for the feedback! |
@kentcdodds A reasonable example is in ReactServerRendering-test.js, we have a test called "should have the correct mounting behavior". Specifically, the most interesting line is: A test would probably look something like this:
|
113facd
to
cf76b1c
Compare
@kentcdodds updated the pull request. |
Hi @jimfb. Sorry this took a bit. I've updated the tests and added an integration test as you suggested. I think it's solid. One thing that I did change in response to your comments is I combined two tests. When I asserted the console output in the one test, I realized that it was pretty much identical to another. So I just merged the two into one. Let me know if you'd like to see that change. Thanks for this opportunity to contribute! :D |
@kentcdodds This all looks good to me, thanks! But it looks like we broke lint (you can run locally with |
…leased syntheticEvents Closes facebook#5939
cf76b1c
to
6312852
Compare
(-‸ლ) thanks! The PR has been updated to fix linting. Looking forward to my next opportunity to contribute ⭐ |
@kentcdodds updated the pull request. |
Add warning when reading from event which has been returned to the pool
Thanks @kentcdodds! |
🎉 🎊 |
This is a WIP. I just want to make sure that I'm headed in the right direction for solving #5939
I'm not certain where the logic for deconstructing SyntheticEvents occurs. My guess is it's an abstraction that utilizes the
EventInterface
.Also, what's the proper way to reference
NODE_ENV
for doing this only in development mode.Thank you for helping a newbie to the codebase :-)