Skip to content

Commit

Permalink
Improve reusability for RCTRootViewFactory (#43528)
Browse files Browse the repository at this point in the history
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: #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
  • Loading branch information
Kudo authored and facebook-github-bot committed Apr 19, 2024
1 parent c9b3a31 commit 23709f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@protocol RCTComponentViewFactoryComponentProvider;
@protocol RCTTurboModuleManagerDelegate;
@class RCTBridge;
@class RCTHost;
@class RCTRootView;
@class RCTSurfacePresenterBridgeAdapter;

Expand Down Expand Up @@ -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
Expand All @@ -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
31 changes: 18 additions & 13 deletions packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ @interface RCTRootViewFactory () <RCTCxxBridgeDelegate> {
@end

@implementation RCTRootViewFactory {
RCTHost *_reactHost;
RCTRootViewFactoryConfiguration *_configuration;
__weak id<RCTTurboModuleManagerDelegate> _turboModuleManagerDelegate;
}
Expand Down Expand Up @@ -144,7 +143,7 @@ - (UIView *)viewWithModuleName:(NSString *)moduleName

[self createReactHostIfNeeded:launchOptions];

RCTFabricSurface *surface = [_reactHost createSurfaceWithModuleName:moduleName initialProperties:initProps];
RCTFabricSurface *surface = [self.reactHost createSurfaceWithModuleName:moduleName initialProperties:initProps];

RCTSurfaceHostingProxyRootView *surfaceHostingProxyRootView = [[RCTSurfaceHostingProxyRootView alloc]
initWithSurface:surface
Expand Down Expand Up @@ -228,23 +227,29 @@ - (void)createBridgeAdapterIfNeeded

- (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
Expand Down

0 comments on commit 23709f7

Please sign in to comment.