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

[RN] Move definition of public instances to ReactNativePrivateInterface #32446

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
* @flow
*/

import type {ViewConfig} from './ReactNativeTypes';
import type {
HostInstance,
LegacyPublicInstance,
MeasureOnSuccessCallback,
MeasureInWindowOnSuccessCallback,
MeasureLayoutOnSuccessCallback,
MeasureOnSuccessCallback,
INativeMethods,
ViewConfig,
} from './ReactNativeTypes';
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
import type {Instance} from './ReactFiberConfigNative';

// Modules provided by RN:
Expand All @@ -29,7 +28,7 @@ import {
warnForStyleProps,
} from './NativeMethodsMixinUtils';

class ReactNativeFiberHostComponent implements INativeMethods {
class ReactNativeFiberHostComponent implements LegacyPublicInstance {
_children: Array<Instance | number>;
_nativeTag: number;
_internalFiberInstanceHandleDEV: Object;
Expand Down Expand Up @@ -71,7 +70,7 @@ class ReactNativeFiberHostComponent implements INativeMethods {
}

measureLayout(
relativeToNativeNode: number | HostInstance,
relativeToNativeNode: number | LegacyPublicInstance,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail?: () => void /* currently unused */,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ function getInspectorDataForViewAtPoint(
pointerY: locationY,
frame: {left, top, width, height},
touchedViewTag: nativeViewTag,
// $FlowExpectedError[incompatible-call]
closestPublicInstance: nativeViewTag,
});
},
Expand Down
14 changes: 5 additions & 9 deletions packages/react-native-renderer/src/ReactNativePublicCompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* @flow
*/

import type {Node, HostComponent} from './ReactNativeTypes';
import type {Node} from './ReactNativeTypes';
import type {ElementRef, ElementType} from 'react';
import type {PublicInstance} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';

// Modules provided by RN:
import {
Expand All @@ -32,7 +33,7 @@ import {

export function findHostInstance_DEPRECATED<TElementType: ElementType>(
componentOrHandle: ?(ElementRef<TElementType> | number),
): ?ElementRef<HostComponent<{...}>> {
): ?PublicInstance {
if (__DEV__) {
const owner = currentOwner;
if (owner !== null && isRendering && owner.stateNode !== null) {
Expand Down Expand Up @@ -222,15 +223,10 @@ export function getNodeFromInternalInstanceHandle(
);
}

// Should have been PublicInstance from ReactFiberConfigFabric
type FabricPublicInstance = mixed;
// Should have been PublicInstance from ReactFiberConfigNative
type PaperPublicInstance = HostComponent<empty>;

// Remove this once Paper is no longer supported and DOM Node API are enabled by default in RN.
export function isChildPublicInstance(
parentInstance: FabricPublicInstance | PaperPublicInstance,
childInstance: FabricPublicInstance | PaperPublicInstance,
parentInstance: PublicInstance,
childInstance: PublicInstance,
): boolean {
if (__DEV__) {
// Paper
Expand Down
94 changes: 17 additions & 77 deletions packages/react-native-renderer/src/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,17 @@ import type {
ElementRef,
ElementType,
MixedElement,
RefSetter,
} from 'react';
// $FlowFixMe[nonstrict-import] TODO(@rubennorte)
import {type PublicRootInstance} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';

export type MeasureOnSuccessCallback = (
x: number,
y: number,
width: number,
height: number,
pageX: number,
pageY: number,
) => void;

export type MeasureInWindowOnSuccessCallback = (
x: number,
y: number,
width: number,
height: number,
) => void;

export type MeasureLayoutOnSuccessCallback = (
left: number,
top: number,
width: number,
height: number,
) => void;
import type {
// $FlowFixMe[nonstrict-import] TODO(@rubennorte)
MeasureOnSuccessCallback,
// $FlowFixMe[nonstrict-import] TODO(@rubennorte)
PublicInstance,
// $FlowFixMe[nonstrict-import] TODO(@rubennorte)
PublicRootInstance,
// $FlowFixMe[nonstrict-import] TODO(@rubennorte)
PublicTextInstance,
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';

export type AttributeType<T, V> =
| true
Expand Down Expand Up @@ -106,45 +90,6 @@ export type PartialViewConfig = $ReadOnly<{
validAttributes?: PartialAttributeConfiguration,
}>;

/**
* Current usages should migrate to this definition
*/
export interface INativeMethods {
blur(): void;
focus(): void;
measure(callback: MeasureOnSuccessCallback): void;
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void;
measureLayout(
relativeToNativeNode: number | HostInstance,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail?: () => void,
): void;
setNativeProps(nativeProps: {...}): void;
}

export type NativeMethods = $ReadOnly<{
blur(): void,
focus(): void,
measure(callback: MeasureOnSuccessCallback): void,
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void,
measureLayout(
relativeToNativeNode: number | HostInstance,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail?: () => void,
): void,
setNativeProps(nativeProps: {...}): void,
}>;

// This validates that INativeMethods and NativeMethods stay in sync using Flow!
declare const ensureNativeMethodsAreSynced: NativeMethods;
(ensureNativeMethodsAreSynced: INativeMethods);

export type HostInstance = NativeMethods;
export type HostComponent<Config: {...}> = component(
ref: RefSetter<HostInstance>,
...Config
);

type InspectorDataProps = $ReadOnly<{
[propName: string]: string,
...
Expand Down Expand Up @@ -211,20 +156,17 @@ export type RenderRootOptions = {
export type ReactNativeType = {
findHostInstance_DEPRECATED<TElementType: ElementType>(
componentOrHandle: ?(ElementRef<TElementType> | number),
): ?HostInstance,
): ?PublicInstance,
findNodeHandle<TElementType: ElementType>(
componentOrHandle: ?(ElementRef<TElementType> | number),
): ?number,
isChildPublicInstance(
parent: PublicInstance | HostComponent<empty>,
child: PublicInstance | HostComponent<empty>,
): boolean,
isChildPublicInstance(parent: PublicInstance, child: PublicInstance): boolean,
dispatchCommand(
handle: HostInstance,
handle: PublicInstance,
command: string,
args: Array<mixed>,
): void,
sendAccessibilityEvent(handle: HostInstance, eventType: string): void,
sendAccessibilityEvent(handle: PublicInstance, eventType: string): void,
render(
element: MixedElement,
containerTag: number,
Expand All @@ -239,23 +181,21 @@ export type ReactNativeType = {

export opaque type Node = mixed;
export opaque type InternalInstanceHandle = mixed;
type PublicInstance = mixed;
type PublicTextInstance = mixed;

export type ReactFabricType = {
findHostInstance_DEPRECATED<TElementType: ElementType>(
componentOrHandle: ?(ElementRef<TElementType> | number),
): ?HostInstance,
): ?PublicInstance,
findNodeHandle<TElementType: ElementType>(
componentOrHandle: ?(ElementRef<TElementType> | number),
): ?number,
dispatchCommand(
handle: HostInstance,
handle: PublicInstance,
command: string,
args: Array<mixed>,
): void,
isChildPublicInstance(parent: PublicInstance, child: PublicInstance): boolean,
sendAccessibilityEvent(handle: HostInstance, eventType: string): void,
sendAccessibilityEvent(handle: PublicInstance, eventType: string): void,
render(
element: MixedElement,
containerTag: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
* @flow strict-local
*/

export opaque type PublicInstance = mixed;
export opaque type PublicTextInstance = mixed;
export opaque type PublicRootInstance = mixed;

module.exports = {
get BatchedBridge() {
return require('./BatchedBridge.js');
Expand Down
40 changes: 37 additions & 3 deletions scripts/flow/react-native-host-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@

// libdefs cannot actually import. These are supposed to be the types imported
// from 'react-native-renderer/src/ReactNativeTypes'
type __MeasureOnSuccessCallback = any;
type __MeasureInWindowOnSuccessCallback = any;
type __MeasureLayoutOnSuccessCallback = any;
type __MeasureOnSuccessCallback = (
x: number,
y: number,
width: number,
height: number,
pageX: number,
pageY: number,
) => void;
type __MeasureInWindowOnSuccessCallback = (
x: number,
y: number,
width: number,
height: number,
) => void;
type __MeasureLayoutOnSuccessCallback = (
left: number,
top: number,
width: number,
height: number,
) => void;
type __ReactNativeBaseComponentViewConfig = any;
type __ViewConfigGetter = any;
type __ViewConfig = any;
Expand Down Expand Up @@ -144,6 +161,23 @@ declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'
declare export opaque type PublicInstance;
declare export opaque type PublicTextInstance;
declare export opaque type PublicRootInstance;
declare export type MeasureOnSuccessCallback = __MeasureOnSuccessCallback;
declare export type MeasureInWindowOnSuccessCallback =
__MeasureInWindowOnSuccessCallback;
declare export type MeasureLayoutOnSuccessCallback =
__MeasureLayoutOnSuccessCallback;
declare export interface LegacyPublicInstance {
blur(): void;
focus(): void;
measure(callback: __MeasureOnSuccessCallback): void;
measureInWindow(callback: __MeasureInWindowOnSuccessCallback): void;
measureLayout(
relativeToNativeNode: number | LegacyPublicInstance,
onSuccess: __MeasureLayoutOnSuccessCallback,
onFail?: () => void,
): void;
setNativeProps(nativeProps: {...}): void;
}
declare export function getNodeFromPublicInstance(
publicInstance: PublicInstance,
): Object;
Expand Down
Loading