Skip to content

Commit

Permalink
UIManagerJSInterface: Make reactTags non-nullable (#42208)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #42208

I went through the [android](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java?fbclid=IwAR29Xk1KiGsxg7O2Fc7F2k4uKFmqqAbcMtsEEdaBalcGjJK7xuyYDo6X1Z8) and [ios](https://github.com/facebook/react-native/blob/main/packages/react-native/React/Modules/RCTUIManager.m?fbclid=IwAR2axOTNVuvGbnGw0_1wlnRhftx4W6K-ptTm_DtQ-eSOLFtnrYwzbjPyeYA) implementations of UIManagerModule. It turns out, the reactTag is always nonnull in all methods!

This diff updates UIManagerJSInterface accordingly.

Changelog: [General][Changed] - UIManagerModule: Make reactTags required

Reviewed By: sammy-SC

Differential Revision: D52627087

fbshipit-source-id: e79650f539fa2fbc00654a1ecce4fae2da8ae11a
  • Loading branch information
RSNara authored and facebook-github-bot committed Feb 3, 2024
1 parent 4473fe8 commit fafd00a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
84 changes: 17 additions & 67 deletions packages/react-native/Libraries/ReactNative/BridgelessUIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const getDefaultEventTypesCached = (function () {
*/
const UIManagerJSOverridenAPIs = {
measure: (
reactTag: ?number,
reactTag: number,
callback: (
left: number,
top: number,
Expand All @@ -78,14 +78,14 @@ const UIManagerJSOverridenAPIs = {
raiseSoftError('measure');
},
measureInWindow: (
reactTag: ?number,
reactTag: number,
callback: (x: number, y: number, width: number, height: number) => void,
): void => {
raiseSoftError('measureInWindow');
},
measureLayout: (
reactTag: ?number,
ancestorReactTag: ?number,
reactTag: number,
ancestorReactTag: number,
errorCallback: (error: Object) => void,
callback: (
left: number,
Expand All @@ -97,7 +97,7 @@ const UIManagerJSOverridenAPIs = {
raiseSoftError('measureLayout');
},
measureLayoutRelativeToParent: (
reactTag: ?number,
reactTag: number,
errorCallback: (error: Object) => void,
callback: (
left: number,
Expand All @@ -109,7 +109,7 @@ const UIManagerJSOverridenAPIs = {
raiseSoftError('measureLayoutRelativeToParent');
},
dispatchViewManagerCommand: (
reactTag: ?number,
reactTag: number,
commandID: number,
commandArgs: ?Array<string | number | boolean>,
): void => {
Expand All @@ -124,7 +124,7 @@ const UIManagerJSOverridenAPIs = {
*/
const UIManagerJSUnusedInNewArchAPIs = {
createView: (
reactTag: ?number,
reactTag: number,
viewName: string,
rootTag: RootTag,
props: Object,
Expand All @@ -134,11 +134,11 @@ const UIManagerJSUnusedInNewArchAPIs = {
updateView: (reactTag: number, viewName: string, props: Object): void => {
raiseSoftError('updateView');
},
setChildren: (containerTag: ?number, reactTags: Array<number>): void => {
setChildren: (containerTag: number, reactTags: Array<number>): void => {
raiseSoftError('setChildren');
},
manageChildren: (
containerTag: ?number,
containerTag: number,
moveFromIndices: Array<number>,
moveToIndices: Array<number>,
addChildReactTags: Array<number>,
Expand All @@ -147,7 +147,7 @@ const UIManagerJSUnusedInNewArchAPIs = {
): void => {
raiseSoftError('manageChildren');
},
setJSResponder: (reactTag: ?number, blockNativeResponder: boolean): void => {
setJSResponder: (reactTag: number, blockNativeResponder: boolean): void => {
raiseSoftError('setJSResponder');
},
clearJSResponder: (): void => {
Expand All @@ -163,7 +163,7 @@ const UIManagerJSDeprecatedPlatformAPIs = Platform.select({
android: {
// TODO(T175424986): Remove UIManager.showPopupMenu() in React Native v0.75.
showPopupMenu: (
reactTag: ?number,
reactTag: number,
items: Array<string>,
error: (error: Object) => void,
success: (event: string, selected?: number) => void,
Expand Down Expand Up @@ -213,16 +213,7 @@ const UIManagerJSPlatformAPIs = Platform.select({
);
}
},
sendAccessibilityEvent: (reactTag: ?number, eventType: number): void => {
if (reactTag == null) {
console.error(
`sendAccessibilityEvent() dropping event: Cannot be called with ${String(
reactTag,
)} reactTag`,
);
return;
}

sendAccessibilityEvent: (reactTag: number, eventType: number): void => {
// Keep this in sync with java:FabricUIManager.sendAccessibilityEventFromJS
// and legacySendAccessibilityEvent.android.js
const AccessibilityEvent = {
Expand Down Expand Up @@ -271,14 +262,7 @@ const UIManagerJSPlatformAPIs = Platform.select({
raiseSoftError('lazilyLoadView');
return {};
},
focus: (reactTag: ?number): void => {
if (reactTag == null) {
console.error(
`focus() noop: Cannot be called with ${String(reactTag)} reactTag`,
);
return;
}

focus: (reactTag: number): void => {
const FabricUIManager = nullthrows(getFabricUIManager());
const shadowNode =
FabricUIManager.findShadowNodeByTag_DEPRECATED(reactTag);
Expand All @@ -288,14 +272,7 @@ const UIManagerJSPlatformAPIs = Platform.select({
}
FabricUIManager.dispatchCommand(shadowNode, 'focus', []);
},
blur: (reactTag: ?number): void => {
if (reactTag == null) {
console.error(
`blur() noop: Cannot be called with ${String(reactTag)} reactTag`,
);
return;
}

blur: (reactTag: number): void => {
const FabricUIManager = nullthrows(getFabricUIManager());
const shadowNode =
FabricUIManager.findShadowNodeByTag_DEPRECATED(reactTag);
Expand Down Expand Up @@ -344,7 +321,7 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
}
},
findSubviewIn: (
reactTag: ?number,
reactTag: number,
point: Array<number>,
callback: (
nativeViewTag: number,
Expand All @@ -354,15 +331,6 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
height: number,
) => void,
): void => {
if (reactTag == null) {
console.error(
`findSubviewIn() noop: Cannot be called with ${String(
reactTag,
)} reactTag`,
);
return;
}

const FabricUIManager = nullthrows(getFabricUIManager());
const shadowNode = FabricUIManager.findShadowNodeByTag_DEPRECATED(reactTag);

Expand Down Expand Up @@ -404,19 +372,10 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
);
},
viewIsDescendantOf: (
reactTag: ?number,
ancestorReactTag: ?number,
reactTag: number,
ancestorReactTag: number,
callback: (result: Array<boolean>) => void,
): void => {
if (reactTag == null) {
console.error(
`viewIsDescendantOf() noop: Cannot be called with ${String(
reactTag,
)} reactTag`,
);
return;
}

const FabricUIManager = nullthrows(getFabricUIManager());
const shadowNode = FabricUIManager.findShadowNodeByTag_DEPRECATED(reactTag);
if (!shadowNode) {
Expand All @@ -426,15 +385,6 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
return;
}

if (ancestorReactTag == null) {
console.error(
`viewIsDescendantOf() noop: Cannot be called with ${String(
ancestorReactTag,
)} ancestorReactTag`,
);
return;
}

const ancestorShadowNode =
FabricUIManager.findShadowNodeByTag_DEPRECATED(ancestorReactTag);
if (!ancestorShadowNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function getViewManagerConfig(viewManagerName: string): any {
const UIManagerJS: UIManagerJSInterface = {
...NativeUIManager,
createView(
reactTag: ?number,
reactTag: number,
viewName: string,
rootTag: RootTag,
props: Object,
Expand Down
26 changes: 13 additions & 13 deletions packages/react-native/src/private/specs/modules/NativeUIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboMod
export interface Spec extends TurboModule {
+getConstants: () => Object;
+createView: (
reactTag: ?number,
reactTag: number,
viewName: string,
rootTag: RootTag,
props: Object,
) => void;
+updateView: (reactTag: number, viewName: string, props: Object) => void;
+findSubviewIn: (
reactTag: ?number,
reactTag: number,
point: Array<number>,
callback: (
nativeViewTag: number,
Expand All @@ -34,8 +34,8 @@ export interface Spec extends TurboModule {
) => void,
) => void;
+dispatchViewManagerCommand: (
reactTag: ?number,
commandID: number,
reactTag: number,
commandID: number, // number || string
commandArgs: ?Array<any>,
) => void;
+measure: (
Expand All @@ -54,8 +54,8 @@ export interface Spec extends TurboModule {
callback: (x: number, y: number, width: number, height: number) => void,
) => void;
+viewIsDescendantOf: (
reactTag: ?number,
ancestorReactTag: ?number,
reactTag: number,
ancestorReactTag: number,
callback: (result: Array<boolean>) => void,
) => void;
+measureLayout: (
Expand All @@ -79,16 +79,16 @@ export interface Spec extends TurboModule {
height: number,
) => void,
) => void;
+setJSResponder: (reactTag: ?number, blockNativeResponder: boolean) => void;
+setJSResponder: (reactTag: number, blockNativeResponder: boolean) => void;
+clearJSResponder: () => void;
+configureNextLayoutAnimation: (
config: Object,
callback: () => void, // check what is returned here
errorCallback: (error: Object) => void,
) => void;
+setChildren: (containerTag: ?number, reactTags: Array<number>) => void;
+setChildren: (containerTag: number, reactTags: Array<number>) => void;
+manageChildren: (
containerTag: ?number,
containerTag: number,
moveFromIndices: Array<number>,
moveToIndices: Array<number>,
addChildReactTags: Array<number>,
Expand All @@ -100,9 +100,9 @@ export interface Spec extends TurboModule {
+getConstantsForViewManager?: (viewManagerName: string) => ?Object;
+getDefaultEventTypes?: () => Array<string>;
+setLayoutAnimationEnabledExperimental?: (enabled: boolean) => void;
+sendAccessibilityEvent?: (reactTag: ?number, eventType: number) => void;
+sendAccessibilityEvent?: (reactTag: number, eventType: number) => void;
+showPopupMenu?: (
reactTag: ?number,
reactTag: number,
items: Array<string>,
error: (error: Object) => void,
success: (event: string, selected?: number) => void,
Expand All @@ -111,8 +111,8 @@ export interface Spec extends TurboModule {

// ios only
+lazilyLoadView?: (name: string) => Object; // revisit return
+focus?: (reactTag: ?number) => void;
+blur?: (reactTag: ?number) => void;
+focus?: (reactTag: number) => void;
+blur?: (reactTag: number) => void;
}

export default (TurboModuleRegistry.getEnforcing<Spec>('UIManager'): Spec);
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ export function callNativeMethodToChangeBackgroundColor(
console.log('viewRef is null');
return;
}
const reactTag = ReactNative.findNodeHandle(viewRef);
if (reactTag == null) {
console.log('reactTag is null');
return;
}
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(viewRef),
reactTag,
UIManager.getViewManagerConfig(
'RNTMyLegacyNativeView',
).Commands.changeBackgroundColor.toString(),
Expand All @@ -60,8 +65,14 @@ export function callNativeMethodToAddOverlays(
console.log('viewRef is null');
return;
}
const reactTag = ReactNative.findNodeHandle(viewRef);
if (reactTag == null) {
console.log('reactTag is null');
return;
}

UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(viewRef),
reactTag,
UIManager.getViewManagerConfig(
'RNTMyLegacyNativeView',
).Commands.addOverlays.toString(),
Expand All @@ -76,8 +87,14 @@ export function callNativeMethodToRemoveOverlays(
console.log('viewRef is null');
return;
}
const reactTag = ReactNative.findNodeHandle(viewRef);
if (reactTag == null) {
console.log('reactTag is null');
return;
}

UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(viewRef),
reactTag,
UIManager.getViewManagerConfig(
'RNTMyLegacyNativeView',
).Commands.removeOverlays.toString(),
Expand Down

0 comments on commit fafd00a

Please sign in to comment.