[Fiber] Support React DevTools (#8818)

* Initial React DevTools support for Fiber

* Record a newly failing irrelevant test

This is fine because we don't use it in Stack anyway (at least not that part), and we also rely on Set/Map in other parts of code.
The newly failing test is the one saying "it works without Map/Set".

* Explicitly check for Fiber DevTools support

This lets us ignore pre-Fiber DevTools instead of crashing them by injecting a different API.

* Track the roots in DevTools instead

* Lol Dan this is not how warnings work in our codebase

* Move injection to the renderer

* Notify DevTools about unmounts

* Make it work in production

* Fix lint and flow

* Fixups

* Address feedback
This commit is contained in:
Dan Abramov authored and GitHub committed 2017-01-27 22:50:35 +00:00
1 parent 3c75593692
commit d3e29d4e54
4 files changed
+91 -1

No files matched your search

+8 -1
View File
@@ -29,7 +29,7 @@ var ReactInputSelection = require('ReactInputSelection');
var ReactInstanceMap = require('ReactInstanceMap');
var ReactPortal = require('ReactPortal');
var { isValidElement } = require('React');
var { injectInternals } = require('ReactFiberDevToolsHook');
var findDOMNode = require('findDOMNode');
var invariant = require('invariant');
@@ -407,4 +407,11 @@ var ReactDOM = {
};
if (typeof injectInternals === 'function') {
injectInternals({
findFiberByHostInstance: ReactDOMComponentTree.getClosestInstanceFromNode,
findHostInstanceByFiber: DOMRenderer.findHostInstance,
});
}
module.exports = ReactDOM;
@@ -25,6 +25,7 @@ var {
CoroutineComponent,
} = ReactTypeOfWork;
var { commitCallbacks } = require('ReactFiberUpdateQueue');
var { onCommitUnmount } = require('ReactFiberDevToolsHook');
var {
Placement,
@@ -317,6 +318,10 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
// deletion, so don't let them throw. Host-originating errors should
// interrupt deletion, so it's okay
function commitUnmount(current : Fiber) : void {
if (typeof onCommitUnmount === 'function') {
onCommitUnmount(current);
}
switch (current.tag) {
case ClassComponent: {
safelyDetachRef(current);
@@ -0,0 +1,74 @@
/**
* Copyright 2013-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 ReactFiberDevToolsHook
* @flow
*/
/* globals __REACT_DEVTOOLS_GLOBAL_HOOK__ */
'use strict';
import type { Fiber } from 'ReactFiber';
import type { FiberRoot } from 'ReactFiberRoot';
if (__DEV__) {
var warning = require('warning');
}
let rendererID = null;
let injectInternals = null;
let onCommitRoot = null;
let onCommitUnmount = null;
if (
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.supportsFiber
) {
let {
inject,
onCommitFiberRoot,
onCommitFiberUnmount,
} = __REACT_DEVTOOLS_GLOBAL_HOOK__;
injectInternals = function(internals : Object) {
warning(rendererID == null, 'Cannot inject into DevTools twice.');
rendererID = inject(internals);
};
onCommitRoot = function(root : FiberRoot) {
if (rendererID == null) {
return;
}
try {
onCommitFiberRoot(rendererID, root);
} catch (err) {
// Catch all errors because it is unsafe to throw in the commit phase.
if (__DEV__) {
warning(false, 'React DevTools encountered an error: %s', err);
}
}
};
onCommitUnmount = function(fiber : Fiber) {
if (rendererID == null) {
return;
}
try {
onCommitFiberUnmount(rendererID, fiber);
} catch (err) {
// Catch all errors because it is unsafe to throw in the commit phase.
if (__DEV__) {
warning(false, 'React DevTools encountered an error: %s', err);
}
}
};
}
exports.injectInternals = injectInternals;
exports.onCommitRoot = onCommitRoot;
exports.onCommitUnmount = onCommitUnmount;
@@ -44,6 +44,7 @@ var ReactFeatureFlags = require('ReactFeatureFlags');
var getComponentName = require('getComponentName');
var { cloneFiber } = require('ReactFiber');
var { onCommitRoot } = require('ReactFiberDevToolsHook');
var {
NoWork,
@@ -411,6 +412,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
}
isCommitting = false;
if (typeof onCommitRoot === 'function') {
onCommitRoot(finishedWork.stateNode);
}
if (__DEV__ && ReactFiberInstrumentation.debugTool) {
ReactFiberInstrumentation.debugTool.onCommitWork(finishedWork);
}