Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted definition and access to public instances to a separate module in Fabric #26291

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions packages/react-native-renderer/src/NativeMethodsMixinUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,6 @@ export function mountSafeCallback_NOT_REALLY_SAFE(
};
}

export function throwOnStylesProp(component: any, props: any) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused :)

if (props.styles !== undefined) {
const owner = component._owner || null;
const name = component.constructor.displayName;
let msg =
'`styles` is not a supported property of `' +
name +
'`, did ' +
'you mean `style` (singular)?';
if (owner && owner.constructor && owner.constructor.displayName) {
msg +=
'\n\nCheck the `' +
owner.constructor.displayName +
'` parent ' +
' component.';
}
throw new Error(msg);
}
}

export function warnForStyleProps(props: any, validAttributes: any) {
if (__DEV__) {
for (const key in validAttributes.style) {
Expand Down
45 changes: 34 additions & 11 deletions packages/react-native-renderer/src/ReactFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal';
import {setBatchingImplementation} from './legacy-events/ReactGenericBatching';
import ReactVersion from 'shared/ReactVersion';
import {getNativeTagFromPublicInstance} from './ReactFabricPublicInstanceUtils';

// Modules provided by RN:
import {
Expand All @@ -44,6 +45,7 @@ import {
import {LegacyRoot, ConcurrentRoot} from 'react-reconciler/src/ReactRootTags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import type {PublicInstance} from './ReactFabricHostConfig';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

Expand All @@ -67,19 +69,23 @@ function findHostInstance_DEPRECATED<TElementType: ElementType>(
owner.stateNode._warnedAboutRefsInRender = true;
}
}

if (componentOrHandle == null) {
return null;
}
// $FlowFixMe Flow has hardcoded values for React DOM that don't work with RN
if (componentOrHandle._nativeTag) {
// $FlowFixMe Flow has hardcoded values for React DOM that don't work with RN
return componentOrHandle;

// For compatibility with Paper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: avoid codename "Paper", use "legacy renderer"

if (componentOrHandle._nativeTag != null) {
// $FlowExpectedError[incompatible-cast] For compatibility with Paper (when using Fabric)
return (componentOrHandle: PublicInstance);
}
// $FlowFixMe Flow has hardcoded values for React DOM that don't work with RN
if (componentOrHandle.canonical && componentOrHandle.canonical._nativeTag) {
// $FlowFixMe Flow has hardcoded values for React DOM that don't work with RN
return componentOrHandle.canonical;

// Fabric-specific
if (componentOrHandle.publicInstance != null) {
// $FlowExpectedError[incompatible-cast] For compatibility with Fabric (when using Paper)
return (componentOrHandle.publicInstance: PublicInstance);
}

let hostInstance;
if (__DEV__) {
hostInstance = findHostInstanceWithWarning(
Expand Down Expand Up @@ -111,19 +117,28 @@ function findNodeHandle(componentOrHandle: any): ?number {
owner.stateNode._warnedAboutRefsInRender = true;
}
}

if (componentOrHandle == null) {
return null;
}

if (typeof componentOrHandle === 'number') {
// Already a node handle
return componentOrHandle;
}

// For compatibility with Paper
if (componentOrHandle._nativeTag) {
return componentOrHandle._nativeTag;
}
if (componentOrHandle.canonical && componentOrHandle.canonical._nativeTag) {
return componentOrHandle.canonical._nativeTag;

if (componentOrHandle.internals != null) {
const nativeTag = componentOrHandle.internals.nativeTag;
if (nativeTag != null) {
return nativeTag;
}
}

let hostInstance;
if (__DEV__) {
hostInstance = findHostInstanceWithWarning(
Expand All @@ -138,7 +153,14 @@ function findNodeHandle(componentOrHandle: any): ?number {
return hostInstance;
}

return hostInstance._nativeTag;
// $FlowExpectedError[prop-missing] For compatibility with Paper (when using Fabric)
if (hostInstance._nativeTag != null) {
// $FlowExpectedError[incompatible-return]
return hostInstance._nativeTag;
}

// $FlowExpectedError[incompatible-call] For compatibility with Fabric (when using Paper)
return getNativeTagFromPublicInstance(hostInstance);
}

function dispatchCommand(handle: any, command: string, args: Array<any>) {
Expand Down Expand Up @@ -265,6 +287,7 @@ export {
};

injectIntoDevTools({
// $FlowExpectedError[incompatible-call] The type of `Instance` in `getClosestInstanceFromNode` does not match in Fabric and Paper, so it fails to typecheck here.
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: __DEV__ ? 1 : 0,
version: ReactVersion,
Expand Down
53 changes: 39 additions & 14 deletions packages/react-native-renderer/src/ReactFabricComponentTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
*/

function getInstanceFromInstance(instanceHandle) {
return instanceHandle;
import type {
PublicInstance,
Instance,
Props,
TextInstance,
} from './ReactFabricHostConfig';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import {getPublicInstance} from './ReactFabricHostConfig';

// `node` is typed incorrectly here. The proper type should be `PublicInstance`.
// This is ok in DOM because they types are interchangeable, but in React Native
// they aren't.
function getInstanceFromNode(node: Instance | TextInstance): Fiber | null {
const instance: Instance = (node: $FlowFixMe); // In React Native, node is never a text instance

if (
instance.internals != null &&
instance.internals.internalInstanceHandle != null
) {
return instance.internals.internalInstanceHandle;
}

// $FlowFixMe[incompatible-return] DevTools incorrectly passes a fiber in React Native.
return node;
}

function getTagFromInstance(inst) {
const nativeInstance = inst.stateNode.canonical;
function getNodeFromInstance(fiber: Fiber): PublicInstance {
const publicInstance = getPublicInstance(fiber.stateNode);

if (!nativeInstance._nativeTag) {
throw new Error('All native instances should have a tag.');
if (publicInstance == null) {
throw new Error('Could not find host instance from fiber');
}

return nativeInstance;
return publicInstance;
}

function getFiberCurrentPropsFromNode(instance: Instance): Props {
return instance && instance.internals && instance.internals.currentProps;
}

export {
getInstanceFromInstance as getClosestInstanceFromNode,
getInstanceFromInstance as getInstanceFromNode,
getTagFromInstance as getNodeFromInstance,
getInstanceFromNode,
getInstanceFromNode as getClosestInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
};

export function getFiberCurrentPropsFromNode(inst) {
return inst.canonical.currentProps;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {batchedUpdates} from './legacy-events/ReactGenericBatching';
import accumulateInto from './legacy-events/accumulateInto';

import {getPublicInstance} from './ReactFabricHostConfig';
import getListener from './ReactNativeGetListener';
import {runEventsInBatch} from './legacy-events/EventBatching';

Expand Down Expand Up @@ -92,7 +93,8 @@ export function dispatchEvent(
const stateNode = targetFiber.stateNode;
// Guard against Fiber being unmounted
if (stateNode != null) {
eventTarget = stateNode.canonical;
// $FlowExpectedError[incompatible-cast] public instances in Fabric do not implement `EventTarget` yet.
eventTarget = (getPublicInstance(stateNode): EventTarget);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,24 @@
* @flow
*/

// Module provided by RN:
import {UIManager} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';

const ReactFabricGlobalResponderHandler = {
onChange: function (from: any, to: any, blockNativeResponder: boolean) {
const fromOrTo = from || to;
const fromOrToStateNode = fromOrTo && fromOrTo.stateNode;
const isFabric = !!(
fromOrToStateNode && fromOrToStateNode.canonical._internalInstanceHandle
);

if (isFabric) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this safe removal because this handler file is solely handling fabric? no mixed case?

if (from) {
// equivalent to clearJSResponder
nativeFabricUIManager.setIsJSResponder(
from.stateNode.node,
false,
blockNativeResponder || false,
);
}
if (from) {
// equivalent to clearJSResponder
nativeFabricUIManager.setIsJSResponder(
from.stateNode.node,
false,
blockNativeResponder || false,
);
}

if (to) {
// equivalent to setJSResponder
nativeFabricUIManager.setIsJSResponder(
to.stateNode.node,
true,
blockNativeResponder || false,
);
}
} else {
if (to !== null) {
const tag = to.stateNode.canonical._nativeTag;
UIManager.setJSResponder(tag, blockNativeResponder);
} else {
UIManager.clearJSResponder();
}
if (to) {
// equivalent to setJSResponder
nativeFabricUIManager.setIsJSResponder(
to.stateNode.node,
true,
blockNativeResponder || false,
);
}
},
};
Expand Down
Loading