Skip to content

Commit e876cc6

Browse files
RSNarafacebook-github-bot
authored andcommitted
Make TurboModules long-lived on Android
Summary: On iOS, calling the `__turboModuleProxy` function with the same name returns the same instance of the TurboModule. Adding this behaviour to Andorid as well. Reviewed By: mdvacca Differential Revision: D16553363 fbshipit-source-id: c95e150d6967604a808cfb49877b7a633e33d729
1 parent a6fffb7 commit e876cc6

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/TurboModuleManager.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,30 @@ void TurboModuleManager::installJSIBindings() {
5656
}
5757
TurboModuleBinding::install(*runtime_, std::make_shared<TurboModuleBinding>(
5858
[this](const std::string &name) -> std::shared_ptr<TurboModule> {
59+
auto turboModuleLookup = turboModuleCache_.find(name);
60+
if (turboModuleLookup != turboModuleCache_.end()) {
61+
return turboModuleLookup->second;
62+
}
63+
5964
auto cxxModule = turboModuleManagerDelegate_->cthis()->getTurboModule(name, jsCallInvoker_);
6065
if (cxxModule) {
66+
turboModuleCache_.insert({name, cxxModule});
6167
return cxxModule;
6268
}
6369

6470
auto legacyCxxModule = getLegacyCxxJavaModule(name);
6571
if (legacyCxxModule) {
66-
return std::make_shared<react::TurboCxxModule>(legacyCxxModule->cthis()->getModule(), jsCallInvoker_);
72+
auto turboModule = std::make_shared<react::TurboCxxModule>(legacyCxxModule->cthis()->getModule(), jsCallInvoker_);
73+
turboModuleCache_.insert({name, turboModule});
74+
return turboModule;
6775
}
6876

6977
auto moduleInstance = getJavaModule(name);
7078

7179
if (moduleInstance) {
72-
return turboModuleManagerDelegate_->cthis()->getTurboModule(name, moduleInstance, jsCallInvoker_);
80+
auto turboModule = turboModuleManagerDelegate_->cthis()->getTurboModule(name, moduleInstance, jsCallInvoker_);
81+
turboModuleCache_.insert({name, turboModule});
82+
return turboModule;
7383
}
7484

7585
return std::shared_ptr<TurboModule>(nullptr);

ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/TurboModuleManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include <memory>
11+
#include <unordered_map>
1112
#include <fb/fbjni.h>
1213
#include <jsi/jsi.h>
1314
#include <ReactCommon/TurboModule.h>
@@ -37,6 +38,14 @@ class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {
3738
std::shared_ptr<JSCallInvoker> jsCallInvoker_;
3839
jni::global_ref<TurboModuleManagerDelegate::javaobject> turboModuleManagerDelegate_;
3940

41+
/**
42+
* TODO(T48018690):
43+
* All modules are currently long-lived.
44+
* We need to come up with a mechanism to allow modules to specify whether
45+
* they want to be long-lived or short-lived.
46+
*/
47+
std::unordered_map<std::string, std::shared_ptr<react::TurboModule>> turboModuleCache_;
48+
4049
jni::global_ref<JTurboModule> getJavaModule(std::string name);
4150
jni::global_ref<CxxModuleWrapper::javaobject> getLegacyCxxJavaModule(std::string name);
4251
void installJSIBindings();

ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ @implementation RCTTurboModuleManager {
4040
__weak id<RCTTurboModuleManagerDelegate> _delegate;
4141
__weak RCTBridge *_bridge;
4242
/**
43-
* TODO(rsnara):
43+
* TODO(T48018690):
4444
* All modules are currently long-lived.
4545
* We need to come up with a mechanism to allow modules to specify whether
4646
* they want to be long-lived or short-lived.

0 commit comments

Comments
 (0)