Skip to content

Commit d45d15f

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Introduce DefaultReactHostDelegate (#37565)
Summary: Pull Request resolved: #37565 Introduce DefaultReactHostDelegate class that will be used to simplify creation of ReactHostDelegate in React Native Android changelog: [internal] internal Reviewed By: cortinico, philIip Differential Revision: D45665528 fbshipit-source-id: 154f4a2f0916c6f1cb846bc052a86dfc72f04dd7
1 parent c99d6f6 commit d45d15f

File tree

6 files changed

+71
-22
lines changed

6 files changed

+71
-22
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BindingsInstaller.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.facebook.proguard.annotations.DoNotStripAny
1313
import com.facebook.soloader.SoLoader
1414

1515
@DoNotStripAny
16-
abstract class BindingsInstaller(@field:DoNotStrip private val mHybridData: HybridData) {
16+
abstract class BindingsInstaller(@field:DoNotStrip private val mHybridData: HybridData?) {
1717
companion object {
1818
init {
1919
SoLoader.loadLibrary("rninstance")

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHostDelegate.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface ReactHostDelegate {
3232

3333
fun getTurboModuleManagerDelegate(context: ReactApplicationContext): TurboModuleManagerDelegate
3434

35-
fun handleInstanceException(e: Exception)
35+
fun handleInstanceException(error: Exception)
3636

3737
fun getReactNativeConfig(context: ReactContext): ReactNativeConfig
3838

@@ -46,14 +46,14 @@ interface ReactHostDelegate {
4646
private val turboModuleManagerDelegate:
4747
(context: ReactApplicationContext) -> TurboModuleManagerDelegate,
4848
private val reactNativeConfig: ReactNativeConfig = ReactNativeConfig.DEFAULT_CONFIG,
49-
private val exceptionHandler: (Exception) -> Unit = {}
49+
private val exceptionHandler: (error: Exception) -> Unit = {}
5050
) : ReactHostDelegate {
5151

5252
override fun getTurboModuleManagerDelegate(context: ReactApplicationContext) =
5353
turboModuleManagerDelegate(context)
5454

5555
override fun getReactNativeConfig(context: ReactContext) = reactNativeConfig
5656

57-
override fun handleInstanceException(e: Exception) = exceptionHandler(e)
57+
override fun handleInstanceException(error: Exception) = exceptionHandler(error)
5858
}
5959
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/hermes/HermesInstance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.facebook.jni.annotations.DoNotStrip
1212
import com.facebook.react.bridgeless.JSEngineInstance
1313
import com.facebook.soloader.SoLoader
1414

15-
class HermesInstance() : JSEngineInstance(initHybrid()!!) {
15+
class HermesInstance : JSEngineInstance(initHybrid()!!) {
1616

1717
companion object {
1818
@JvmStatic @DoNotStrip protected external fun initHybrid(): HybridData?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.defaults
9+
10+
import com.facebook.react.bridgeless.BindingsInstaller
11+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
12+
13+
@UnstableReactNativeAPI class DefaultBindingsInstaller : BindingsInstaller(null) {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.defaults
9+
10+
import com.facebook.react.ReactPackage
11+
import com.facebook.react.bridge.JSBundleLoader
12+
import com.facebook.react.bridge.ReactApplicationContext
13+
import com.facebook.react.bridge.ReactContext
14+
import com.facebook.react.bridgeless.BindingsInstaller
15+
import com.facebook.react.bridgeless.JSEngineInstance
16+
import com.facebook.react.bridgeless.ReactHostDelegate
17+
import com.facebook.react.bridgeless.hermes.HermesInstance
18+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
19+
import com.facebook.react.fabric.ReactNativeConfig
20+
import com.facebook.react.turbomodule.core.TurboModuleManagerDelegate
21+
22+
@UnstableReactNativeAPI
23+
class DefaultReactHostDelegate(
24+
override val jSMainModulePath: String,
25+
override val jSBundleLoader: JSBundleLoader,
26+
override val reactPackages: List<ReactPackage> = emptyList(),
27+
override val jSEngineInstance: JSEngineInstance = HermesInstance(),
28+
override val bindingsInstaller: BindingsInstaller = DefaultBindingsInstaller(),
29+
private val turboModuleManagerDelegate:
30+
(context: ReactApplicationContext) -> TurboModuleManagerDelegate,
31+
private val reactNativeConfig: ReactNativeConfig = ReactNativeConfig.DEFAULT_CONFIG,
32+
private val exceptionHandler: (Exception) -> Unit = {}
33+
) : ReactHostDelegate {
34+
35+
override fun getTurboModuleManagerDelegate(context: ReactApplicationContext) =
36+
turboModuleManagerDelegate(context)
37+
38+
override fun getReactNativeConfig(context: ReactContext) = reactNativeConfig
39+
40+
override fun handleInstanceException(error: Exception) = exceptionHandler(error)
41+
}

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactHostDelegateTest.kt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
package com.facebook.react.bridgeless
99

10-
import com.facebook.react.ReactPackage
1110
import com.facebook.react.bridge.JSBundleLoader
11+
import com.facebook.react.bridgeless.hermes.HermesInstance
1212
import com.facebook.react.common.annotations.UnstableReactNativeAPI
13-
import com.facebook.react.fabric.ReactNativeConfig
13+
import com.facebook.react.defaults.DefaultReactHostDelegate
1414
import com.facebook.react.turbomodule.core.TurboModuleManagerDelegate
1515
import com.facebook.testutils.shadows.ShadowSoLoader
1616
import org.assertj.core.api.Assertions.assertThat
@@ -25,28 +25,23 @@ import org.robolectric.annotation.Config
2525
@Config(shadows = [ShadowSoLoader::class])
2626
class ReactHostDelegateTest {
2727

28-
/** Mock test for ReactInstanceDelegate, used to setup the process to create a stable API */
28+
/**
29+
* Mock test for {@link DefaultReactHostDelegate}, used to setup the process to create a stable
30+
* API
31+
*/
2932
@Test
30-
fun testReactInstanceDelegateCreation() {
33+
fun testDefaultReactHostDelegateCreation() {
3134
val jsBundleLoader: JSBundleLoader = Mockito.mock(JSBundleLoader::class.java)
32-
val reactPackage: ReactPackage = Mockito.mock(ReactPackage::class.java)
33-
val bindingsInstallerMock: BindingsInstaller = Mockito.mock(BindingsInstaller::class.java)
3435
val turboModuleManagerDelegateMock: TurboModuleManagerDelegate =
3536
Mockito.mock(TurboModuleManagerDelegate::class.java)
36-
val jsEngineInstanceMock: JSEngineInstance = Mockito.mock(JSEngineInstance::class.java)
37-
val reactNativeConfigMock: ReactNativeConfig = Mockito.mock(ReactNativeConfig::class.java)
38-
val reactPackages = listOf(reactPackage)
37+
val hermesInstance: JSEngineInstance = Mockito.mock(HermesInstance::class.java)
3938
val jsMainModulePathMocked = "mockedJSMainModulePath"
4039
val delegate =
41-
ReactHostDelegate.ReactHostDelegateBase(
42-
jsMainModulePathMocked,
40+
DefaultReactHostDelegate(
41+
jSMainModulePath = jsMainModulePathMocked,
4342
jSBundleLoader = jsBundleLoader,
44-
reactPackages = reactPackages,
45-
bindingsInstaller = bindingsInstallerMock,
46-
jSEngineInstance = jsEngineInstanceMock,
47-
reactNativeConfig = reactNativeConfigMock,
48-
turboModuleManagerDelegate = { turboModuleManagerDelegateMock },
49-
exceptionHandler = {})
43+
jSEngineInstance = hermesInstance,
44+
turboModuleManagerDelegate = { turboModuleManagerDelegateMock })
5045

5146
assertThat(delegate.jSMainModulePath).isEqualTo(jsMainModulePathMocked)
5247
}

0 commit comments

Comments
 (0)