From d3e29d4e54f696da915f9aec08202b6314a162a1 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 27 Jan 2017 22:50:35 +0000 Subject: [PATCH] [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 --- src/renderers/dom/fiber/ReactDOMFiber.js | 9 ++- .../shared/fiber/ReactFiberCommitWork.js | 5 ++ .../shared/fiber/ReactFiberDevToolsHook.js | 74 +++++++++++++++++++ .../shared/fiber/ReactFiberScheduler.js | 4 + 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/renderers/shared/fiber/ReactFiberDevToolsHook.js diff --git a/src/renderers/dom/fiber/ReactDOMFiber.js b/src/renderers/dom/fiber/ReactDOMFiber.js index a541ff756a..7638fb82cb 100644 --- a/src/renderers/dom/fiber/ReactDOMFiber.js +++ b/src/renderers/dom/fiber/ReactDOMFiber.js @@ -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; diff --git a/src/renderers/shared/fiber/ReactFiberCommitWork.js b/src/renderers/shared/fiber/ReactFiberCommitWork.js index 0dd429cdda..04d24c44f5 100644 --- a/src/renderers/shared/fiber/ReactFiberCommitWork.js +++ b/src/renderers/shared/fiber/ReactFiberCommitWork.js @@ -25,6 +25,7 @@ var { CoroutineComponent, } = ReactTypeOfWork; var { commitCallbacks } = require('ReactFiberUpdateQueue'); +var { onCommitUnmount } = require('ReactFiberDevToolsHook'); var { Placement, @@ -317,6 +318,10 @@ module.exports = function( // 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); diff --git a/src/renderers/shared/fiber/ReactFiberDevToolsHook.js b/src/renderers/shared/fiber/ReactFiberDevToolsHook.js new file mode 100644 index 0000000000..6f85684fb6 --- /dev/null +++ b/src/renderers/shared/fiber/ReactFiberDevToolsHook.js @@ -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; diff --git a/src/renderers/shared/fiber/ReactFiberScheduler.js b/src/renderers/shared/fiber/ReactFiberScheduler.js index 7cdf1b917d..cb4ab209c1 100644 --- a/src/renderers/shared/fiber/ReactFiberScheduler.js +++ b/src/renderers/shared/fiber/ReactFiberScheduler.js @@ -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(config : HostConfig