Skip to content

Commit

Permalink
BridgelessUIManager: Align platform-only methods w/ PaperUIManager (#…
Browse files Browse the repository at this point in the history
…41997)

Summary:

Many methods on PaperUIMangaer are iOS only.

Many methods on PaperUIManager are Android  only.

This diff makes sure that BridgelessUIManager only exports Android methods on Android, and iOS methods on iOS.

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D52012876
  • Loading branch information
RSNara authored and facebook-github-bot committed Jan 9, 2024
1 parent f7f9250 commit bd2a0c6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import UIManager from '../../ReactNative/UIManager';
import nullthrows from 'nullthrows';

/**
* This is a function exposed to the React Renderer that can be used by the
Expand All @@ -19,13 +20,13 @@ function legacySendAccessibilityEvent(
eventType: string,
): void {
if (eventType === 'focus') {
UIManager.sendAccessibilityEvent(
nullthrows(UIManager.sendAccessibilityEvent)(
reactTag,
UIManager.getConstants().AccessibilityEventTypes.typeViewFocused,
);
}
if (eventType === 'click') {
UIManager.sendAccessibilityEvent(
nullthrows(UIManager.sendAccessibilityEvent)(
reactTag,
UIManager.getConstants().AccessibilityEventTypes.typeViewClicked,
);
Expand Down
85 changes: 46 additions & 39 deletions packages/react-native/Libraries/ReactNative/BridgelessUIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {RootTag} from '../Types/RootTagTypes';
import type {UIManagerJSInterface} from '../Types/UIManagerJSInterface';

import {unstable_hasComponent} from '../NativeComponent/NativeComponentRegistryUnstable';
import Platform from '../Utilities/Platform';
import nullthrows from 'nullthrows';

function raiseSoftError(methodName: string, details?: string): void {
Expand Down Expand Up @@ -96,8 +97,53 @@ const UIManagerJSOverridenAPIs = {
},
};

const UIManagerJSPlatformAPIs = Platform.select({
android: {
getConstantsForViewManager: (viewManagerName: string): Object => {
raiseSoftError('getConstantsForViewManager');
return {};
},
getDefaultEventTypes: (): Array<string> => {
raiseSoftError('getDefaultEventTypes');
return [];
},
setLayoutAnimationEnabledExperimental: (enabled: boolean): void => {
raiseSoftError('setLayoutAnimationEnabledExperimental');
},
// Please use AccessibilityInfo.sendAccessibilityEvent instead.
// See SetAccessibilityFocusExample in AccessibilityExample.js for a migration example.
sendAccessibilityEvent: (reactTag: ?number, eventType: number): void => {
raiseSoftError('sendAccessibilityEvent');
},
showPopupMenu: (
reactTag: ?number,
items: Array<string>,
error: (error: Object) => void,
success: (event: string, selected?: number) => void,
): void => {
raiseSoftError('showPopupMenu');
},
dismissPopupMenu: (): void => {
raiseSoftError('dismissPopupMenu');
},
},
ios: {
lazilyLoadView: (name: string): Object => {
raiseSoftError('lazilyLoadView');
return {};
},
focus: (reactTag: ?number): void => {
raiseSoftError('focus');
},
blur: (reactTag: ?number): void => {
raiseSoftError('blur');
},
},
});

const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
...UIManagerJSOverridenAPIs,
...UIManagerJSPlatformAPIs,
getViewManagerConfig: (viewManagerName: string): mixed => {
if (getUIManagerConstants) {
return getUIManagerConstantsCache()[viewManagerName];
Expand All @@ -120,18 +166,6 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
return null;
}
},
getConstantsForViewManager: (viewManagerName: string): Object => {
raiseSoftError('getConstantsForViewManager');
return {};
},
getDefaultEventTypes: (): Array<string> => {
raiseSoftError('getDefaultEventTypes');
return [];
},
lazilyLoadView: (name: string): Object => {
raiseSoftError('lazilyLoadView');
return {};
},
createView: (
reactTag: ?number,
viewName: string,
Expand All @@ -143,12 +177,6 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
updateView: (reactTag: number, viewName: string, props: Object): void => {
raiseSoftError('updateView');
},
focus: (reactTag: ?number): void => {
raiseSoftError('focus');
},
blur: (reactTag: ?number): void => {
raiseSoftError('blur');
},
findSubviewIn: (
reactTag: ?number,
point: Array<number>,
Expand Down Expand Up @@ -204,27 +232,6 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
): void => {
raiseSoftError('manageChildren');
},

// Android only
setLayoutAnimationEnabledExperimental: (enabled: boolean): void => {
raiseSoftError('setLayoutAnimationEnabledExperimental');
},
// Please use AccessibilityInfo.sendAccessibilityEvent instead.
// See SetAccessibilityFocusExample in AccessibilityExample.js for a migration example.
sendAccessibilityEvent: (reactTag: ?number, eventType: number): void => {
raiseSoftError('sendAccessibilityEvent');
},
showPopupMenu: (
reactTag: ?number,
items: Array<string>,
error: (error: Object) => void,
success: (event: string, selected?: number) => void,
): void => {
raiseSoftError('showPopupMenu');
},
dismissPopupMenu: (): void => {
raiseSoftError('dismissPopupMenu');
},
};

if (getUIManagerConstants) {
Expand Down
20 changes: 11 additions & 9 deletions packages/react-native/Libraries/ReactNative/NativeUIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';

export interface Spec extends TurboModule {
+getConstants: () => Object;
+getConstantsForViewManager: (viewManagerName: string) => Object;
+getDefaultEventTypes: () => Array<string>;
+lazilyLoadView: (name: string) => Object; // revisit return
+createView: (
reactTag: ?number,
viewName: string,
rootTag: RootTag,
props: Object,
) => void;
+updateView: (reactTag: number, viewName: string, props: Object) => void;
+focus: (reactTag: ?number) => void;
+blur: (reactTag: ?number) => void;
+findSubviewIn: (
reactTag: ?number,
point: Array<number>,
Expand Down Expand Up @@ -107,15 +102,22 @@ export interface Spec extends TurboModule {
) => void;

// Android only
+setLayoutAnimationEnabledExperimental: (enabled: boolean) => void;
+sendAccessibilityEvent: (reactTag: ?number, eventType: number) => void;
+showPopupMenu: (
+getConstantsForViewManager?: (viewManagerName: string) => Object;
+getDefaultEventTypes?: () => Array<string>;
+setLayoutAnimationEnabledExperimental?: (enabled: boolean) => void;
+sendAccessibilityEvent?: (reactTag: ?number, eventType: number) => void;
+showPopupMenu?: (
reactTag: ?number,
items: Array<string>,
error: (error: Object) => void,
success: (event: string, selected?: number) => void,
) => void;
+dismissPopupMenu: () => void;
+dismissPopupMenu?: () => void;

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

export default (TurboModuleRegistry.getEnforcing<Spec>('UIManager'): Spec);
6 changes: 4 additions & 2 deletions packages/react-native/Libraries/ReactNative/PaperUIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {RootTag} from '../Types/RootTagTypes';
import type {UIManagerJSInterface} from '../Types/UIManagerJSInterface';

import NativeUIManager from './NativeUIManager';
import nullthrows from 'nullthrows';

const NativeModules = require('../BatchedBridge/NativeModules');
const defineLazyObjectProperty = require('../Utilities/defineLazyObjectProperty');
Expand Down Expand Up @@ -67,7 +68,7 @@ function getViewManagerConfig(viewManagerName: string): any {
NativeUIManager.lazilyLoadView &&
!triedLoadingConfig.has(viewManagerName)
) {
const result = NativeUIManager.lazilyLoadView(viewManagerName);
const result = nullthrows(NativeUIManager.lazilyLoadView)(viewManagerName);
triedLoadingConfig.add(viewManagerName);
if (result != null && result.viewConfig != null) {
getConstants()[viewManagerName] = result.viewConfig;
Expand Down Expand Up @@ -161,7 +162,8 @@ if (Platform.OS === 'ios') {
} else if (getConstants().ViewManagerNames) {
NativeUIManager.getConstants().ViewManagerNames.forEach(viewManagerName => {
defineLazyObjectProperty(NativeUIManager, viewManagerName, {
get: () => NativeUIManager.getConstantsForViewManager(viewManagerName),
get: () =>
nullthrows(NativeUIManager.getConstantsForViewManager)(viewManagerName),
});
});
}
Expand Down
18 changes: 11 additions & 7 deletions packages/react-native/Libraries/ReactNative/UIManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export interface UIManagerStatic {
*
* UIManager.setLayoutAnimationEnabledExperimental && UIManager.setLayoutAnimationEnabledExperimental(true);
*/
setLayoutAnimationEnabledExperimental(value: boolean): void;
setLayoutAnimationEnabledExperimental?:
| ((value: boolean) => void)
| undefined;

