Skip to content

Commit 0519a58

Browse files
committed
refactor: fork isChildPublicInstance implementation per renderer
1 parent 49439b4 commit 0519a58

File tree

3 files changed

+66
-60
lines changed

3 files changed

+66
-60
lines changed

packages/react-native-renderer/src/ReactFabric.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ import {
3333
getInspectorDataForInstance,
3434
} from './ReactNativeFiberInspector';
3535
import {LegacyRoot, ConcurrentRoot} from 'react-reconciler/src/ReactRootTags';
36+
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
3637
import {
3738
findHostInstance_DEPRECATED,
3839
findNodeHandle,
3940
dispatchCommand,
4041
sendAccessibilityEvent,
4142
getNodeFromInternalInstanceHandle,
42-
isChildPublicInstance,
4343
} from './ReactNativePublicCompat';
4444
import {getPublicInstanceFromInternalInstanceHandle} from './ReactFiberConfigFabric';
45+
import {getInternalInstanceHandleFromPublicInstance} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
4546

4647
// $FlowFixMe[missing-local-annot]
4748
function onRecoverableError(error) {
@@ -101,6 +102,36 @@ function createPortal(
101102
return createPortalImpl(children, containerTag, null, key);
102103
}
103104

105+
type FabricPublicInstance = mixed; // Should have been PublicInstance from ReactFiberConfigFabric
106+
type PaperPublicInstance = mixed; // Should be unknown for Fabric
107+
function isChildPublicInstance(
108+
parentInstance: FabricPublicInstance | PaperPublicInstance,
109+
childInstance: FabricPublicInstance | PaperPublicInstance,
110+
): boolean {
111+
if (__DEV__) {
112+
const parentInternalInstanceHandle =
113+
// $FlowExpectedError[incompatible-call] Type for parentInstance should have been PublicInstance from ReactFiberConfigFabric.
114+
getInternalInstanceHandleFromPublicInstance(parentInstance);
115+
const childInternalInstanceHandle =
116+
// $FlowExpectedError[incompatible-call] Type for childInstance should have been PublicInstance from ReactFiberConfigFabric.
117+
getInternalInstanceHandleFromPublicInstance(childInstance);
118+
119+
if (
120+
parentInternalInstanceHandle != null &&
121+
childInternalInstanceHandle != null
122+
) {
123+
return doesFiberContain(
124+
parentInternalInstanceHandle,
125+
childInternalInstanceHandle,
126+
);
127+
}
128+
129+
return false;
130+
} else {
131+
throw new Error('isChildPublicInstance() is not available in production.');
132+
}
133+
}
134+
104135
setBatchingImplementation(batchedUpdatesImpl, discreteUpdates);
105136

106137
const roots = new Map<number, FiberRoot>();

packages/react-native-renderer/src/ReactNativePublicCompat.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@ import {
1616
legacySendAccessibilityEvent,
1717
getNodeFromPublicInstance,
1818
getNativeTagFromPublicInstance,
19-
getInternalInstanceHandleFromPublicInstance,
2019
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
2120

2221
import {
2322
findHostInstance,
2423
findHostInstanceWithWarning,
2524
} from 'react-reconciler/src/ReactFiberReconciler';
26-
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
2725
import ReactSharedInternals from 'shared/ReactSharedInternals';
2826
import getComponentNameFromType from 'shared/getComponentNameFromType';
2927

30-
import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent';
31-
3228
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
3329

3430
export function findHostInstance_DEPRECATED<TElementType: ElementType>(
@@ -222,57 +218,3 @@ export function getNodeFromInternalInstanceHandle(
222218
internalInstanceHandle.stateNode.node
223219
);
224220
}
225-
226-
// Should have been PublicInstance from ReactFiberConfigFabric
227-
type FabricPublicInstance = mixed;
228-
// Should have been PublicInstance from ReactFiberConfigNative
229-
type PaperPublicInstance = HostComponent<mixed>;
230-
231-
// Remove this once Paper is no longer supported and DOM Node API are enabled by default in RN.
232-
export function isChildPublicInstance(
233-
parentInstance: FabricPublicInstance | PaperPublicInstance,
234-
childInstance: FabricPublicInstance | PaperPublicInstance,
235-
): boolean {
236-
if (__DEV__) {
237-
// Paper
238-
if (
239-
parentInstance instanceof ReactNativeFiberHostComponent ||
240-
childInstance instanceof ReactNativeFiberHostComponent
241-
) {
242-
if (
243-
parentInstance instanceof ReactNativeFiberHostComponent &&
244-
childInstance instanceof ReactNativeFiberHostComponent
245-
) {
246-
return doesFiberContain(
247-
parentInstance._internalFiberInstanceHandleDEV,
248-
childInstance._internalFiberInstanceHandleDEV,
249-
);
250-
}
251-
252-
// Means that one instance is from Fabric and other is from Paper.
253-
return false;
254-
}
255-
256-
const parentInternalInstanceHandle =
257-
// $FlowExpectedError[incompatible-call] Type for parentInstance should have been PublicInstance from ReactFiberConfigFabric.
258-
getInternalInstanceHandleFromPublicInstance(parentInstance);
259-
const childInternalInstanceHandle =
260-
// $FlowExpectedError[incompatible-call] Type for childInstance should have been PublicInstance from ReactFiberConfigFabric.
261-
getInternalInstanceHandleFromPublicInstance(childInstance);
262-
263-
// Fabric
264-
if (
265-
parentInternalInstanceHandle != null &&
266-
childInternalInstanceHandle != null
267-
) {
268-
return doesFiberContain(
269-
parentInternalInstanceHandle,
270-
childInternalInstanceHandle,
271-
);
272-
}
273-
274-
return false;
275-
} else {
276-
throw new Error('isChildPublicInstance() is not available in production.');
277-
}
278-
}

packages/react-native-renderer/src/ReactNativeRenderer.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {ReactPortal, ReactNodeList} from 'shared/ReactTypes';
1111
import type {ElementRef, Element, ElementType} from 'react';
1212
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
13+
import type {HostComponent} from './ReactNativeTypes';
1314

1415
import './ReactNativeInjection';
1516

@@ -39,13 +40,14 @@ import {
3940
getInspectorDataForInstance,
4041
} from './ReactNativeFiberInspector';
4142
import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
43+
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
4244
import {
4345
findHostInstance_DEPRECATED,
4446
findNodeHandle,
4547
dispatchCommand,
4648
sendAccessibilityEvent,
47-
isChildPublicInstance,
4849
} from './ReactNativePublicCompat';
50+
import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent';
4951

5052
// $FlowFixMe[missing-local-annot]
5153
function onRecoverableError(error) {
@@ -106,6 +108,37 @@ function createPortal(
106108
return createPortalImpl(children, containerTag, null, key);
107109
}
108110

111+
type PaperPublicInstance = HostComponent<mixed>; // Should have been PublicInstance from ReactFiberConfigNative
112+
type FabricPublicInstance = mixed; // Should be unknown for Paper
113+
function isChildPublicInstance(
114+
parentInstance: PaperPublicInstance | FabricPublicInstance,
115+
childInstance: PaperPublicInstance | FabricPublicInstance,
116+
): boolean {
117+
if (__DEV__) {
118+
if (
119+
parentInstance instanceof ReactNativeFiberHostComponent ||
120+
childInstance instanceof ReactNativeFiberHostComponent
121+
) {
122+
if (
123+
parentInstance instanceof ReactNativeFiberHostComponent &&
124+
childInstance instanceof ReactNativeFiberHostComponent
125+
) {
126+
return doesFiberContain(
127+
parentInstance._internalFiberInstanceHandleDEV,
128+
childInstance._internalFiberInstanceHandleDEV,
129+
);
130+
}
131+
132+
// Means that one instance is from Fabric and other is from Paper.
133+
return false;
134+
}
135+
136+
return false;
137+
} else {
138+
throw new Error('isChildPublicInstance() is not available in production.');
139+
}
140+
}
141+
109142
setBatchingImplementation(batchedUpdatesImpl, discreteUpdates);
110143

111144
function computeComponentStackForErrorReporting(reactTag: number): string {

0 commit comments

Comments
 (0)