mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
BridgelessUIManager: Refactor how constants are retrieved and cached (#42000)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42000 I think this makes BridgelessUIManager easier to read: if the getUIManagerConstants method exists, get the cached constants. Also, this unifies the nomenclature between PaperUIManager and BridgleessUIManager. That way, it's easy to compare/constrast the two files. Changelog: [Internal] Reviewed By: dmytrorykun, luluwu2032 Differential Revision: D52002910 fbshipit-source-id: 01bfbd5fedbe3f995b4a1f68309714d84027133b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
09ab59ab25
commit
55e4543eea
@@ -14,8 +14,7 @@ import type {RootTag} from '../Types/RootTagTypes';
|
||||
import type {UIManagerJSInterface} from '../Types/UIManagerJSInterface';
|
||||
|
||||
import {unstable_hasComponent} from '../NativeComponent/NativeComponentRegistryUnstable';
|
||||
|
||||
let cachedConstants = null;
|
||||
import nullthrows from 'nullthrows';
|
||||
|
||||
function raiseSoftError(methodName: string, details?: string): void {
|
||||
console.error(
|
||||
@@ -24,21 +23,25 @@ function raiseSoftError(methodName: string, details?: string): void {
|
||||
);
|
||||
}
|
||||
|
||||
function nativeViewConfigsInBridgelessModeEnabled(): boolean {
|
||||
return global.RN$LegacyInterop_UIManager_getConstants !== undefined;
|
||||
}
|
||||
const getUIManagerConstants: ?() => {[viewManagerName: string]: Object} =
|
||||
global.RN$LegacyInterop_UIManager_getConstants;
|
||||
|
||||
function getCachedConstants(): Object {
|
||||
if (!cachedConstants) {
|
||||
cachedConstants = global.RN$LegacyInterop_UIManager_getConstants();
|
||||
}
|
||||
return cachedConstants;
|
||||
}
|
||||
const getUIManagerConstantsCache = (function () {
|
||||
let wasCalledOnce = false;
|
||||
let result = {};
|
||||
return () => {
|
||||
if (!wasCalledOnce) {
|
||||
result = nullthrows(getUIManagerConstants)();
|
||||
wasCalledOnce = true;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
|
||||
const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
|
||||
getViewManagerConfig: (viewManagerName: string): mixed => {
|
||||
if (nativeViewConfigsInBridgelessModeEnabled()) {
|
||||
return getCachedConstants()[viewManagerName];
|
||||
if (getUIManagerConstants) {
|
||||
return getUIManagerConstantsCache()[viewManagerName];
|
||||
} else {
|
||||
raiseSoftError(
|
||||
'getViewManagerConfig',
|
||||
@@ -51,8 +54,8 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
|
||||
return unstable_hasComponent(viewManagerName);
|
||||
},
|
||||
getConstants: (): Object => {
|
||||
if (nativeViewConfigsInBridgelessModeEnabled()) {
|
||||
return getCachedConstants();
|
||||
if (getUIManagerConstants) {
|
||||
return getUIManagerConstantsCache();
|
||||
} else {
|
||||
raiseSoftError('getConstants');
|
||||
return null;
|
||||
@@ -177,9 +180,9 @@ const UIManagerJS: UIManagerJSInterface & {[string]: any} = {
|
||||
dismissPopupMenu: (): void => raiseSoftError('dismissPopupMenu'),
|
||||
};
|
||||
|
||||
if (nativeViewConfigsInBridgelessModeEnabled()) {
|
||||
Object.keys(getCachedConstants()).forEach(viewConfigName => {
|
||||
UIManagerJS[viewConfigName] = getCachedConstants()[viewConfigName];
|
||||
if (getUIManagerConstants) {
|
||||
Object.keys(getUIManagerConstantsCache()).forEach(viewConfigName => {
|
||||
UIManagerJS[viewConfigName] = getUIManagerConstantsCache()[viewConfigName];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user