From 693392e38657de383b320e811da2d2f9a1949185 Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Tue, 19 Mar 2024 00:24:48 +0800 Subject: [PATCH] Improve reusability for RCTRootViewFactory --- .../AppDelegate/RCTRootViewFactory.h | 2 ++ .../AppDelegate/RCTRootViewFactory.mm | 30 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h index 1926dd81747565..5d8a27588a36e9 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h +++ b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h @@ -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 diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm index 6e93e4835f30d6..48bc46c2839b0b 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm @@ -95,7 +95,6 @@ @interface RCTRootViewFactory () { @end @implementation RCTRootViewFactory { - RCTHost *_reactHost; RCTRootViewFactoryConfiguration *_configuration; __weak id _turboModuleManagerDelegate; } @@ -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 @@ -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() { - return [weakSelf createJSRuntimeFactory]; - } - launchOptions:launchOptions]; - [_reactHost setBundleURLProvider:^NSURL *() { + RCTHost *reactHost = [[RCTHost alloc] initWithBundleURLProvider:self->_configuration.bundleURLBlock + hostDelegate:nil + turboModuleManagerDelegate:_turboModuleManagerDelegate + jsEngineProvider:^std::shared_ptr() { + 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)createJSRuntimeFactory