Skip to content

Commit

Permalink
Make RNTester use RCTAppDelegate (#37572)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37572

Currently, RNTester was using a completely custom AppDelegate and not leveraging the RCTAppDelegate we use in the OSS. This resulted in a misalignment between the two setups and duplicated work to test stuff internally furst and then in the OSS, with some more time needed to understand why one setup was working and the other wasn't.

With this change, we are aligning the two, bringing RNTester closer to the OSS setup. There are still small differences, but we can iterate over those.

## Changelog:
[iOS][Changed] - Make RNTester use RCTAppDelegate

Reviewed By: cortinico

Differential Revision: D46182888

fbshipit-source-id: 4b013abdc5f273ee9b5b3a38195056a4b4edf39f
  • Loading branch information
cipolleschi authored and facebook-github-bot committed May 25, 2023
1 parent bdeb09b commit 054a0ea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 226 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
Expand All @@ -11,7 +12,7 @@ @implementation RCTLegacyInteropComponents

+ (NSArray<NSString *> *)legacyInteropComponents
{
return @[];
return @[ @"RNTMyLegacyNativeView" ];
}

@end
8 changes: 2 additions & 6 deletions packages/rn-tester/RNTester/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

#import <RCTAppDelegate.h>
#import <UIKit/UIKit.h>

@class RCTBridge;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, readonly) RCTBridge *bridge;
@interface AppDelegate : RCTAppDelegate

@end
240 changes: 21 additions & 219 deletions packages/rn-tester/RNTester/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,152 +7,42 @@

#import "AppDelegate.h"

#ifndef RCT_USE_HERMES
#if __has_include(<reacthermes/HermesExecutorFactory.h>)
#define RCT_USE_HERMES 1
#else
#define RCT_USE_HERMES 0
#endif
#endif

#ifdef RCT_NEW_ARCH_ENABLED
#ifndef RN_FABRIC_ENABLED
#define RN_FABRIC_ENABLED
#endif
#endif

#if RCT_USE_HERMES
#import <reacthermes/HermesExecutorFactory.h>
#else
#import <React/JSCExecutorFactory.h>
#endif

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTCxxBridgeDelegate.h>
#import <React/RCTDataRequestHandler.h>
#import <React/RCTFileRequestHandler.h>
#import <React/RCTGIFImageDecoder.h>
#import <React/RCTHTTPRequestHandler.h>
#import <React/RCTImageLoader.h>
#import <React/RCTJSIExecutorRuntimeInstaller.h>
#import <React/RCTJavaScriptLoader.h>
#import <React/RCTLinkingManager.h>
#import <React/RCTLocalAssetImageLoader.h>
#import <React/RCTNetworking.h>
#import <React/RCTRootView.h>

#import <cxxreact/JSExecutor.h>
#import "RNTesterTurboModuleProvider.h"

#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
#import <React/RCTPushNotificationManager.h>
#endif

#ifdef RN_FABRIC_ENABLED
#import <React/RCTComponentViewFactory.h>
#import <React/RCTFabricSurfaceHostingProxyRootView.h>
#import <React/RCTSurfacePresenter.h>
#import <React/RCTSurfacePresenterBridgeAdapter.h>

#import <React/RCTLegacyViewManagerInteropComponentView.h>
#import <react/config/ReactNativeConfig.h>
#import <react/renderer/runtimescheduler/RuntimeScheduler.h>
#import <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>
#import <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h>
#endif

#if RCT_NEW_ARCH_ENABLED
#import <RNTMyNativeViewComponentView.h>
#endif

#if DEBUG
#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitLayoutPlugin/SKDescriptorMapper.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#endif
#endif
// RCTImage
#import <React/RCTGIFImageDecoder.h>
#import <React/RCTImageLoader.h>
#import <React/RCTImageURLLoader.h>
#import <React/RCTLocalAssetImageLoader.h>

#import <ReactCommon/RCTTurboModuleManager.h>
#import "RNTesterTurboModuleProvider.h"
// RCTNetworking
#import <React/RCTNetworking.h>

@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
#ifdef RN_FABRIC_ENABLED
RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
facebook::react::ContextContainer::Shared _contextContainer;
std::shared_ptr<facebook::react::RuntimeScheduler> _runtimeScheduler;
#endif
}
@end
// RCTURLRequestHandlers
#import <React/RCTDataRequestHandler.h>
#import <React/RCTFileRequestHandler.h>
#import <React/RCTHTTPRequestHandler.h>

#if RCT_NEW_ARCH_ENABLED
/// Declare conformance to `RCTComponentViewFactoryComponentProvider`
@interface AppDelegate () <RCTComponentViewFactoryComponentProvider>
@end
#import <RNTMyNativeViewComponentView.h>
#endif

static NSString *const kRNConcurrentRoot = @"concurrentRoot";

@implementation AppDelegate

#ifdef RN_FABRIC_ENABLED
- (instancetype)init
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (self = [super init]) {
_contextContainer = std::make_shared<facebook::react::ContextContainer const>();
_reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
_contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
}
return self;
}
#endif

