Skip to content

Commit 68a6be9

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Import ReactFabric methods directly in renderer implementation modules (facebook#56566)
Summary: Pull Request resolved: facebook#56566 Changelog: [internal] `RendererImplementation.js` used a lazy-loading pattern (`getFabricRenderer` + per-method caches and `getMethod` helpers) to defer requiring the Fabric renderer until first use. The lazy/dynamic indirection is unnecessary in this file because it is the actual implementation — the runtime swap-at-import-time indirection lives in `RendererProxy.fb.js` (Meta-only), which uses `setImplementation` / `getImplementationForMethod` to defer loading until first use. Simplify by importing `ReactFabric` directly and re-exporting its methods via a single destructuring `export const {...} = ReactFabric` statement. This removes: - `cachedFabricRenderer` and `getFabricRenderer()` - `getMethod` / `getFabricMethod` helpers - per-method caches (`cachedFabricRender`, `cachedFabricDispatchCommand`) - the associated `$FlowExpectedError` suppressions that were only needed for the dynamic indirection Reviewed By: javache Differential Revision: D101983735 fbshipit-source-id: 7aecd701fb431fad176aedeb9884a4e79e730977
1 parent e37bba5 commit 68a6be9

2 files changed

Lines changed: 80 additions & 93 deletions

File tree

packages/react-native/Libraries/ReactNative/RendererImplementation.js

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,22 @@
88
* @format
99
*/
1010

11-
import type {HostInstance} from '../../src/private/types/HostInstance';
12-
import typeof ReactFabricType from '../Renderer/shims/ReactFabric';
13-
1411
import {
1512
onCaughtError,
1613
onRecoverableError,
1714
onUncaughtError,
1815
} from '../../src/private/renderer/errorhandling/ErrorHandlers';
16+
import ReactFabric from '../Renderer/shims/ReactFabric';
1917
import * as React from 'react';
2018

21-
let cachedFabricRenderer;
22-
23-
function getFabricRenderer(): ReactFabricType {
24-
if (cachedFabricRenderer == null) {
25-
cachedFabricRenderer = require('../Renderer/shims/ReactFabric').default;
26-
}
27-
return cachedFabricRenderer;
28-
}
29-
30-
const getMethod: <MethodName extends keyof ReactFabricType>(
31-
() => ReactFabricType,
32-
MethodName,
33-
) => ReactFabricType[MethodName] = (getRenderer, methodName) => {
34-
let cachedImpl;
35-
36-
// $FlowExpectedError[incompatible-type]
37-
return function (arg1, arg2, arg3, arg4, arg5, arg6) {
38-
if (cachedImpl == null) {
39-
// $FlowExpectedError[prop-missing]
40-
// $FlowExpectedError[invalid-computed-prop]
41-
cachedImpl = getRenderer()[methodName];
42-
}
43-
44-
// $FlowExpectedError[extra-arg]
45-
return cachedImpl(arg1, arg2, arg3, arg4, arg5);
46-
};
47-
};
48-
49-
function getFabricMethod<MethodName extends keyof ReactFabricType>(
50-
methodName: MethodName,
51-
): ReactFabricType[MethodName] {
52-
return getMethod(getFabricRenderer, methodName);
53-
}
54-
55-
let cachedFabricRender;
56-
5719
export function renderElement({
5820
element,
5921
rootTag,
6022
}: {
6123
element: React.MixedElement,
6224
rootTag: number,
6325
}): void {
64-
if (cachedFabricRender == null) {
65-
cachedFabricRender = getFabricRenderer().render;
66-
}
67-
68-
cachedFabricRender(
26+
ReactFabric.render(
6927
element,
7028
rootTag,
7129
/* callback */ null,
@@ -78,34 +36,22 @@ export function renderElement({
7836
);
7937
}
8038

81-
let cachedFabricDispatchCommand;
82-
83-
export function dispatchCommand(
84-
handle: HostInstance,
85-
command: string,
86-
args: Array<unknown>,
87-
): void {
88-
if (cachedFabricDispatchCommand == null) {
89-
cachedFabricDispatchCommand = getFabricRenderer().dispatchCommand;
90-
}
91-
92-
return cachedFabricDispatchCommand(handle, command, args);
93-
}
94-
95-
export const findHostInstance_DEPRECATED: <
96-
TElementType extends React.ElementType,
97-
>(
98-
// $FlowExpectedError[incompatible-type]
99-
componentOrHandle: ?(React.ElementRef<TElementType> | number),
100-
) => ?HostInstance = getFabricMethod('findHostInstance_DEPRECATED');
101-
102-
export const findNodeHandle: <TElementType extends React.ElementType>(
103-
// $FlowExpectedError[incompatible-type]
104-
componentOrHandle: ?(React.ElementRef<TElementType> | number),
105-
) => ?number = getFabricMethod('findNodeHandle');
106-
107-
export const sendAccessibilityEvent: ReactFabricType['sendAccessibilityEvent'] =
108-
getFabricMethod('sendAccessibilityEvent');
39+
// NOTE: these cannot be combined into a single destructuring statement because
40+
// `flow-api-translator` (used by `yarn build-types` to generate the
41+
// open-source type definitions) does not support destructuring in variable
42+
// declarations.
43+
export const dispatchCommand = ReactFabric.dispatchCommand;
44+
export const findHostInstance_DEPRECATED =
45+
ReactFabric.findHostInstance_DEPRECATED;
46+
export const findNodeHandle = ReactFabric.findNodeHandle;
47+
export const sendAccessibilityEvent = ReactFabric.sendAccessibilityEvent;
48+
export const isChildPublicInstance = ReactFabric.isChildPublicInstance;
49+
export const getNodeFromInternalInstanceHandle =
50+
ReactFabric.getNodeFromInternalInstanceHandle;
51+
export const getPublicInstanceFromInternalInstanceHandle =
52+
ReactFabric.getPublicInstanceFromInternalInstanceHandle;
53+
export const getPublicInstanceFromRootTag =
54+
ReactFabric.getPublicInstanceFromRootTag;
10955

11056
export function unstable_batchedUpdates<T>(
11157
fn: (bookkeeping: T) => void,
@@ -114,18 +60,6 @@ export function unstable_batchedUpdates<T>(
11460
fn(bookkeeping);
11561
}
11662

117-
export const isChildPublicInstance: ReactFabricType['isChildPublicInstance'] =
118-
getFabricMethod('isChildPublicInstance');
119-
120-
export const getNodeFromInternalInstanceHandle: ReactFabricType['getNodeFromInternalInstanceHandle'] =
121-
getFabricMethod('getNodeFromInternalInstanceHandle');
122-
123-
export const getPublicInstanceFromInternalInstanceHandle: ReactFabricType['getPublicInstanceFromInternalInstanceHandle'] =
124-
getFabricMethod('getPublicInstanceFromInternalInstanceHandle');
125-
126-
export const getPublicInstanceFromRootTag: ReactFabricType['getPublicInstanceFromRootTag'] =
127-
getFabricMethod('getPublicInstanceFromRootTag');
128-
12963
export function isProfilingRenderer(): boolean {
13064
return Boolean(__DEV__);
13165
}

packages/react-native/ReactNativeApi.d.ts

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<ad3edd7df03c5d97bc1bbfc138e3b283>>
7+
* @generated SignedSource<<1c8637ab03a5fec9d39704d1ae305595>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -88,6 +88,7 @@ declare const $$index: {
8888
declare const $$NativeDeviceInfo: typeof NativeDeviceInfo_default
8989
declare const $$NativeDialogManagerAndroid: null | Spec | undefined
9090
declare const $$ProgressBarAndroidNativeComponent: HostComponent<AndroidProgressBarNativeProps>
91+
declare const $$ReactFabric: typeof ReactFabric_default
9192
declare const $$ScrollViewContext: typeof ScrollViewContext_default
9293
declare const $$ScrollViewNativeComponent: typeof ScrollViewNativeComponent_default
9394
declare const $$SwitchNativeComponent: ComponentType
@@ -234,12 +235,7 @@ declare const eventImpl: <T>(
234235
argMapping: ReadonlyArray<Mapping | null | undefined>,
235236
config: EventConfig<T>,
236237
) => any
237-
declare const findNodeHandle: <TElementType extends React.ElementType>(
238-
componentOrHandle:
239-
| (number | React.ComponentRef<TElementType>)
240-
| null
241-
| undefined,
242-
) => null | number | undefined
238+
declare const findNodeHandle: typeof $$ReactFabric.findNodeHandle
243239
declare const flatten: typeof $$flattenStyle
244240
declare const forkEvent: typeof $$AnimatedImplementation.forkEvent
245241
declare const hairlineWidth: number
@@ -387,6 +383,7 @@ declare const RCTNetworking_default: {
387383
withCredentials: boolean,
388384
): void
389385
}
386+
declare let ReactFabric_default: ReactFabricType
390387
declare const ReadOnlyNodeBase: typeof Object
391388
declare const registerCallableModule: typeof registerCallableModule_default
392389
declare const registerCallableModule_default: RegisterCallableModule
@@ -1017,6 +1014,7 @@ declare type $$NativeDeviceInfo = typeof $$NativeDeviceInfo
10171014
declare type $$NativeDialogManagerAndroid = typeof $$NativeDialogManagerAndroid
10181015
declare type $$ProgressBarAndroidNativeComponent =
10191016
typeof $$ProgressBarAndroidNativeComponent
1017+
declare type $$ReactFabric = typeof $$ReactFabric
10201018
declare type $$ScrollViewContext = typeof $$ScrollViewContext
10211019
declare type $$ScrollViewNativeComponent = typeof $$ScrollViewNativeComponent
10221020
declare type $$SwitchNativeComponent = typeof $$SwitchNativeComponent
@@ -3424,6 +3422,9 @@ declare interface NativeUIEvent {
34243422
}
34253423
declare type NativeVirtualText = typeof NativeVirtualText
34263424
declare type Networking = typeof Networking
3425+
declare type Node = symbol & {
3426+
__Node__: string
3427+
}
34273428
declare class NodeList_default<T> implements Iterable<T>, ArrayLike_2<T> {
34283429
[index: number]: T
34293430
readonly length: number
@@ -3989,6 +3990,36 @@ declare type RCTNetworkingEventDefinitions = {
39893990
]
39903991
readonly didSendNetworkData: [[number, number, number]]
39913992
}
3993+
declare type ReactFabricType = {
3994+
dispatchCommand(
3995+
handle: HostInstance,
3996+
command: string,
3997+
args: Array<unknown>,
3998+
): void
3999+
findHostInstance_DEPRECATED<TElementType extends React.ElementType>(
4000+
componentOrHandle: (number | React.ComponentRef<TElementType>) | undefined,
4001+
): HostInstance | undefined
4002+
findNodeHandle<TElementType extends React.ElementType>(
4003+
componentOrHandle: (number | React.ComponentRef<TElementType>) | undefined,
4004+
): number | undefined
4005+
getNodeFromInternalInstanceHandle(
4006+
internalInstanceHandle: InternalInstanceHandle,
4007+
): Node | undefined
4008+
getPublicInstanceFromInternalInstanceHandle(
4009+
internalInstanceHandle: InternalInstanceHandle,
4010+
): HostInstance | null | PublicTextInstance
4011+
getPublicInstanceFromRootTag(rootTag: number): null | PublicRootInstance
4012+
isChildPublicInstance(parent: HostInstance, child: HostInstance): boolean
4013+
render(
4014+
element: React.JSX.Element,
4015+
containerTag: number,
4016+
callback: (() => void) | undefined,
4017+
concurrentRoot: boolean | undefined,
4018+
options: RenderRootOptions | undefined,
4019+
): React.ComponentRef<React.ElementType> | undefined
4020+
sendAccessibilityEvent(handle: HostInstance, eventType: string): void
4021+
unmountComponentAtNode(containerTag: number): void
4022+
}
39924023
declare class ReactNativeDocument_default extends ReadOnlyNode_default {
39934024
get childElementCount(): number
39944025
get children(): HTMLCollection_default<ReadOnlyElement_default>
@@ -4188,6 +4219,28 @@ declare type Registry = {
41884219
runnables: Runnables
41894220
sections: ReadonlyArray<string>
41904221
}
4222+
declare type RenderRootOptions = {
4223+
onCaughtError?: (
4224+
error: unknown,
4225+
errorInfo: {
4226+
readonly componentStack?: string
4227+
readonly errorBoundary?: React.Component<any, any>
4228+
},
4229+
) => void
4230+
onDefaultTransitionIndicator?: () => (() => void) | void
4231+
onRecoverableError?: (
4232+
error: unknown,
4233+
errorInfo: {
4234+
readonly componentStack?: string
4235+
},
4236+
) => void
4237+
onUncaughtError?: (
4238+
error: unknown,
4239+
errorInfo: {
4240+
readonly componentStack?: string
4241+
},
4242+
) => void
4243+
}
41914244
declare type RequestBody =
41924245
| ArrayBuffer
41934246
| ArrayBufferView
@@ -6011,7 +6064,7 @@ export {
60116064
AlertOptions, // a0cdac0f
60126065
AlertType, // 5ab91217
60136066
AndroidKeyboardEvent, // e03becc8
6014-
Animated, // a77a3944
6067+
Animated, // 0652b5d1
60156068
AppConfig, // ce4209a7
60166069
AppRegistry, // 5edf0524
60176070
AppState, // 12012be5
@@ -6276,7 +6329,7 @@ export {
62766329
WrapperComponentProvider, // 9cf3844c
62776330
codegenNativeCommands, // 628a7c0a
62786331
codegenNativeComponent, // 2baac257
6279-
findNodeHandle, // 65981202
6332+
findNodeHandle, // 93f80214
62806333
processColor, // 6e877698
62816334
registerCallableModule, // 839c8cfe
62826335
requireNativeComponent, // 7f7f105a

0 commit comments

Comments
 (0)