Skip to content

Commit

Permalink
Improve reusability for RCTRootViewFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudo committed Apr 10, 2024
1 parent 54757ca commit 693392e
Show file tree
Hide file tree
Showing 2 changed files with 19 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 Down
30 changes: 17 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 @@ -139,7 +138,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 @@ -223,23 +222,28 @@ - (void)createBridgeAdapterIfNeeded

- (void)createReactHostIfNeeded:(NSDictionary *)launchOptions
{
if (_reactHost) {
if (self.reactHost) {
return;
}
self.reactHost = [self createReactHost];
}

- (RCTHost *)createReactHost
{
__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 693392e

Please sign in to comment.