-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CompositeReactPackageTurboModuleManagerDelegate
Summary: This diff adds a CompositeReactPackageTurboModuleManagerDelegate which can be used to combine 1 or more TurboModuleManagerDelegates into a single one. This is useful when both the app and one or multiple of its libraries provides a TurboModuleManagerDelegate Changelog: [Internal] [Changed] - Add CompositeReactPackageTurboModuleManagerDelegate Reviewed By: cortinico Differential Revision: D36057036 fbshipit-source-id: bf131fa7315941b3353c0522e4d77d909b82914b
- Loading branch information
1 parent
e7d9e4d
commit 3f09b48
Showing
7 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...oid/src/main/java/com/facebook/react/CompositeReactPackageTurboModuleManagerDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.facebook.react; | ||
|
||
import androidx.annotation.NonNull; | ||
import com.facebook.jni.HybridData; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.turbomodule.core.TurboModuleManagerDelegate; | ||
import com.facebook.soloader.SoLoader; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CompositeReactPackageTurboModuleManagerDelegate | ||
extends ReactPackageTurboModuleManagerDelegate { | ||
|
||
private static volatile boolean sIsSoLibraryLoaded; | ||
|
||
protected native HybridData initHybrid(); | ||
|
||
private final List<TurboModuleManagerDelegate> mDelegates; | ||
|
||
private CompositeReactPackageTurboModuleManagerDelegate( | ||
ReactApplicationContext context, | ||
List<ReactPackage> packages, | ||
List<TurboModuleManagerDelegate> delegates) { | ||
super(context, packages); | ||
mDelegates = delegates; | ||
for (TurboModuleManagerDelegate delegate : delegates) { | ||
addTurboModuleManagerDelegate(delegate); | ||
} | ||
} | ||
|
||
private native void addTurboModuleManagerDelegate(TurboModuleManagerDelegate delegates); | ||
|
||
public static class Builder extends ReactPackageTurboModuleManagerDelegate.Builder { | ||
private final List<ReactPackageTurboModuleManagerDelegate.Builder> mDelegatesBuilder; | ||
|
||
public Builder(@NonNull List<ReactPackageTurboModuleManagerDelegate.Builder> delegatesBuilder) { | ||
mDelegatesBuilder = delegatesBuilder; | ||
} | ||
|
||
protected ReactPackageTurboModuleManagerDelegate build( | ||
ReactApplicationContext context, List<ReactPackage> packages) { | ||
List<TurboModuleManagerDelegate> delegates = new ArrayList<>(); | ||
for (ReactPackageTurboModuleManagerDelegate.Builder delegatesBuilder : mDelegatesBuilder) { | ||
delegates.add(delegatesBuilder.build(context, Collections.emptyList())); | ||
} | ||
return new CompositeReactPackageTurboModuleManagerDelegate(context, packages, delegates); | ||
} | ||
} | ||
|
||
protected synchronized void maybeLoadOtherSoLibraries() { | ||
// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context. | ||
if (!sIsSoLibraryLoaded) { | ||
SoLoader.loadLibrary("turbomodulejsijni"); | ||
sIsSoLibraryLoaded = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...m/facebook/react/turbomodule/core/jni/ReactCommon/CompositeTurboModuleManagerDelegate.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 "CompositeTurboModuleManagerDelegate.h" | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
jni::local_ref<CompositeTurboModuleManagerDelegate::jhybriddata> | ||
CompositeTurboModuleManagerDelegate::initHybrid(jni::alias_ref<jhybridobject>) { | ||
return makeCxxInstance(); | ||
} | ||
|
||
void CompositeTurboModuleManagerDelegate::registerNatives() { | ||
registerHybrid({ | ||
makeNativeMethod( | ||
"initHybrid", CompositeTurboModuleManagerDelegate::initHybrid), | ||
makeNativeMethod( | ||
"addTurboModuleManagerDelegate", | ||
CompositeTurboModuleManagerDelegate::addTurboModuleManagerDelegate), | ||
}); | ||
} | ||
|
||
std::shared_ptr<TurboModule> | ||
CompositeTurboModuleManagerDelegate::getTurboModule( | ||
const std::string moduleName, | ||
const std::shared_ptr<CallInvoker> jsInvoker) { | ||
for (auto delegate : mDelegates_) { | ||
if (auto turboModule = delegate->getTurboModule(moduleName, jsInvoker)) { | ||
return turboModule; | ||
} | ||
} | ||
return nullptr; | ||
} | ||
|
||
std::shared_ptr<TurboModule> | ||
CompositeTurboModuleManagerDelegate::getTurboModule( | ||
const std::string moduleName, | ||
const JavaTurboModule::InitParams ¶ms) { | ||
for (auto delegate : mDelegates_) { | ||
if (auto turboModule = delegate->getTurboModule(moduleName, params)) { | ||
return turboModule; | ||
} | ||
} | ||
return nullptr; | ||
} | ||
|
||
void CompositeTurboModuleManagerDelegate::addTurboModuleManagerDelegate( | ||
jni::alias_ref<TurboModuleManagerDelegate::javaobject> | ||
turboModuleManagerDelegate) { | ||
mDelegates_.insert(turboModuleManagerDelegate->cthis()); | ||
} | ||
|
||
} // namespace react | ||
} // namespace facebook |
49 changes: 49 additions & 0 deletions
49
...com/facebook/react/turbomodule/core/jni/ReactCommon/CompositeTurboModuleManagerDelegate.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 <ReactCommon/TurboModuleManagerDelegate.h> | ||
#include <fbjni/fbjni.h> | ||
#include <memory> | ||
#include <string> | ||
#include <unordered_set> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class CompositeTurboModuleManagerDelegate | ||
: public jni::HybridClass< | ||
CompositeTurboModuleManagerDelegate, | ||
TurboModuleManagerDelegate> { | ||
public: | ||
static auto constexpr kJavaDescriptor = | ||
"Lcom/facebook/react/CompositeReactPackageTurboModuleManagerDelegate;"; | ||
|
||
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>); | ||
|
||
static void registerNatives(); | ||
|
||
std::shared_ptr<TurboModule> getTurboModule( | ||
const std::string moduleName, | ||
const std::shared_ptr<CallInvoker> jsInvoker) override; | ||
std::shared_ptr<TurboModule> getTurboModule( | ||
const std::string moduleName, | ||
const JavaTurboModule::InitParams ¶ms) override; | ||
|
||
private: | ||
friend HybridBase; | ||
using HybridBase::HybridBase; | ||
std::unordered_set<TurboModuleManagerDelegate *> mDelegates_; | ||
|
||
void addTurboModuleManagerDelegate( | ||
jni::alias_ref<TurboModuleManagerDelegate::javaobject> | ||
turboModuleManagerDelegate); | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters