diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp index 5d792fb2c669a7..20a47db010e274 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -168,6 +169,14 @@ std::shared_ptr TurboModuleManager::getTurboModule( return cxxModule; } + auto& cxxTurboModuleMapProvider = globalExportedCxxTurboModuleMap(); + auto it = cxxTurboModuleMapProvider.find(name); + if (it != cxxTurboModuleMapProvider.end()) { + auto turboModule = it->second(jsCallInvoker_); + turboModuleCache_.insert({name, turboModule}); + return turboModule; + } + static auto getTurboJavaModule = javaPart->getClass() ->getMethod(const std::string&)>( diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.cpp b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.cpp new file mode 100644 index 00000000000000..4ddafa742addc9 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.cpp @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#include "CxxTurboModuleUtils.h" + +namespace facebook::react { + +std::unordered_map< + std::string, + std::function< + std::shared_ptr(std::shared_ptr jsInvoker)>>& +globalExportedCxxTurboModuleMap() { + static std::unordered_map< + std::string, + std::function( + std::shared_ptr jsInvoker)>> + map; + return map; +} + +void registerCxxModuleToGlobalModuleMap( + std::string name, + std::function( + std::shared_ptr jsInvoker)> moduleProviderFunc) { + globalExportedCxxTurboModuleMap()[name] = moduleProviderFunc; +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.h b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.h new file mode 100644 index 00000000000000..7a74f04f18e44d --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.h @@ -0,0 +1,35 @@ +/* + * 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. + */ + +#pragma once + +#include +#include + +#include +#include + +namespace facebook::react { + +std::unordered_map< + std::string, + std::function< + std::shared_ptr(std::shared_ptr jsInvoker)>>& +globalExportedCxxTurboModuleMap(); + +/** + * Registers the given C++ TurboModule initializer function + * in the global module map. + * This needs to be called before the TurboModule is requested from JS, + * for example in a `+ load`, your AppDelegate's start, or from Java init. + */ +void registerCxxModuleToGlobalModuleMap( + std::string name, + std::function( + std::shared_ptr jsInvoker)> moduleProviderFunc); + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm index 9fb81c3f91414c..d113d52eedd31e 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm @@ -29,6 +29,7 @@ #import #import #import +#import #import #import #import @@ -328,6 +329,14 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy TurboModulePerfLogger::moduleCreateFail(moduleName, moduleId); } + auto &cxxTurboModuleMapProvider = globalExportedCxxTurboModuleMap(); + auto it = cxxTurboModuleMapProvider.find(moduleName); + if (it != cxxTurboModuleMapProvider.end()) { + auto turboModule = it->second(_jsInvoker); + _turboModuleCache.insert({moduleName, turboModule}); + return turboModule; + } + /** * Step 2: Look for platform-specific modules. */