Skip to content

Commit

Permalink
Added warning when calling methods on a released event.
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvin Erikson committed Aug 25, 2015
1 parent 10c8166 commit 34af8f8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/renderers/dom/client/syntheticEvents/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var PooledClass = require('PooledClass');

var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var warning = require('warning');

/**
* @interface Event
Expand Down Expand Up @@ -89,6 +90,16 @@ assign(SyntheticEvent.prototype, {
preventDefault: function() {
this.defaultPrevented = true;
var event = this.nativeEvent;
if (__DEV__) {
warning(event,
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
'you\'re calling `preventDefault` on a released/nullified Synthetic event. ' +
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
'for more information.'
);
}
if (!event) return;

if (event.preventDefault) {
event.preventDefault();
} else {
Expand All @@ -99,6 +110,16 @@ assign(SyntheticEvent.prototype, {

stopPropagation: function() {
var event = this.nativeEvent;
if (__DEV__) {
warning(event,
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
'you\'re calling `stopPropagation` on a released/nullified Synthetic event. ' +
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
'for more information.'
);
}
if (!event) return;

if (event.stopPropagation) {
event.stopPropagation();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,33 @@ describe('SyntheticEvent', function() {
expect(syntheticEvent.isPersistent()).toBe(true);
});

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(
'Warning: ' +
'This Synthetic event is reused for performance reasons. If you\'re seeing this, ' +
'you\'re calling `preventDefault` on a released/nullified Synthetic event. ' +
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
'for more information.'
);
});

it('should warn if the Synthetic event has been released when calling `stopPropagation`', function() {
spyOn(console, 'error');
var syntheticEvent = createEvent({});
SyntheticEvent.release(syntheticEvent);
syntheticEvent.stopPropagation();
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 calling `stopPropagation` on a released/nullified Synthetic event. ' +
'This is a no-op. See https://facebook.github.io/react/docs/events.html#event-pooling ' +
'for more information.'
);
});
});

0 comments on commit 34af8f8

Please sign in to comment.