From 55e4543eea1a2f7a42ed6997c9a692b4fc61550a Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Mon, 25 Dec 2023 19:23:14 -0800 Subject: [PATCH] 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 --- .../ReactNative/BridgelessUIManager.js | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js b/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js index b7351924327..f001f31c15d 100644 --- a/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js +++ b/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js @@ -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]; }); }