diff --git a/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/TurboModuleManager.cpp b/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/TurboModuleManager.cpp index db8c26ad075ab1..73cfd4f994b87d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/TurboModuleManager.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/TurboModuleManager.cpp @@ -65,65 +65,62 @@ void TurboModuleManager::installJSIBindings() { TurboModuleBinding::install( *runtime_, - std::make_shared( - [turboModuleCache_ = - std::weak_ptr(turboModuleCache_), - jsCallInvoker_ = std::weak_ptr(jsCallInvoker_), - nativeCallInvoker_ = std::weak_ptr(nativeCallInvoker_), - delegate_ = jni::make_weak(delegate_), - javaPart_ = jni::make_weak(javaPart_)]( - const std::string &name) -> std::shared_ptr { - auto turboModuleCache = turboModuleCache_.lock(); - auto jsCallInvoker = jsCallInvoker_.lock(); - auto nativeCallInvoker = nativeCallInvoker_.lock(); - auto delegate = delegate_.lockLocal(); - auto javaPart = javaPart_.lockLocal(); - - if (!turboModuleCache || !jsCallInvoker || !nativeCallInvoker || - !delegate || !javaPart) { - return nullptr; - } - - auto turboModuleLookup = turboModuleCache->find(name); - if (turboModuleLookup != turboModuleCache->end()) { - return turboModuleLookup->second; - } - - auto cxxModule = - delegate->cthis()->getTurboModule(name, jsCallInvoker); - if (cxxModule) { - turboModuleCache->insert({name, cxxModule}); - return cxxModule; - } - - static auto getLegacyCxxModule = - delegate->getClass() - ->getMethod( - const std::string &)>("getLegacyCxxModule"); - auto legacyCxxModule = getLegacyCxxModule(delegate.get(), name); - - if (legacyCxxModule) { - auto turboModule = std::make_shared( - legacyCxxModule->cthis()->getModule(), jsCallInvoker); - turboModuleCache->insert({name, turboModule}); - return turboModule; - } - - static auto getJavaModule = - javaPart->getClass() - ->getMethod( - const std::string &)>("getJavaModule"); - auto moduleInstance = getJavaModule(javaPart.get(), name); - - if (moduleInstance) { - auto turboModule = delegate->cthis()->getTurboModule( - name, moduleInstance, jsCallInvoker, nativeCallInvoker); - turboModuleCache->insert({name, turboModule}); - return turboModule; - } - - return nullptr; - })); + [turboModuleCache_ = std::weak_ptr(turboModuleCache_), + jsCallInvoker_ = std::weak_ptr(jsCallInvoker_), + nativeCallInvoker_ = std::weak_ptr(nativeCallInvoker_), + delegate_ = jni::make_weak(delegate_), + javaPart_ = jni::make_weak(javaPart_)]( + const std::string &name) -> std::shared_ptr { + auto turboModuleCache = turboModuleCache_.lock(); + auto jsCallInvoker = jsCallInvoker_.lock(); + auto nativeCallInvoker = nativeCallInvoker_.lock(); + auto delegate = delegate_.lockLocal(); + auto javaPart = javaPart_.lockLocal(); + + if (!turboModuleCache || !jsCallInvoker || !nativeCallInvoker || + !delegate || !javaPart) { + return nullptr; + } + + auto turboModuleLookup = turboModuleCache->find(name); + if (turboModuleLookup != turboModuleCache->end()) { + return turboModuleLookup->second; + } + + auto cxxModule = delegate->cthis()->getTurboModule(name, jsCallInvoker); + if (cxxModule) { + turboModuleCache->insert({name, cxxModule}); + return cxxModule; + } + + static auto getLegacyCxxModule = + delegate->getClass() + ->getMethod( + const std::string &)>("getLegacyCxxModule"); + auto legacyCxxModule = getLegacyCxxModule(delegate.get(), name); + + if (legacyCxxModule) { + auto turboModule = std::make_shared( + legacyCxxModule->cthis()->getModule(), jsCallInvoker); + turboModuleCache->insert({name, turboModule}); + return turboModule; + } + + static auto getJavaModule = + javaPart->getClass() + ->getMethod(const std::string &)>( + "getJavaModule"); + auto moduleInstance = getJavaModule(javaPart.get(), name); + + if (moduleInstance) { + auto turboModule = delegate->cthis()->getTurboModule( + name, moduleInstance, jsCallInvoker, nativeCallInvoker); + turboModuleCache->insert({name, turboModule}); + return turboModule; + } + + return nullptr; + }); } } // namespace react diff --git a/ReactCommon/turbomodule/core/TurboModuleBinding.cpp b/ReactCommon/turbomodule/core/TurboModuleBinding.cpp index 549e0df060797c..cfb9ecd3e898b1 100644 --- a/ReactCommon/turbomodule/core/TurboModuleBinding.cpp +++ b/ReactCommon/turbomodule/core/TurboModuleBinding.cpp @@ -21,12 +21,12 @@ namespace react { * Public API to install the TurboModule system. */ TurboModuleBinding::TurboModuleBinding( - const TurboModuleProviderFunctionType &moduleProvider) - : moduleProvider_(moduleProvider) {} + const TurboModuleProviderFunctionType &&moduleProvider) + : moduleProvider_(std::move(moduleProvider)) {} void TurboModuleBinding::install( jsi::Runtime &runtime, - std::shared_ptr binding) { + const TurboModuleProviderFunctionType &&moduleProvider) { runtime.global().setProperty( runtime, "__turboModuleProxy", @@ -34,7 +34,8 @@ void TurboModuleBinding::install( runtime, jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"), 1, - [binding]( + [binding = + std::make_shared(std::move(moduleProvider))]( jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, @@ -43,7 +44,7 @@ void TurboModuleBinding::install( })); } -void TurboModuleBinding::invalidate() const { +TurboModuleBinding::~TurboModuleBinding() { LongLivedObjectCollection::get().clear(); } diff --git a/ReactCommon/turbomodule/core/TurboModuleBinding.h b/ReactCommon/turbomodule/core/TurboModuleBinding.h index c98113da2adae1..96de89bed470e8 100644 --- a/ReactCommon/turbomodule/core/TurboModuleBinding.h +++ b/ReactCommon/turbomodule/core/TurboModuleBinding.h @@ -28,15 +28,10 @@ class TurboModuleBinding { */ static void install( jsi::Runtime &runtime, - std::shared_ptr binding); + const TurboModuleProviderFunctionType &&moduleProvider); - TurboModuleBinding(const TurboModuleProviderFunctionType &moduleProvider); - - /* - * Invalidates the binding. - * Can be called in any thread. - */ - void invalidate() const; + TurboModuleBinding(const TurboModuleProviderFunctionType &&moduleProvider); + virtual ~TurboModuleBinding(); /** * Get an TurboModule instance for the given module name. diff --git a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.h b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.h index 7fdb9fd017b6af..2adeb6bbbc3dc1 100644 --- a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.h +++ b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.h @@ -44,8 +44,6 @@ - (void)installJSBindingWithRuntime:(facebook::jsi::Runtime *)runtime; -- (std::shared_ptr)getModule:(const std::string &)name; - - (void)invalidate; @end diff --git a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm index 004aa7113cf9eb..c5a46e004d8c3a 100644 --- a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm +++ b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm @@ -36,7 +36,6 @@ static Class getFallbackClassFromName(const char *name) @implementation RCTTurboModuleManager { jsi::Runtime *_runtime; std::shared_ptr _jsInvoker; - std::shared_ptr _binding; __weak id _delegate; __weak RCTBridge *_bridge; /** @@ -83,38 +82,6 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge selector:@selector(bridgeDidInvalidateModules:) name:RCTBridgeDidInvalidateModulesNotification object:_bridge.parentBridge]; - - __weak __typeof(self) weakSelf = self; - - auto moduleProvider = [weakSelf](const std::string &name) -> std::shared_ptr { - if (!weakSelf) { - return nullptr; - } - - __strong __typeof(self) strongSelf = weakSelf; - - auto moduleName = name.c_str(); - auto moduleWasNotInitialized = ![strongSelf moduleIsInitialized:moduleName]; - if (moduleWasNotInitialized) { - [strongSelf->_bridge.performanceLogger markStartForTag:RCTPLTurboModuleSetup]; - } - - /** - * By default, all TurboModules are long-lived. - * Additionally, if a TurboModule with the name `name` isn't found, then we - * trigger an assertion failure. - */ - auto turboModule = [strongSelf provideTurboModule:moduleName]; - - if (moduleWasNotInitialized && [strongSelf moduleIsInitialized:moduleName]) { - [strongSelf->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup]; - [strongSelf notifyAboutTurboModuleSetup:moduleName]; - } - - return turboModule; - }; - - _binding = std::make_shared(moduleProvider); } return self; } @@ -376,12 +343,36 @@ - (void)installJSBindingWithRuntime:(jsi::Runtime *)runtime return; } - react::TurboModuleBinding::install(*_runtime, _binding); -} + __weak __typeof(self) weakSelf = self; -- (std::shared_ptr)getModule:(const std::string &)name -{ - return _binding->getModule(name); + react::TurboModuleBinding::install( + *_runtime, [weakSelf](const std::string &name) -> std::shared_ptr { + if (!weakSelf) { + return nullptr; + } + + __strong __typeof(self) strongSelf = weakSelf; + + auto moduleName = name.c_str(); + auto moduleWasNotInitialized = ![strongSelf moduleIsInitialized:moduleName]; + if (moduleWasNotInitialized) { + [strongSelf->_bridge.performanceLogger markStartForTag:RCTPLTurboModuleSetup]; + } + + /** + * By default, all TurboModules are long-lived. + * Additionally, if a TurboModule with the name `name` isn't found, then we + * trigger an assertion failure. + */ + auto turboModule = [strongSelf provideTurboModule:moduleName]; + + if (moduleWasNotInitialized && [strongSelf moduleIsInitialized:moduleName]) { + [strongSelf->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup]; + [strongSelf notifyAboutTurboModuleSetup:moduleName]; + } + + return turboModule; + }); } #pragma mark RCTTurboModuleLookupDelegate @@ -465,8 +456,6 @@ - (void)bridgeDidInvalidateModules:(NSNotification *)notification } _turboModuleCache.clear(); - - _binding->invalidate(); } - (void)invalidate @@ -491,8 +480,6 @@ - (void)invalidate } _turboModuleCache.clear(); - - _binding->invalidate(); } @end