mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Switch over to JavaTurboModule::InitParams
Summary:
## Problem
Every time we want to add, remove, or change the data passed to JavaTurboModule's constructor, we have to modify the C++ TurboModule codegen. (The same is true of `ObjCTurboModule`).
**Why was this necessary?**
- `JavaTurboModule` is effectively an abstract class whose constructor is always invoked by code-generated C++ classes. These C++ code-generated class constructors accept an argument list, and manually foward each and every item in that list to `JavaTurboModule::JavaTurboModule`.
## The fix
In this diff, I introduce a struct `JavaTurboModule::InitParams`, to represent a bag of arguments:
```
class JSI_EXPORT JavaTurboModule : public TurboModule {
public:
struct InitParams {
std::string moduleName;
jni::alias_ref<JTurboModule> instance;
std::shared_ptr<CallInvoker> jsInvoker;
std::shared_ptr<CallInvoker> nativeInvoker;
};
```
All `JavaTurboModules` will be created with an instance of this `InitParams` struct, instead of a list of arguments. Our code-generated C++ `jsi::HostObject` sublcasses will simply accept `InitParams` in their constructor, and forward it to `JavaTurboModule`'s constructor. This way, the codegen remains oblivious to what arguments JavaTurboModule requires.
## Okay, but why do we need this change now?
In the future, I plan to modify the constructor for `JavaTurboModule` to accept a performance logger, and a `RuntimeExecutor`. Similar modifications are planned for ObjC. For this reason, to avoid these four codemods, and any potential other codemods that occur because we're making modifications to `JavaTurboModule` or `ObjCTurboModule`, I'm launching this codemod, and the codemods in this stack.
## Misc Fix
- Previously, we were generating the TurboModule name from the Spec filename. This is incorrect because that name represents the spec name. Now, the name will be forwarded from TurboModuleManager in the `JavaTurboModule::InitParams` struct.
## Alternative implementations
I initially considered using `ContextContainer`, but decided against it because:
1. There are no type-safety guarantees.
2. I think it's a bit overkill for this scenario. We just need an opaque bag of data, and for our purposes a simple struct does the job fine.
## Commands run
Reviewed By: fkgozali
Differential Revision: D21035208
fbshipit-source-id: 9542cafea192081bc34d337ab3a7a783083eb06c
This commit is contained in:
committed by
Facebook GitHub Bot
parent
33b3a1a145
commit
25ed045e36
+6
-2
@@ -112,8 +112,12 @@ void TurboModuleManager::installJSIBindings() {
|
||||
auto moduleInstance = getJavaModule(javaPart.get(), name);
|
||||
|
||||
if (moduleInstance) {
|
||||
auto turboModule = delegate->cthis()->getTurboModule(
|
||||
name, moduleInstance, jsCallInvoker, nativeCallInvoker);
|
||||
JavaTurboModule::InitParams params = {.moduleName = name,
|
||||
.instance = moduleInstance,
|
||||
.jsInvoker = jsCallInvoker,
|
||||
.nativeInvoker = nativeCallInvoker};
|
||||
|
||||
auto turboModule = delegate->cthis()->getTurboModule(name, params);
|
||||
turboModuleCache->insert({name, turboModule});
|
||||
return turboModule;
|
||||
}
|
||||
|
||||
+4
-6
@@ -23,13 +23,11 @@ class TurboModuleManagerDelegate
|
||||
"Lcom/facebook/react/turbomodule/core/TurboModuleManagerDelegate;";
|
||||
|
||||
virtual std::shared_ptr<TurboModule> getTurboModule(
|
||||
std::string name,
|
||||
jni::alias_ref<JTurboModule> turboModule,
|
||||
std::shared_ptr<CallInvoker> jsInvoker,
|
||||
std::shared_ptr<CallInvoker> nativeInvoker) = 0;
|
||||
const std::string name,
|
||||
const JavaTurboModule::InitParams ¶ms) = 0;
|
||||
virtual std::shared_ptr<TurboModule> getTurboModule(
|
||||
std::string name,
|
||||
std::shared_ptr<CallInvoker> jsInvoker) = 0;
|
||||
const std::string name,
|
||||
const std::shared_ptr<CallInvoker> jsInvoker) = 0;
|
||||
|
||||
private:
|
||||
friend HybridBase;
|
||||
|
||||
Reference in New Issue
Block a user