Skip to content

Commit

Permalink
Allow DefaultReactHost to register C++ view managers when DefaultComp…
Browse files Browse the repository at this point in the history
…onentsRegistry is not used.

Summary:
Some apps, such as Ads Manager and Twilight don't use DefaultComponentsRegistry to register C++ view managers. Instead these apps either make their own Registry (ex: [Twilight](https://www.internalfb.com/code/fbsource/[1585b5fe6b3e0a8d6a620a02034def4ba27abd53]/fbandroid/java/com/oculus/twilight/crossapp/reactpackage/XOCReactNativeHost.java?lines=43%2C130)) or they use the CatalystRegistery (ex: [AdsManager](https://www.internalfb.com/code/fbsource/[0ccac33cd873b8d2efc73a12af76836d535d7fc8]/fbandroid/java/com/facebook/adsmanager/AdsManagerApplicationImpl.java?lines=484-484)).

When migrating from away usage of ReactNativeHost to ReactHost, there was previously no way to register these view managers, leading to bridgeless specific bugs. Long term, we will make a solution to automatically register these view managers as part of our code gen, and when this is complete, this diff can be reverted.

Differential Revision: D64149373
  • Loading branch information
Maddie Lord authored and facebook-github-bot committed Oct 10, 2024
1 parent b70709d commit f626f78
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public object DefaultReactHost {
* @param useDevSupport whether to enable dev support, default to ReactBuildConfig.DEBUG.
* @param cxxReactPackageProviders a list of cxxreactpackage providers (to register c++ turbo
* modules)
* @param externalComponentFactory temporary way to register C++ view managers that are not using
* the DefaultComponentsRegistry.
*
* TODO(T186951312): Should this be @UnstableReactNativeAPI?
*/
Expand All @@ -59,6 +61,7 @@ public object DefaultReactHost {
isHermesEnabled: Boolean = true,
useDevSupport: Boolean = ReactBuildConfig.DEBUG,
cxxReactPackageProviders: List<(ReactContext) -> CxxReactPackage> = emptyList(),
externalComponentFactory: ComponentFactory? = null,
): ReactHost {
if (reactHost == null) {
val jsBundleLoader =
Expand All @@ -81,21 +84,49 @@ public object DefaultReactHost {
reactPackages = packageList,
jsRuntimeFactory = jsRuntimeFactory,
turboModuleManagerDelegateBuilder = defaultTmmDelegateBuilder)
val componentFactory = ComponentFactory()
DefaultComponentsRegistry.register(componentFactory)

val defaultComponentFactory = ComponentFactory()
if (externalComponentFactory == null) {
DefaultComponentsRegistry.register(defaultComponentFactory)
}

// TODO: T164788699 find alternative of accessing ReactHostImpl for initialising reactHost
reactHost =
ReactHostImpl(
context,
defaultReactHostDelegate,
componentFactory,
externalComponentFactory ?: defaultComponentFactory,
true /* allowPackagerServerAccess */,
useDevSupport,
)
}
return reactHost as ReactHost
}

@OptIn(UnstableReactNativeAPI::class)
@JvmStatic
public fun getDefaultReactHost(
context: Context,
packageList: List<ReactPackage>,
jsMainModulePath: String = "index",
jsBundleAssetPath: String = "index",
jsBundleFilePath: String? = null,
isHermesEnabled: Boolean = true,
useDevSupport: Boolean = ReactBuildConfig.DEBUG,
cxxReactPackageProviders: List<(ReactContext) -> CxxReactPackage> = emptyList(),
): ReactHost {
return getDefaultReactHost(
context,
packageList,
jsMainModulePath,
jsBundleAssetPath,
jsBundleFilePath,
isHermesEnabled,
useDevSupport,
cxxReactPackageProviders,
null)
}

/**
* Util function to create a default [ReactHost] to be used in your application. This method is
* used by the New App template.
Expand Down

0 comments on commit f626f78

Please sign in to comment.