Skip to content

Commit

Permalink
Prepare AppDelegate to enable Concurrent Root on iOS (#33671)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #33671

With React 18, we now need to allow users on Fabric to opt-in for Concurrent Root.

This commit adds a new method that can be customized in the AppDelegate to turn the feature on and off.
The flag is passed as an initialProps to the rootView.

## Changelog:
[iOS][Added] - Prepare a method in the AppDelegate to control the concurrentRoot.

Reviewed By: cortinico, dmitryrykun

Differential Revision: D35757833

fbshipit-source-id: 192cf74c796554cba39366aa90c53c191f960c20
  • Loading branch information
Riccardo Cipolleschi authored and facebook-github-bot committed Apr 21, 2022
1 parent 323db75 commit 8ac8439
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
31 changes: 25 additions & 6 deletions packages/rn-tester/RNTester/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate>
}
@end

static NSString *const kRNConcurrentRoot = @"concurrentRoot";

@implementation AppDelegate

- (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Expand All @@ -86,12 +88,7 @@ - (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWith
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];

// Appetizer.io params check
NSDictionary *initProps = @{};
NSString *_routeUri = [[NSUserDefaults standardUserDefaults] stringForKey:@"route"];
if (_routeUri) {
initProps =
@{@"exampleFromAppetizeParams" : [NSString stringWithFormat:@"rntester://example/%@Example", _routeUri]};
}
NSDictionary *initProps = [self prepareInitialProps];

#ifdef RN_FABRIC_ENABLED
_contextContainer = std::make_shared<facebook::react::ContextContainer const>();
Expand Down Expand Up @@ -119,6 +116,28 @@ - (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWith
return YES;
}

- (BOOL)concurrentRootEnabled
{
// Switch this bool to turn on and off the concurrent root
return true;
}

- (NSDictionary *)prepareInitialProps
{
NSMutableDictionary *initProps = [NSMutableDictionary new];

NSString *_routeUri = [[NSUserDefaults standardUserDefaults] stringForKey:@"route"];
if (_routeUri) {
initProps[@"exampleFromAppetizeParams"] = [NSString stringWithFormat:@"rntester://example/%@Example", _routeUri];
}

#ifdef RCT_NEW_ARCH_ENABLED
initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
#endif

return initProps;
}

- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
{
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"packages/rn-tester/js/RNTesterApp.ios"];
Expand Down
27 changes: 26 additions & 1 deletion template/ios/HelloWorld/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <react/config/ReactNativeConfig.h>

static NSString *const kRNConcurrentRoot = @"concurrentRoot";

@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
RCTTurboModuleManager *_turboModuleManager;
RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
Expand All @@ -41,7 +43,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
#endif

UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"HelloWorld", nil);
NSDictionary *initProps = [self prepareInitialProps];
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"HelloWorld", initProps);

if (@available(iOS 13.0, *)) {
rootView.backgroundColor = [UIColor systemBackgroundColor];
Expand All @@ -57,6 +60,28 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
///
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
/// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
/// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
- (BOOL)concurrentRootEnabled
{
// Switch this bool to turn on and off the concurrent root
return true;
}

- (NSDictionary *)prepareInitialProps
{
NSMutableDictionary *initProps = [NSMutableDictionary new];

#ifdef RCT_NEW_ARCH_ENABLED
initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
#endif

return initProps;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
Expand Down

0 comments on commit 8ac8439

Please sign in to comment.