diff --git a/packages/react-native/Libraries/Core/registerCallableModule.js b/packages/react-native/Libraries/Core/registerCallableModule.js new file mode 100644 index 00000000000000..1d02aab8112978 --- /dev/null +++ b/packages/react-native/Libraries/Core/registerCallableModule.js @@ -0,0 +1,42 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +'use strict'; + +type Module = {...}; +type RegisterCallableModule = ( + name: string, + moduleOrFactory: Module | (void => Module), +) => void; + +const registerCallableModule: RegisterCallableModule = (function () { + if (global.RN$Bridgeless === true) { + return (name, moduleOrFactory) => { + if (moduleOrFactory instanceof Function) { + global.RN$registerCallableModule(name, moduleOrFactory); + return; + } + + global.RN$registerCallableModule(name, () => moduleOrFactory); + }; + } + + const BatchedBridge = require('../BatchedBridge/BatchedBridge'); + return (name, moduleOrFactory) => { + if (moduleOrFactory instanceof Function) { + BatchedBridge.registerLazyCallableModule(name, moduleOrFactory); + return; + } + + BatchedBridge.registerCallableModule(name, moduleOrFactory); + }; +})(); + +export default registerCallableModule; diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 006258d71fc488..e0c5b5e73960d2 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -3929,6 +3929,17 @@ exports[`public API should not change unintentionally Libraries/Core/checkNative exports[`public API should not change unintentionally Libraries/Core/polyfillPromise.js 1`] = `""`; +exports[`public API should not change unintentionally Libraries/Core/registerCallableModule.js 1`] = ` +"type Module = { ... }; +type RegisterCallableModule = ( + name: string, + moduleOrFactory: Module | ((void) => Module) +) => void; +declare const registerCallableModule: RegisterCallableModule; +declare export default typeof registerCallableModule; +" +`; + exports[`public API should not change unintentionally Libraries/Core/setUpAlert.js 1`] = `""`; exports[`public API should not change unintentionally Libraries/Core/setUpBatchedBridge.js 1`] = `""`; @@ -9164,6 +9175,7 @@ declare export default class EventEmitter exports[`public API should not change unintentionally index.js 1`] = ` "export type HostComponent = _HostComponentInternal; declare module.exports: { + get registerCallableModule(): RegisterCallableModule, get AccessibilityInfo(): AccessibilityInfo, get ActivityIndicator(): ActivityIndicator, get Button(): Button, diff --git a/packages/react-native/index.js b/packages/react-native/index.js index 7cd220bc0755a0..270e32924b0d00 100644 --- a/packages/react-native/index.js +++ b/packages/react-native/index.js @@ -43,6 +43,7 @@ import typeof TouchableNativeFeedback from './Libraries/Components/Touchable/Tou import typeof TouchableOpacity from './Libraries/Components/Touchable/TouchableOpacity'; import typeof TouchableWithoutFeedback from './Libraries/Components/Touchable/TouchableWithoutFeedback'; import typeof View from './Libraries/Components/View/View'; +import typeof RegisterCallableModule from './Libraries/Core/registerCallableModule'; import typeof NativeEventEmitter from './Libraries/EventEmitter/NativeEventEmitter'; import typeof RCTDeviceEventEmitter from './Libraries/EventEmitter/RCTDeviceEventEmitter'; import typeof RCTNativeAppEventEmitter from './Libraries/EventEmitter/RCTNativeAppEventEmitter'; @@ -97,6 +98,9 @@ const invariant = require('invariant'); export type HostComponent = _HostComponentInternal; module.exports = { + get registerCallableModule(): RegisterCallableModule { + return require('./Libraries/Core/registerCallableModule').default; + }, // Components get AccessibilityInfo(): AccessibilityInfo { return require('./Libraries/Components/AccessibilityInfo/AccessibilityInfo')