/**
* Used to display an Android PopupMenu. If a menu item is pressed, the success callback will
Expand All @@ -126,12 +128,14 @@ export interface UIManagerStatic {
*
* Note that this works only on Android
*/
showPopupMenu(
node: number,
items: string[],
error: () => void /* currently unused */,
success: (item: string, index: number | undefined) => void,
): void;
showPopupMenu?:
| ((
node: number,
items: string[],
error: () => void /* currently unused */,
success: (item: string, index: number | undefined) => void,
) => void)
| undefined;

getViewManagerConfig: (name: string) => {
Commands: {[key: string]: number};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const pointsDiffer = require('../Utilities/differ/pointsDiffer');
const sizesDiffer = require('../Utilities/differ/sizesDiffer');
const UIManager = require('./UIManager');
const invariant = require('invariant');
const nullthrows = require('nullthrows');

function getNativeComponentAttributes(uiViewClassName: string): any {
const viewConfig = UIManager.getViewManagerConfig(uiViewClassName);
Expand Down Expand Up @@ -105,7 +106,10 @@ function attachDefaultEventTypes(viewConfig: any) {
const constants = UIManager.getConstants();
if (constants.ViewManagerNames || constants.LazyViewManagersEnabled) {
// Lazy view managers enabled.
viewConfig = merge(viewConfig, UIManager.getDefaultEventTypes());
viewConfig = merge(
viewConfig,
nullthrows(UIManager.getDefaultEventTypes)(),
);
} else {
viewConfig.bubblingEventTypes = merge(
viewConfig.bubblingEventTypes,
Expand Down

0 comments on commit bd2a0c6

Please sign in to comment.