Added warning when calling methods on a released event.

This commit is contained in:
Edvin Erikson
2015-08-25 14:45:56 +02:00
parent 10c8166043
commit 34af8f8597
2 changed files with 50 additions and 0 deletions
@@ -16,6 +16,7 @@ var PooledClass = require('PooledClass');
var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var warning = require('warning');
/**
* @interface Event
@@ -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 {
@@ -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 {
@@ -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.'
);
});
});