From d7b5b2e9e4545bd6da2f5dd5002d884bcc0556ee Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Tue, 6 Mar 2018 17:49:16 -0800 Subject: [PATCH] Allow Inspector to handle 2 renderers Summary: To avoid crashing when there are 2 different renderers, let the Inspector be a bit lenient. Reviewed By: yungsters Differential Revision: D7175338 fbshipit-source-id: ee5f86252f090361e42b6e2a93ae56c4c83c8c53 --- Libraries/Inspector/Inspector.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Libraries/Inspector/Inspector.js b/Libraries/Inspector/Inspector.js index 90e30b346..963b616fc 100644 --- a/Libraries/Inspector/Inspector.js +++ b/Libraries/Inspector/Inspector.js @@ -32,15 +32,26 @@ export type ReactRenderer = { }; const hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__; -const renderer: ReactRenderer = findRenderer(); +const renderers = findRenderers(); + // required for devtools to be able to edit react native styles hook.resolveRNStyle = require('flattenStyle'); -function findRenderer(): ReactRenderer { - const renderers = hook._renderers; - const keys = Object.keys(renderers); - invariant(keys.length === 1, 'Expected to find exactly one React Native renderer on DevTools hook.'); - return renderers[keys[0]]; +function findRenderers(): $ReadOnlyArray { + const allRenderers = Object.keys(hook._renderers).map(key => hook._renderers[key]); + invariant(allRenderers.length >= 1, 'Expected to find at least one React Native renderer on DevTools hook.'); + return allRenderers; +} + +function getInspectorDataForViewTag(touchedViewTag: number) { + for (let i = 0; i < renderers.length; i++) { + const renderer = renderers[i]; + const inspectorData = renderer.getInspectorDataForViewTag(touchedViewTag); + if (inspectorData.hierarchy.length > 0) { + return inspectorData; + } + } + throw new Error('Expected to find at least one React renderer.'); } class Inspector extends React.Component<{ @@ -170,7 +181,7 @@ class Inspector extends React.Component<{ props, selection, source, - } = renderer.getInspectorDataForViewTag(touchedViewTag); + } = getInspectorDataForViewTag(touchedViewTag); if (this.state.devtoolsAgent) { // Skip host leafs