Improve reusability for RCTRootViewFactory (#43528)

Summary:
RCTRootViewFactory is a great work for creating react binding view. we want to reuse the factory inside expo and would be good to have these improvements.
- exposing `reactHost` property so that we can update the RCTHost instance without recreate a factory.
- break bridgeless creation logic to a specific `createReactHost`, so that we can reuse the method for RCTHost creation

## Changelog:

[IOS][CHANGED] - Improve reusability for RCTRootViewFactory

Pull Request resolved: https://github.com/facebook/react-native/pull/43528

Test Plan: this pr should not introduce any regression and getting all ci passed

Reviewed By: cortinico

Differential Revision: D56056103

Pulled By: cipolleschi

fbshipit-source-id: 9f312707b9013c36863945c9b99a697f949f10b5
This commit is contained in:
Kudo Chien
2024-04-19 06:12:11 -07:00
committed by Facebook GitHub Bot
parent c9b3a3106b
commit 23709f7c61
2 changed files with 24 additions and 13 deletions
@@ -13,6 +13,7 @@
@protocol RCTComponentViewFactoryComponentProvider;
@protocol RCTTurboModuleManagerDelegate;
@class RCTBridge;
@class RCTHost;
@class RCTRootView;
@class RCTSurfacePresenterBridgeAdapter;
@@ -147,6 +148,7 @@ typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *modu
@interface RCTRootViewFactory : NSObject
@property (nonatomic, strong, nullable) RCTBridge *bridge;
@property (nonatomic, strong, nullable) RCTHost *reactHost;
@property (nonatomic, strong, nullable) RCTSurfacePresenterBridgeAdapter *bridgeAdapter;
- (instancetype)initWithConfiguration:(RCTRootViewFactoryConfiguration *)configuration
@@ -170,6 +172,10 @@ typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *modu
- (UIView *_Nonnull)viewWithModuleName:(NSString *)moduleName;
#pragma mark - RCTRootViewFactory Helpers
- (RCTHost *)createReactHost:(NSDictionary *__nullable)launchOptions;
@end
NS_ASSUME_NONNULL_END
@@ -95,7 +95,6 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri
@end
@implementation RCTRootViewFactory {
RCTHost *_reactHost;
RCTRootViewFactoryConfiguration *_configuration;
__weak id<RCTTurboModuleManagerDelegate> _turboModuleManagerDelegate;
}
@@ -144,7 +143,7 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri
[self createReactHostIfNeeded:launchOptions];
RCTFabricSurface *surface = [_reactHost createSurfaceWithModuleName:moduleName initialProperties:initProps];
RCTFabricSurface *surface = [self.reactHost createSurfaceWithModuleName:moduleName initialProperties:initProps];
RCTSurfaceHostingProxyRootView *surfaceHostingProxyRootView = [[RCTSurfaceHostingProxyRootView alloc]
initWithSurface:surface
@@ -228,23 +227,29 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri
- (void)createReactHostIfNeeded:(NSDictionary *)launchOptions
{
if (_reactHost) {
if (self.reactHost) {
return;
}
self.reactHost = [self createReactHost:launchOptions];
}
- (RCTHost *)createReactHost:(NSDictionary *)launchOptions
{
__weak __typeof(self) weakSelf = self;
_reactHost = [[RCTHost alloc] initWithBundleURLProvider:self->_configuration.bundleURLBlock
hostDelegate:nil
turboModuleManagerDelegate:_turboModuleManagerDelegate
jsEngineProvider:^std::shared_ptr<facebook::react::JSRuntimeFactory>() {
return [weakSelf createJSRuntimeFactory];
}
launchOptions:launchOptions];
[_reactHost setBundleURLProvider:^NSURL *() {
RCTHost *reactHost =
[[RCTHost alloc] initWithBundleURLProvider:self->_configuration.bundleURLBlock
hostDelegate:nil
turboModuleManagerDelegate:_turboModuleManagerDelegate
jsEngineProvider:^std::shared_ptr<facebook::react::JSRuntimeFactory>() {
return [weakSelf createJSRuntimeFactory];
}
launchOptions:launchOptions];
[reactHost setBundleURLProvider:^NSURL *() {
return [weakSelf bundleURL];
}];
[_reactHost setContextContainerHandler:self];
[_reactHost start];
[reactHost setContextContainerHandler:self];
[reactHost start];
return reactHost;
}
- (std::shared_ptr<facebook::react::JSRuntimeFactory>)createJSRuntimeFactory