@samitouri / QOS-React-2 / commits / 26f3785a8c

Use duck typing instead of allocating event objects

jim committed Dec 16, 2015 at 19:13 UTC 26f3785a8c2e78ca8d393eb153f1b879d318b858
6 files changed +105 -74
src/renderers/dom/ReactDOM.js
-4
@@ -14,8 +14,6 @@
14 'use strict';
15
16 var ReactDOMComponentTree = require('ReactDOMComponentTree');
17 -var ReactDOMDevtools = require('ReactDOMDevtools');
18 -var ReactDOMUnknownPropertyDevtool = require('ReactDOMUnknownPropertyDevtool');
17 var ReactDefaultInjection = require('ReactDefaultInjection');
18 var ReactMount = require('ReactMount');
19 var ReactPerf = require('ReactPerf');
@@ -137,8 +135,6 @@ if (__DEV__) {
135 break;
136 }
137 }
140 -
141 - ReactDOMDevtools.addDevtool(new ReactDOMUnknownPropertyDevtool());
138 }
139 }
140
src/renderers/dom/shared/DOMPropertyOperations.js
+10 -4
@@ -12,7 +12,7 @@
12 'use strict';
13
14 var DOMProperty = require('DOMProperty');
15 -var ReactDOMDevtools = require('ReactDOMDevtools');
15 +var ReactDOMInstrumentation = require('ReactDOMInstrumentation');
16 var ReactPerf = require('ReactPerf');
17
18 var quoteAttributeValueForBrowser = require('quoteAttributeValueForBrowser');
@@ -87,7 +87,9 @@ var DOMPropertyOperations = {
87 * @return {?string} Markup string, or null if the property was invalid.
88 */
89 createMarkupForProperty: function(name, value) {
90 - ReactDOMDevtools.emitEvent('createMarkupForProperty', {name, value});
90 + if (__DEV__) {
91 + ReactDOMInstrumentation.debugTool.onCreateMarkupForProperty(name, value);
92 + }
93 var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
94 DOMProperty.properties[name] : null;
95 if (propertyInfo) {
@@ -131,7 +133,9 @@ var DOMPropertyOperations = {
133 * @param {*} value
134 */
135 setValueForProperty: function(node, name, value) {
134 - ReactDOMDevtools.emitEvent('setValueForProperty', {node, name, value});
136 + if (__DEV__) {
137 + ReactDOMInstrumentation.debugTool.onSetValueForProperty(node, name, value);
138 + }
139 var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
140 DOMProperty.properties[name] : null;
141 if (propertyInfo) {
@@ -187,7 +191,9 @@ var DOMPropertyOperations = {
191 * @param {string} name
192 */
193 deleteValueForProperty: function(node, name) {
190 - ReactDOMDevtools.emitEvent('deleteValueForProperty', {node, name});
194 + if (__DEV__) {
195 + ReactDOMInstrumentation.debugTool.onDeleteValueForProperty(node, name);
196 + }
197 var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
198 DOMProperty.properties[name] : null;
199 if (propertyInfo) {
src/renderers/dom/shared/ReactDOMDebugTool.js new
+66
@@ -0,0 +1,66 @@
1 +/**
2 + * Copyright 2013-2015, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule ReactDOMDebugTool
10 + */
11 +
12 +'use strict';
13 +
14 +var ReactDOMUnknownPropertyDevtool = require('ReactDOMUnknownPropertyDevtool');
15 +
16 +var warning = require('warning');
17 +
18 +var eventHandlers = [];
19 +var handlerDoesThrowForEvent = {};
20 +
21 +function emitEvent(handlerFunctionName, arg1, arg2, arg3, arg4, arg5) {
22 + if (__DEV__) {
23 + eventHandlers.forEach(function(handler) {
24 + try {
25 + if (handler[handlerFunctionName]) {
26 + handler[handlerFunctionName](arg1, arg2, arg3, arg4, arg5);
27 + }
28 + } catch (e) {
29 + warning(
30 + !handlerDoesThrowForEvent[handlerFunctionName],
31 + 'exception thrown by devtool while handling %s: %s',
32 + handlerFunctionName,
33 + e.message
34 + );
35 + handlerDoesThrowForEvent[handlerFunctionName] = true;
36 + }
37 + });
38 + }
39 +}
40 +
41 +var ReactDOMDebugTool = {
42 + addDevtool(devtool) {
43 + eventHandlers.push(devtool);
44 + },
45 + removeDevtool(devtool) {
46 + for (var i = 0; i < eventHandlers.length; i++) {
47 + if (eventHandlers[i] === devtool) {
48 + eventHandlers.splice(i, 1);
49 + i--;
50 + }
51 + }
52 + },
53 + onCreateMarkupForProperty(name, value) {
54 + emitEvent('onCreateMarkupForProperty', name, value);
55 + },
56 + onSetValueForProperty(node, name, value) {
57 + emitEvent('onSetValueForProperty', node, name, value);
58 + },
59 + onDeleteValueForProperty(node, name) {
60 + emitEvent('onDeleteValueForProperty', node, name);
61 + },
62 +};
63 +
64 +ReactDOMDebugTool.addDevtool(ReactDOMUnknownPropertyDevtool);
65 +
66 +module.exports = ReactDOMDebugTool;
src/renderers/dom/shared/ReactDOMDevtools.js deleted
-52
@@ -1,52 +0,0 @@
1 -/**
2 - * Copyright 2013-2015, Facebook, Inc.
3 - * All rights reserved.
4 - *
5 - * This source code is licensed under the BSD-style license found in the
6 - * LICENSE file in the root directory of this source tree. An additional grant
7 - * of patent rights can be found in the PATENTS file in the same directory.
8 - *
9 - * @providesModule ReactDOMDevtools
10 - */
11 -
12 -'use strict';
13 -
14 -var warning = require('warning');
15 -
16 -var eventHandlers = [];
17 -var handlerDoesThrowForEvent = {};
18 -
19 -var ReactDOMDevtools = {
20 - addDevtool(devtool) {
21 - eventHandlers.push(devtool);
22 - },
23 -
24 - removeDevtool(devtool) {
25 - for (var i = 0; i < eventHandlers.length; i++) {
26 - if (eventHandlers[i] === devtool) {
27 - eventHandlers.splice(i, 1);
28 - i--;
29 - }
30 - }
31 - },
32 -
33 - emitEvent(eventName, eventData) {
34 - if (__DEV__) {
35 - eventHandlers.forEach(function(handler) {
36 - try {
37 - handler.handleEvent(eventName, eventData);
38 - } catch (e) {
39 - warning(
40 - !handlerDoesThrowForEvent[eventName],
41 - 'exception thrown by devtool while handling %s: %s',
42 - eventName,
43 - e.message
44 - );
45 - handlerDoesThrowForEvent[eventName] = true;
46 - }
47 - });
48 - }
49 - },
50 -};
51 -
52 -module.exports = ReactDOMDevtools;
src/renderers/dom/shared/ReactDOMInstrumentation.js new
+16
@@ -0,0 +1,16 @@
1 +/**
2 + * Copyright 2013-2015, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule ReactDOMInstrumentation
10 + */
11 +
12 +'use strict';
13 +
14 +var ReactDOMDebugTool = require('ReactDOMDebugTool');
15 +
16 +module.exports = {debugTool: ReactDOMDebugTool};
src/renderers/dom/shared/devtools/ReactDOMUnknownPropertyDevtool.js
+13 -14
@@ -26,6 +26,9 @@ if (__DEV__) {
26 var warnedProperties = {};
27
28 var warnUnknownProperty = function(name) {
29 + if (DOMProperty.properties.hasOwnProperty(name) || DOMProperty.isCustomAttribute(name)) {
30 + return;
31 + }
32 if (reactProps.hasOwnProperty(name) && reactProps[name] ||
33 warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
34 return;
@@ -69,20 +72,16 @@ if (__DEV__) {
72 };
73 }
74
72 -class ReactDOMUnknownPropertyDevtool {
73 - handleEvent(eventName, eventData) {
74 - switch (eventName) {
75 - case 'createMarkupForProperty':
76 - case 'setValueForProperty':
77 - case 'deleteValueForProperty':
78 - var name = eventData.name;
79 - if (!DOMProperty.properties.hasOwnProperty(name) &&
80 - !DOMProperty.isCustomAttribute(name)) {
81 - warnUnknownProperty(name);
82 - }
83 - break;
84 - }
85 - }
75 +var ReactDOMUnknownPropertyDevtool = {
76 + onCreateMarkupForProperty(name, value) {
77 + warnUnknownProperty(name);
78 + },
79 + onSetValueForProperty(node, name, value) {
80 + warnUnknownProperty(name);
81 + },
82 + onDeleteValueForProperty(node, name) {
83 + warnUnknownProperty(name);
84 + },
85 }
86
87 module.exports = ReactDOMUnknownPropertyDevtool;