Skip to content

Commit

Permalink
refactor[renderer]: expose getInspectorDataForInstance in rendererConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxyq committed Jun 8, 2023
1 parent 9100456 commit 8b99a4f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/react-native-renderer/src/ReactFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ injectIntoDevTools({
version: ReactVersion,
rendererPackageName: 'react-native-renderer',
rendererConfig: {
getInspectorDataForInstance,
getInspectorDataForViewTag: getInspectorDataForViewTag,
getInspectorDataForViewAtPoint: getInspectorDataForViewAtPoint.bind(
null,
Expand Down
8 changes: 7 additions & 1 deletion packages/react-native-renderer/src/ReactFiberConfigFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
* @flow
*/

import type {TouchedViewDataAtPoint, ViewConfig} from './ReactNativeTypes';
import type {
InspectorData,
TouchedViewDataAtPoint,
ViewConfig,
} from './ReactNativeTypes';
import {create, diff} from './ReactNativeAttributePayload';
import {dispatchEvent} from './ReactFabricEventEmitter';
import {
DefaultEventPriority,
DiscreteEventPriority,
type EventPriority,
} from 'react-reconciler/src/ReactEventPriorities';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import {HostText} from 'react-reconciler/src/ReactWorkTags';

// Modules provided by RN:
Expand Down Expand Up @@ -94,6 +99,7 @@ export type NoTimeout = -1;
export type TransitionStatus = mixed;

export type RendererInspectionConfig = $ReadOnly<{
getInspectorDataForInstance?: (instance: Fiber | null) => InspectorData,
// Deprecated. Replaced with getInspectorDataForViewAtPoint.
getInspectorDataForViewTag?: (tag: number) => Object,
getInspectorDataForViewAtPoint?: (
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native-renderer/src/ReactFiberConfigNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {TouchedViewDataAtPoint} from './ReactNativeTypes';
import type {InspectorData, TouchedViewDataAtPoint} from './ReactNativeTypes';

// Modules provided by RN:
import {
Expand All @@ -28,6 +28,7 @@ import {
DefaultEventPriority,
type EventPriority,
} from 'react-reconciler/src/ReactEventPriorities';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';

const {get: getViewConfigForType} = ReactNativeViewConfigRegistry;

Expand All @@ -49,6 +50,7 @@ export type NoTimeout = -1;
export type TransitionStatus = mixed;

export type RendererInspectionConfig = $ReadOnly<{
getInspectorDataForInstance?: (instance: Fiber | null) => InspectorData,
// Deprecated. Replaced with getInspectorDataForViewAtPoint.
getInspectorDataForViewTag?: (tag: number) => Object,
getInspectorDataForViewAtPoint?: (
Expand Down
33 changes: 6 additions & 27 deletions packages/react-native-renderer/src/ReactNativeFiberInspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ function getInspectorDataForInstance(
selectedIndex,
source,
};
} else {
return (null: any);
}

throw new Error(
'getInspectorDataForInstance() is not available in production',
);
}

function getOwnerHierarchy(instance: any) {
Expand Down Expand Up @@ -153,34 +155,11 @@ function traverseOwnerTreeUp(
}
}

function getInspectorDataForViewTag(viewTag: number): Object {
function getInspectorDataForViewTag(viewTag: number): InspectorData {
if (__DEV__) {
const closestInstance = getClosestInstanceFromNode(viewTag);

// Handle case where user clicks outside of ReactNative
if (!closestInstance) {
return {
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null,
};
}

const fiber = findCurrentFiberUsingSlowPath(closestInstance);
const fiberHierarchy = getOwnerHierarchy(fiber);
const instance = lastNonHostInstance(fiberHierarchy);
const hierarchy = createHierarchy(fiberHierarchy);
const props = getHostProps(instance);
const source = instance._debugSource;
const selectedIndex = fiberHierarchy.indexOf(instance);

return {
hierarchy,
props,
selectedIndex,
source,
};
return getInspectorDataForInstance(closestInstance);
} else {
throw new Error(
'getInspectorDataForViewTag() is not available in production',
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-renderer/src/ReactNativeRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ injectIntoDevTools({
version: ReactVersion,
rendererPackageName: 'react-native-renderer',
rendererConfig: {
getInspectorDataForInstance,
getInspectorDataForViewTag: getInspectorDataForViewTag,
getInspectorDataForViewAtPoint: getInspectorDataForViewAtPoint.bind(
null,
Expand Down

0 comments on commit 8b99a4f

Please sign in to comment.