- (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RCTEnableTurboModule(YES);

_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
self.moduleName = @"RNTesterApp";
// You can add your custom initial props in the dictionary below.
// They will be passed down to the ViewController used by React Native.
self.initialProps = [self prepareInitialProps];

// Appetizer.io params check
NSDictionary *initProps = [self prepareInitialProps];

#if RCT_NEW_ARCH_ENABLED
[RCTComponentViewFactory currentComponentViewFactory].thirdPartyFabricComponentsProvider = self;
#endif

#ifdef RN_FABRIC_ENABLED
_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:_bridge contextContainer:_contextContainer];

_bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;

UIView *rootView = [[RCTFabricSurfaceHostingProxyRootView alloc] initWithBridge:_bridge
moduleName:@"RNTesterApp"
initialProperties:initProps];

[self registerPaperComponents:@[ @"RNTMyLegacyNativeView" ]];
#else
UIView *rootView = [[RCTRootView alloc] initWithBridge:_bridge moduleName:@"RNTesterApp" initialProperties:initProps];
#endif

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
[self initializeFlipper:application];

return YES;
}

- (BOOL)concurrentRootEnabled
{
// Switch this bool to turn on and off the concurrent root
return true;
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (NSDictionary *)prepareInitialProps
Expand All @@ -164,33 +54,14 @@ - (NSDictionary *)prepareInitialProps
initProps[@"exampleFromAppetizeParams"] = [NSString stringWithFormat:@"rntester://example/%@Example", _routeUri];
}

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

return initProps;
}

- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"js/RNTesterApp.ios"];
}

- (void)initializeFlipper:(UIApplication *)application
{
#if DEBUG
#ifdef FB_SONARKIT_ENABLED
FlipperClient *client = [FlipperClient sharedClient];
SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
[client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application
withDescriptorMapper:layoutDescriptorMapper]];
[client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
[client addPlugin:[FlipperKitReactPlugin new]];
[client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
[client start];
#endif
#endif
}
//[self registerPaperComponents:@[ @"RNTMyLegacyNativeView" ]];

- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
Expand All @@ -206,62 +77,6 @@ - (void)loadSourceForBridge:(RCTBridge *)bridge
[RCTJavaScriptLoader loadBundleAtURL:[self sourceURLForBridge:bridge] onProgress:onProgress onComplete:loadCallback];
}

#pragma mark - RCTCxxBridgeDelegate

// This function is called during
// `[[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];`
- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
{
std::shared_ptr<facebook::react::CallInvoker> callInvoker = bridge.jsCallInvoker;

#ifdef RN_FABRIC_ENABLED
_runtimeScheduler = std::make_shared<facebook::react::RuntimeScheduler>(RCTRuntimeExecutorFromBridge(bridge));
_contextContainer->erase("RuntimeScheduler");
_contextContainer->insert("RuntimeScheduler", _runtimeScheduler);
callInvoker = std::make_shared<facebook::react::RuntimeSchedulerCallInvoker>(_runtimeScheduler);
#endif

RCTTurboModuleManager *turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
delegate:self
jsInvoker:callInvoker];
[bridge setRCTTurboModuleRegistry:turboModuleManager];

#if RCT_DEV
/**
* Eagerly initialize RCTDevMenu so CMD + d, CMD + i, and CMD + r work.
* This is a stop gap until we have a system to eagerly init Turbo Modules.
*/
[turboModuleManager moduleForName:"RCTDevMenu"];
#endif

__weak __typeof(self) weakSelf = self;

#if RCT_USE_HERMES
return std::make_unique<facebook::react::HermesExecutorFactory>(
#else
return std::make_unique<facebook::react::JSCExecutorFactory>(
#endif
facebook::react::RCTJSIExecutorRuntimeInstaller([weakSelf, bridge, turboModuleManager](
facebook::jsi::Runtime &runtime) {
if (!bridge) {
return;
}

#if RN_FABRIC_ENABLED
__typeof(self) strongSelf = weakSelf;
if (strongSelf && strongSelf->_runtimeScheduler) {
facebook::react::RuntimeSchedulerBinding::createAndInstallIfNeeded(runtime, strongSelf->_runtimeScheduler);
}
#endif

facebook::react::RuntimeExecutor syncRuntimeExecutor =
[&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); };
[turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor];
}));
}

#pragma mark - RCTTurboModuleManagerDelegate

- (Class)getModuleClassFromName:(const char *)name
{
return facebook::react::RNTesterTurboModuleClassProvider(name);
Expand Down Expand Up @@ -297,19 +112,6 @@ - (Class)getModuleClassFromName:(const char *)name
return [moduleClass new];
}

#pragma mark - Interop layer

- (void)registerPaperComponents:(NSArray<NSString *> *)components
{
#if RCT_NEW_ARCH_ENABLED
for (NSString *component in components) {
[RCTLegacyViewManagerInteropComponentView supportLegacyViewManagerWithName:component];
}
#endif
}

#pragma mark - Push Notifications

#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC

// Required to register for notifications
Expand Down
1 change: 1 addition & 0 deletions packages/rn-tester/react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
project: {
ios: {
sourceDir: '.',
unstable_reactLegacyComponentNames: ['RNTMyLegacyNativeView'],
},
android: {
sourceDir: '../../',
Expand Down

0 comments on commit 054a0ea

Please sign in to comment.