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
This commit is contained in:
Kevin Gozali 2018-03-06 17:49:16 -08:00 committed by Facebook Github Bot
parent d52569c4a1
commit d7b5b2e9e4
1 changed files with 18 additions and 7 deletions

View File

@ -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<ReactRenderer> {
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