Skip to content

Commit

Permalink
Merge pull request #6068 from gaearon/react-instrumentation
Browse files Browse the repository at this point in the history
Add ReactInstrumentation
  • Loading branch information
gaearon committed Feb 26, 2016
2 parents 74070e5 + 3863330 commit 92530b4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/isomorphic/ReactDebugTool.js
@@ -0,0 +1,65 @@
/**
* Copyright 2016-present, 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 ReactDebugTool
*/

'use strict';

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 ReactDebugTool = {
addDevtool(devtool) {
eventHandlers.push(devtool);
},
removeDevtool(devtool) {
for (var i = 0; i < eventHandlers.length; i++) {
if (eventHandlers[i] === devtool) {
eventHandlers.splice(i, 1);
i--;
}
}
},
onMountRootComponent(internalInstance) {
emitEvent('onMountRootComponent', internalInstance);
},
onMountComponent(internalInstance) {
emitEvent('onMountComponent', internalInstance);
},
onUpdateComponent(internalInstance) {
emitEvent('onUpdateComponent', internalInstance);
},
onUnmountComponent(internalInstance) {
emitEvent('onUnmountComponent', internalInstance);
},
};

module.exports = ReactDebugTool;
16 changes: 16 additions & 0 deletions src/isomorphic/ReactInstrumentation.js
@@ -0,0 +1,16 @@
/**
* Copyright 2016-present, 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 ReactInstrumentation
*/

'use strict';

var ReactDebugTool = require('ReactDebugTool');

module.exports = {debugTool: ReactDebugTool};
5 changes: 5 additions & 0 deletions src/renderers/dom/client/ReactMount.js
Expand Up @@ -20,6 +20,7 @@ var ReactDOMContainerInfo = require('ReactDOMContainerInfo');
var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
var ReactElement = require('ReactElement');
var ReactFeatureFlags = require('ReactFeatureFlags');
var ReactInstrumentation = require('ReactInstrumentation');
var ReactMarkupChecksum = require('ReactMarkupChecksum');
var ReactPerf = require('ReactPerf');
var ReactReconciler = require('ReactReconciler');
Expand Down Expand Up @@ -347,6 +348,10 @@ var ReactMount = {
var wrapperID = componentInstance._instance.rootID;
instancesByReactRootID[wrapperID] = componentInstance;

if (__DEV__) {
ReactInstrumentation.debugTool.onMountRootComponent(componentInstance);
}

return componentInstance;
},

Expand Down
16 changes: 15 additions & 1 deletion src/renderers/shared/reconciler/ReactReconciler.js
Expand Up @@ -12,6 +12,7 @@
'use strict';

var ReactRef = require('ReactRef');
var ReactInstrumentation = require('ReactInstrumentation');

/**
* Helper to call ReactRef.attachRefs with this composite component, split out
Expand Down Expand Up @@ -51,6 +52,9 @@ var ReactReconciler = {
internalInstance._currentElement.ref != null) {
transaction.getReactMountReady().enqueue(attachRefs, internalInstance);
}
if (__DEV__) {
ReactInstrumentation.debugTool.onMountComponent(internalInstance);
}
return markup;
},

Expand All @@ -70,7 +74,10 @@ var ReactReconciler = {
*/
unmountComponent: function(internalInstance, safely) {
ReactRef.detachRefs(internalInstance, internalInstance._currentElement);
return internalInstance.unmountComponent(safely);
internalInstance.unmountComponent(safely);
if (__DEV__) {
ReactInstrumentation.debugTool.onUnmountComponent(internalInstance);
}
},

/**
Expand Down Expand Up @@ -119,6 +126,10 @@ var ReactReconciler = {
internalInstance._currentElement.ref != null) {
transaction.getReactMountReady().enqueue(attachRefs, internalInstance);
}

if (__DEV__) {
ReactInstrumentation.debugTool.onUpdateComponent(internalInstance);
}
},

/**
Expand All @@ -133,6 +144,9 @@ var ReactReconciler = {
transaction
) {
internalInstance.performUpdateIfNecessary(transaction);
if (__DEV__) {
ReactInstrumentation.debugTool.onUpdateComponent(internalInstance);
}
},

};
Expand Down

0 comments on commit 92530b4

Please sign in to comment.