Skip to content

Commit

Permalink
refactor[renderer]: expose getInspectorDataForInstance in rendererCon…
Browse files Browse the repository at this point in the history
…fig (#26913)

## Summary
This is required for the case when we have an instance and want to get
inspector data for it. Such case occurs when RN's application being
debugged via React DevTools.

React DevTools sends instance to RN, which then gets all auxiliary data
to highlight some elements. Having `getInspectorDataForInstance` method
exposed makes it possible to easily get current props from fiber, which
then can be used to display some margins & paddings for hovered element
(via props.style).

I see that `getInspectorDataForInstance` is being exported at the top
level of the renderer, but feels like this should also be inside
DevTools global hook, the same way we use it for
[`getInspectorDataForViewAtPoint`](https://github.com/facebook/react-native/blob/e7d3662904e0e35e43380ee6d54859388713a592/packages/react-native/Libraries/Inspector/getInspectorDataForViewAtPoint.js).

DiffTrain build for commit 21a161f.
  • Loading branch information
hoxyq committed Jun 9, 2023
1 parent 8ed315a commit 4000796
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23922,7 +23922,7 @@ function createFiberRoot(
return root;
}

var ReactVersion = "18.3.0-canary-910045696-20230607";
var ReactVersion = "18.3.0-canary-21a161fa3-20230609";

// Might add PROFILE later.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8617,7 +8617,7 @@ var devToolsConfig$jscomp$inline_1031 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "18.3.0-canary-910045696-20230607",
version: "18.3.0-canary-21a161fa3-20230609",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1230 = {
Expand Down Expand Up @@ -8648,7 +8648,7 @@ var internals$jscomp$inline_1230 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-910045696-20230607"
reconcilerVersion: "18.3.0-canary-21a161fa3-20230609"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1231 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9043,7 +9043,7 @@ var devToolsConfig$jscomp$inline_1073 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "18.3.0-canary-910045696-20230607",
version: "18.3.0-canary-21a161fa3-20230609",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1271 = {
Expand Down Expand Up @@ -9074,7 +9074,7 @@ var internals$jscomp$inline_1271 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-910045696-20230607"
reconcilerVersion: "18.3.0-canary-21a161fa3-20230609"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1272 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-canary-910045696-20230607";
var ReactVersion = "18.3.0-canary-21a161fa3-20230609";

// ATTENTION
// When adding new symbols to this file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,4 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-canary-910045696-20230607";
exports.version = "18.3.0-canary-21a161fa3-20230609";
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-canary-910045696-20230607";
exports.version = "18.3.0-canary-21a161fa3-20230609";

/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
910045696bb5f693acb77890e6750c5e4659b420
21a161fa37dce969c58ae17f67f2856d06514892
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<c225a4c67ebf882d1b25c0e3e921041e>>
* @generated SignedSource<<c969a4144be0d81d2e4ff40ed03beb08>>
*/

'use strict';
Expand Down Expand Up @@ -27238,7 +27238,7 @@ function createFiberRoot(
return root;
}

var ReactVersion = "18.3.0-canary-86fc96c3";
var ReactVersion = "18.3.0-canary-cfb11465";

function createPortal$1(
children,
Expand Down Expand Up @@ -28086,30 +28086,8 @@ function traverseOwnerTreeUp(hierarchy, instance) {

function getInspectorDataForViewTag(viewTag) {
{
var closestInstance = getInstanceFromTag(viewTag); // Handle case where user clicks outside of ReactNative

if (!closestInstance) {
return {
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null
};
}

var fiber = findCurrentFiberUsingSlowPath(closestInstance);
var fiberHierarchy = getOwnerHierarchy(fiber);
var instance = lastNonHostInstance(fiberHierarchy);
var hierarchy = createHierarchy(fiberHierarchy);
var props = getHostProps(instance);
var source = instance._debugSource;
var selectedIndex = fiberHierarchy.indexOf(instance);
return {
hierarchy: hierarchy,
props: props,
selectedIndex: selectedIndex,
source: source
};
var closestInstance = getInstanceFromTag(viewTag);
return getInspectorDataForInstance(closestInstance);
}
}

Expand Down Expand Up @@ -28271,6 +28249,7 @@ injectIntoDevTools({
version: ReactVersion,
rendererPackageName: "react-native-renderer",
rendererConfig: {
getInspectorDataForInstance: getInspectorDataForInstance,
getInspectorDataForViewTag: getInspectorDataForViewTag,
getInspectorDataForViewAtPoint: getInspectorDataForViewAtPoint.bind(
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<2b42d6e98a00482728d7cea5fa740ff6>>
* @generated SignedSource<<28a0007bb338e500f65f038a127e0412>>
*/

"use strict";
Expand Down Expand Up @@ -9469,6 +9469,39 @@ function getHostProps(fiber) {
? fiber.memoizedProps || emptyObject
: emptyObject;
}
function getInspectorDataForInstance(closestInstance) {
if (!closestInstance)
return {
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null
};
var fiber = findCurrentFiberUsingSlowPath(closestInstance);
closestInstance = [];
traverseOwnerTreeUp(closestInstance, fiber);
a: {
for (fiber = closestInstance.length - 1; 1 < fiber; fiber--) {
var instance = closestInstance[fiber];
if (5 !== instance.tag) {
fiber = instance;
break a;
}
}
fiber = closestInstance[0];
}
instance = createHierarchy(closestInstance);
var props = getHostProps(fiber),
source = fiber._debugSource;
closestInstance = closestInstance.indexOf(fiber);
return {
closestInstance: fiber,
hierarchy: instance,
props: props,
selectedIndex: closestInstance,
source: source
};
}
function traverseOwnerTreeUp(hierarchy, instance) {
instance &&
(hierarchy.unshift(instance),
Expand All @@ -9493,9 +9526,10 @@ var roots = new Map(),
devToolsConfig$jscomp$inline_1056 = {
findFiberByHostInstance: getInstanceFromNode,
bundleType: 0,
version: "18.3.0-canary-0c80a9e1",
version: "18.3.0-canary-abeac87f",
rendererPackageName: "react-native-renderer",
rendererConfig: {
getInspectorDataForInstance: getInspectorDataForInstance,
getInspectorDataForViewTag: function () {
throw Error(
"getInspectorDataForViewTag() is not available in production"
Expand Down Expand Up @@ -9535,7 +9569,7 @@ var internals$jscomp$inline_1294 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-0c80a9e1"
reconcilerVersion: "18.3.0-canary-abeac87f"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1295 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down Expand Up @@ -9583,39 +9617,7 @@ exports.findHostInstance_DEPRECATED = function (componentOrHandle) {
: findHostInstance(componentOrHandle);
};
exports.findNodeHandle = findNodeHandle;
exports.getInspectorDataForInstance = function (closestInstance) {
if (!closestInstance)
return {
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null
};
var fiber = findCurrentFiberUsingSlowPath(closestInstance);
closestInstance = [];
traverseOwnerTreeUp(closestInstance, fiber);
a: {
for (fiber = closestInstance.length - 1; 1 < fiber; fiber--) {
var instance = closestInstance[fiber];
if (5 !== instance.tag) {
fiber = instance;
break a;
}
}
fiber = closestInstance[0];
}
instance = createHierarchy(closestInstance);
var props = getHostProps(fiber),
source = fiber._debugSource;
closestInstance = closestInstance.indexOf(fiber);
return {
closestInstance: fiber,
hierarchy: instance,
props: props,
selectedIndex: closestInstance,
source: source
};
};
exports.getInspectorDataForInstance = getInspectorDataForInstance;
exports.getNodeFromInternalInstanceHandle = function (internalInstanceHandle) {
return (
internalInstanceHandle &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<fdb735ba84f432fe69ae562a741d9d9d>>
* @generated SignedSource<<fa95eb422bbf02624017fdab1fc74ea8>>
*/


Expand Down Expand Up @@ -10178,6 +10178,39 @@ function getHostProps(fiber) {
? fiber.memoizedProps || emptyObject
: emptyObject;
}
function getInspectorDataForInstance(closestInstance) {
if (!closestInstance)
return {
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null
};
var fiber = findCurrentFiberUsingSlowPath(closestInstance);
closestInstance = [];
traverseOwnerTreeUp(closestInstance, fiber);
a: {
for (fiber = closestInstance.length - 1; 1 < fiber; fiber--) {
var instance = closestInstance[fiber];
if (5 !== instance.tag) {
fiber = instance;
break a;
}
}
fiber = closestInstance[0];
}
instance = createHierarchy(closestInstance);
var props = getHostProps(fiber),
source = fiber._debugSource;
closestInstance = closestInstance.indexOf(fiber);
return {
closestInstance: fiber,
hierarchy: instance,
props: props,
selectedIndex: closestInstance,
source: source
};
}
function traverseOwnerTreeUp(hierarchy, instance) {
instance &&
(hierarchy.unshift(instance),
Expand All @@ -10202,9 +10235,10 @@ var roots = new Map(),
devToolsConfig$jscomp$inline_1134 = {
findFiberByHostInstance: getInstanceFromNode,
bundleType: 0,
version: "18.3.0-canary-d6461ff7",
version: "18.3.0-canary-946c3ff0",
rendererPackageName: "react-native-renderer",
rendererConfig: {
getInspectorDataForInstance: getInspectorDataForInstance,
getInspectorDataForViewTag: function () {
throw Error(
"getInspectorDataForViewTag() is not available in production"
Expand Down Expand Up @@ -10257,7 +10291,7 @@ var roots = new Map(),
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-d6461ff7"
reconcilerVersion: "18.3.0-canary-946c3ff0"
});
exports.createPortal = function (children, containerTag) {
return createPortal$1(
Expand Down Expand Up @@ -10292,39 +10326,7 @@ exports.findHostInstance_DEPRECATED = function (componentOrHandle) {
: findHostInstance(componentOrHandle);
};
exports.findNodeHandle = findNodeHandle;
exports.getInspectorDataForInstance = function (closestInstance) {
if (!closestInstance)
return {
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null
};
var fiber = findCurrentFiberUsingSlowPath(closestInstance);
closestInstance = [];
traverseOwnerTreeUp(closestInstance, fiber);
a: {
for (fiber = closestInstance.length - 1; 1 < fiber; fiber--) {
var instance = closestInstance[fiber];
if (5 !== instance.tag) {
fiber = instance;
break a;
}
}
fiber = closestInstance[0];
}
instance = createHierarchy(closestInstance);
var props = getHostProps(fiber),
source = fiber._debugSource;
closestInstance = closestInstance.indexOf(fiber);
return {
closestInstance: fiber,
hierarchy: instance,
props: props,
selectedIndex: closestInstance,
source: source
};
};
exports.getInspectorDataForInstance = getInspectorDataForInstance;
exports.getNodeFromInternalInstanceHandle = function (internalInstanceHandle) {
return (
internalInstanceHandle &&
Expand Down
Loading

0 comments on commit 4000796

Please sign in to comment.