From 23709f7c61caebebafa9b01264176cb95204ec42 Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Fri, 19 Apr 2024 06:12:11 -0700 Subject: [PATCH] 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 --- .../AppDelegate/RCTRootViewFactory.h | 6 ++++ .../AppDelegate/RCTRootViewFactory.mm | 31 +++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h index 10e4e086271..bc52c1712cc 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 @@ -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 diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm index 3c5c0389140..dd85650179a 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm @@ -95,7 +95,6 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri @end @implementation RCTRootViewFactory { - RCTHost *_reactHost; RCTRootViewFactoryConfiguration *_configuration; __weak id _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() { - 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