Skip to content

Commit

Permalink
Use duck typing instead of allocating event objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfb committed Dec 17, 2015
1 parent 30ef056 commit 26f3785
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 74 deletions.
4 changes: 0 additions & 4 deletions src/renderers/dom/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
'use strict';

var ReactDOMComponentTree = require('ReactDOMComponentTree');
var ReactDOMDevtools = require('ReactDOMDevtools');
var ReactDOMUnknownPropertyDevtool = require('ReactDOMUnknownPropertyDevtool');
var ReactDefaultInjection = require('ReactDefaultInjection');
var ReactMount = require('ReactMount');
var ReactPerf = require('ReactPerf');
Expand Down Expand Up @@ -137,8 +135,6 @@ if (__DEV__) {
break;
}
}

ReactDOMDevtools.addDevtool(new ReactDOMUnknownPropertyDevtool());
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/renderers/dom/shared/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'use strict';

var DOMProperty = require('DOMProperty');
var ReactDOMDevtools = require('ReactDOMDevtools');
var ReactDOMInstrumentation = require('ReactDOMInstrumentation');
var ReactPerf = require('ReactPerf');

var quoteAttributeValueForBrowser = require('quoteAttributeValueForBrowser');
Expand Down Expand Up @@ -87,7 +87,9 @@ var DOMPropertyOperations = {
* @return {?string} Markup string, or null if the property was invalid.
*/
createMarkupForProperty: function(name, value) {
ReactDOMDevtools.emitEvent('createMarkupForProperty', {name, value});
if (__DEV__) {
ReactDOMInstrumentation.debugTool.onCreateMarkupForProperty(name, value);
}
var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
DOMProperty.properties[name] : null;
if (propertyInfo) {
Expand Down Expand Up @@ -131,7 +133,9 @@ var DOMPropertyOperations = {
* @param {*} value
*/
setValueForProperty: function(node, name, value) {
ReactDOMDevtools.emitEvent('setValueForProperty', {node, name, value});
if (__DEV__) {
ReactDOMInstrumentation.debugTool.onSetValueForProperty(node, name, value);
}
var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
DOMProperty.properties[name] : null;
if (propertyInfo) {
Expand Down Expand Up @@ -187,7 +191,9 @@ var DOMPropertyOperations = {
* @param {string} name
*/
deleteValueForProperty: function(node, name) {
ReactDOMDevtools.emitEvent('deleteValueForProperty', {node, name});
if (__DEV__) {
ReactDOMInstrumentation.debugTool.onDeleteValueForProperty(node, name);
}
var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
DOMProperty.properties[name] : null;
if (propertyInfo) {
Expand Down
66 changes: 66 additions & 0 deletions src/renderers/dom/shared/ReactDOMDebugTool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactDOMDebugTool
*/

'use strict';

var ReactDOMUnknownPropertyDevtool = require('ReactDOMUnknownPropertyDevtool');

var warning = require('warning');

var eventHandlers = [];
var handlerDoesThrowForEvent = {};

function emitEvent(handlerFunctionName, arg1, arg2, arg3, arg4, arg5) {
if (__DEV__) {
eventHandlers.forEach(function(handler) {
try {
if (handler[handlerFunctionName]) {
handler[handlerFunctionName](arg1, arg2, arg3, arg4, arg5);
}
} catch (e) {
warning(
!handlerDoesThrowForEvent[handlerFunctionName],
'exception thrown by devtool while handling %s: %s',
handlerFunctionName,
e.message
);
handlerDoesThrowForEvent[handlerFunctionName] = true;
}
});
}
}

var ReactDOMDebugTool = {
addDevtool(devtool) {
eventHandlers.push(devtool);
},
removeDevtool(devtool) {
for (var i = 0; i < eventHandlers.length; i++) {
if (eventHandlers[i] === devtool) {
eventHandlers.splice(i, 1);
i--;
}
}
},
onCreateMarkupForProperty(name, value) {
emitEvent('onCreateMarkupForProperty', name, value);
},
onSetValueForProperty(node, name, value) {
emitEvent('onSetValueForProperty', node, name, value);
},
onDeleteValueForProperty(node, name) {
emitEvent('onDeleteValueForProperty', node, name);
},
};

ReactDOMDebugTool.addDevtool(ReactDOMUnknownPropertyDevtool);

module.exports = ReactDOMDebugTool;
52 changes: 0 additions & 52 deletions src/renderers/dom/shared/ReactDOMDevtools.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/renderers/dom/shared/ReactDOMInstrumentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactDOMInstrumentation
*/

'use strict';

var ReactDOMDebugTool = require('ReactDOMDebugTool');

module.exports = {debugTool: ReactDOMDebugTool};
27 changes: 13 additions & 14 deletions src/renderers/dom/shared/devtools/ReactDOMUnknownPropertyDevtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ if (__DEV__) {
var warnedProperties = {};

var warnUnknownProperty = function(name) {
if (DOMProperty.properties.hasOwnProperty(name) || DOMProperty.isCustomAttribute(name)) {
return;
}
if (reactProps.hasOwnProperty(name) && reactProps[name] ||
warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
return;
Expand Down Expand Up @@ -69,20 +72,16 @@ if (__DEV__) {
};
}

class ReactDOMUnknownPropertyDevtool {
handleEvent(eventName, eventData) {
switch (eventName) {
case 'createMarkupForProperty':
case 'setValueForProperty':
case 'deleteValueForProperty':
var name = eventData.name;
if (!DOMProperty.properties.hasOwnProperty(name) &&
!DOMProperty.isCustomAttribute(name)) {
warnUnknownProperty(name);
}
break;
}
}
var ReactDOMUnknownPropertyDevtool = {
onCreateMarkupForProperty(name, value) {
warnUnknownProperty(name);
},
onSetValueForProperty(node, name, value) {
warnUnknownProperty(name);
},
onDeleteValueForProperty(node, name) {
warnUnknownProperty(name);
},
}

module.exports = ReactDOMUnknownPropertyDevtool;

0 comments on commit 26f3785

Please sign in to comment.