Skip to content

Commit

Permalink
Refactor integration of BridgelessReactPackage into ReactHost (#38010)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #38010

This diff refactors the integration of ReactPackages into ReactHost and ReactHostDelegate.

As part of this diff I'm also modifying ReactHostDelegate to depend on TurboModuleManagerDelegate.Builder instead of TurboModuleManagerDelegateBuilder. This is necessary to be able to create BridgelessReactPackage inside ReactInstance

bypass-github-export-checks

changelog: [internal] internal

Reviewed By: luluwu2032

Differential Revision: D46410795

fbshipit-source-id: 221f0f5ce06b7c57410dc4d351d1a1eae29f2733
  • Loading branch information
mdvacca authored and facebook-github-bot committed Jun 22, 2023
1 parent c018c7b commit 74e6c95
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ package com.facebook.react.bridgeless

import com.facebook.infer.annotation.ThreadSafe
import com.facebook.react.ReactPackage
import com.facebook.react.ReactPackageTurboModuleManagerDelegate
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.fabric.ReactNativeConfig
import com.facebook.react.turbomodule.core.TurboModuleManager
import com.facebook.react.turbomodule.core.TurboModuleManagerDelegate

/**
* [ReactHostDelegate] is an interface that defines parameters required to initialize React Native.
Expand Down Expand Up @@ -49,7 +48,7 @@ interface ReactHostDelegate {
val jSBundleLoader: JSBundleLoader

/** TODO: combine getTurboModuleManagerDelegate inside [ReactPackage] */
fun getTurboModuleManagerDelegate(context: ReactApplicationContext): TurboModuleManagerDelegate
val turboModuleManagerDelegateBuilder: ReactPackageTurboModuleManagerDelegate.Builder

/**
* Callback that can be used by React Native host applications to react to exceptions thrown by
Expand All @@ -68,17 +67,14 @@ interface ReactHostDelegate {
override val jSMainModulePath: String,
override val jSBundleLoader: JSBundleLoader,
override val jSEngineInstance: JSEngineInstance,
override val turboModuleManagerDelegateBuilder:
ReactPackageTurboModuleManagerDelegate.Builder,
override val reactPackages: List<ReactPackage> = emptyList(),
override val bindingsInstaller: BindingsInstaller? = null,
private val turboModuleManagerDelegate:
(context: ReactApplicationContext) -> TurboModuleManagerDelegate,
private val reactNativeConfig: ReactNativeConfig = ReactNativeConfig.DEFAULT_CONFIG,
private val exceptionHandler: (error: Exception) -> Unit = {}
) : ReactHostDelegate {

override fun getTurboModuleManagerDelegate(context: ReactApplicationContext) =
turboModuleManagerDelegate(context)

override fun getReactNativeConfig(turboModuleManager: TurboModuleManager) = reactNativeConfig

override fun handleInstanceException(error: Exception) = exceptionHandler(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.infer.annotation.ThreadSafe;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.BridgelessReactPackage;
import com.facebook.react.ReactPackage;
import com.facebook.react.ViewManagerOnDemandReactPackage;
import com.facebook.react.bridge.JSBundleLoader;
Expand Down Expand Up @@ -78,6 +79,7 @@ final class ReactInstance {

private final ReactHostDelegate mDelegate;
private final BridgelessReactContext mBridgelessReactContext;
private final List<ReactPackage> mReactPackages;

private final ReactQueueConfiguration mQueueConfiguration;
private final TurboModuleManager mTurboModuleManager;
Expand Down Expand Up @@ -186,8 +188,20 @@ public void onHostDestroy() {
// Set up TurboModules
Systrace.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ReactInstance.initialize#initTurboModules");

mReactPackages = mDelegate.getReactPackages();
mReactPackages.add(
new BridgelessReactPackage(
bridgelessReactContext.getDevSupportManager(),
bridgelessReactContext.getDefaultHardwareBackBtnHandler()));

TurboModuleManagerDelegate turboModuleManagerDelegate =
mDelegate.getTurboModuleManagerDelegate(mBridgelessReactContext);
mDelegate
.getTurboModuleManagerDelegateBuilder()
.setPackages(mReactPackages)
.setReactApplicationContext(mBridgelessReactContext)
.build();

mTurboModuleManager =
new TurboModuleManager(
// Use unbuffered RuntimeExecutor to install binding
Expand Down Expand Up @@ -442,7 +456,7 @@ public void registerSegment(int segmentId, String path) {

private @Nullable ViewManager createViewManager(String viewManagerName) {
if (mDelegate != null) {
List<ReactPackage> packages = mDelegate.getReactPackages();
List<ReactPackage> packages = mReactPackages;
if (packages != null) {
synchronized (packages) {
for (ReactPackage reactPackage : packages) {
Expand All @@ -465,7 +479,7 @@ public void registerSegment(int segmentId, String path) {
private @NonNull Collection<String> getViewManagerNames() {
Set<String> uniqueNames = new HashSet<>();
if (mDelegate != null) {
List<ReactPackage> packages = mDelegate.getReactPackages();
List<ReactPackage> packages = mReactPackages;
if (packages != null) {
synchronized (packages) {
for (ReactPackage reactPackage : packages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ package com.facebook.react.defaults

import com.facebook.jni.annotations.DoNotStrip
import com.facebook.react.ReactPackage
import com.facebook.react.ReactPackageTurboModuleManagerDelegate
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridgeless.BindingsInstaller
import com.facebook.react.bridgeless.JSEngineInstance
import com.facebook.react.bridgeless.ReactHostDelegate
import com.facebook.react.bridgeless.hermes.HermesInstance
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.fabric.ReactNativeConfig
import com.facebook.react.turbomodule.core.TurboModuleManager
import com.facebook.react.turbomodule.core.TurboModuleManagerDelegate

/**
* A utility class that allows you to simplify the initialization of React Native by setting up a
Expand Down Expand Up @@ -46,15 +45,11 @@ class DefaultReactHostDelegate(
override val reactPackages: List<ReactPackage> = emptyList(),
override val jSEngineInstance: JSEngineInstance = HermesInstance(),
override val bindingsInstaller: BindingsInstaller = DefaultBindingsInstaller(),
private val turboModuleManagerDelegate:
(context: ReactApplicationContext) -> TurboModuleManagerDelegate,
private val reactNativeConfig: ReactNativeConfig = ReactNativeConfig.DEFAULT_CONFIG,
private val exceptionHandler: (Exception) -> Unit = {}
private val exceptionHandler: (Exception) -> Unit = {},
override val turboModuleManagerDelegateBuilder: ReactPackageTurboModuleManagerDelegate.Builder
) : ReactHostDelegate {

override fun getTurboModuleManagerDelegate(context: ReactApplicationContext) =
turboModuleManagerDelegate(context)

override fun getReactNativeConfig(turboModuleManager: TurboModuleManager) = reactNativeConfig

override fun handleInstanceException(error: Exception) = exceptionHandler(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

package com.facebook.react.bridgeless

import com.facebook.react.ReactPackageTurboModuleManagerDelegate
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridgeless.hermes.HermesInstance
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.defaults.DefaultReactHostDelegate
import com.facebook.react.turbomodule.core.TurboModuleManagerDelegate
import com.facebook.testutils.shadows.ShadowSoLoader
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
Expand All @@ -32,16 +32,16 @@ class ReactHostDelegateTest {
@Test
fun testDefaultReactHostDelegateCreation() {
val jsBundleLoader: JSBundleLoader = Mockito.mock(JSBundleLoader::class.java)
val turboModuleManagerDelegateMock: TurboModuleManagerDelegate =
Mockito.mock(TurboModuleManagerDelegate::class.java)
val turboModuleManagerDelegateBuilderMock: ReactPackageTurboModuleManagerDelegate.Builder =
Mockito.mock(ReactPackageTurboModuleManagerDelegate.Builder::class.java)
val hermesInstance: JSEngineInstance = Mockito.mock(HermesInstance::class.java)
val jsMainModulePathMocked = "mockedJSMainModulePath"
val delegate =
DefaultReactHostDelegate(
jSMainModulePath = jsMainModulePathMocked,
jSBundleLoader = jsBundleLoader,
jSEngineInstance = hermesInstance,
turboModuleManagerDelegate = { turboModuleManagerDelegateMock })
turboModuleManagerDelegateBuilder = turboModuleManagerDelegateBuilderMock)

assertThat(delegate.jSMainModulePath).isEqualTo(jsMainModulePathMocked)
}
Expand Down

0 comments on commit 74e6c95

Please sign in to comment.