Skip to content

Commit

Permalink
Add DefaultReactHost for Template (facebook#39585)
Browse files Browse the repository at this point in the history
Summary:

So that to enable Bridgeless in Template android we only need to override the ReactHostInterface variable in Template's application, see D49464580

Changelog:
[Android][Changed] - Add DefaultReactHost

Differential Revision: D49463901
  • Loading branch information
luluwu2032 authored and facebook-github-bot committed Sep 25, 2023
1 parent 87d2ea9 commit 4c21655
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.defaults

import android.content.Context
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.fabric.ComponentFactory
import com.facebook.react.interfaces.ReactHost
import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler
import com.facebook.react.runtime.JSCInstance
import com.facebook.react.runtime.ReactHostImpl
import com.facebook.react.runtime.hermes.HermesInstance

@UnstableReactNativeAPI
class DefaultReactHost {
companion object {
private var reactHost: ReactHost? = null

fun getDefaultReactHost(
context: Context,
jsMainModulePath: String,
jsBundleAssetPath: String = "",
packageList: List<ReactPackage>,
isHermesEnabled: Boolean = true
): ReactHost {
if (reactHost == null) {
val jsBundleLoader =
JSBundleLoader.createAssetLoader(context, "assets://$jsBundleAssetPath", true)
val jsEngineInstance = if (isHermesEnabled) HermesInstance() else JSCInstance()
val defaultReactHostDelegate =
DefaultReactHostDelegate(
jsMainModulePath = jsMainModulePath,
jsBundleLoader = jsBundleLoader,
reactPackages = packageList,
jsEngineInstance = jsEngineInstance,
turboModuleManagerDelegateBuilder = DefaultTurboModuleManagerDelegate.Builder())
val reactJsExceptionHandler = ReactJsExceptionHandler { _ -> }
val componentFactory = ComponentFactory()
DefaultComponentsRegistry.register(componentFactory)
// TODO: T164788699 find alternative of accessing ReactHostImpl for initialising reactHost
reactHost =
ReactHostImpl(
context,
defaultReactHostDelegate,
componentFactory,
true,
reactJsExceptionHandler,
true)
}
return reactHost as ReactHost
}
}
}

0 comments on commit 4c21655

Please sign in to comment.