|
1 | 1 | #import "AppDelegate.h"
|
2 | 2 |
|
3 | 3 | #import <React/RCTBridge.h>
|
| 4 | +#import <React/RCTFabricSurfaceHostingProxyRootView.h> |
| 5 | +#import <React/RCTSurfacePresenter.h> |
| 6 | +#import <React/RCTSurfacePresenterBridgeAdapter.h> |
4 | 7 | #import <React/RCTBundleURLProvider.h>
|
| 8 | +#import <React/RCTCxxBridgeDelegate.h> |
| 9 | +#import <React/RCTDataRequestHandler.h> |
| 10 | +#import <React/RCTFileRequestHandler.h> |
| 11 | +#import <React/RCTGIFImageDecoder.h> |
| 12 | +#import <React/RCTHTTPRequestHandler.h> |
| 13 | +#import <React/RCTImageLoader.h> |
| 14 | +#import <React/RCTJSIExecutorRuntimeInstaller.h> |
| 15 | +#import <React/RCTJavaScriptLoader.h> |
| 16 | +#import <React/RCTLinkingManager.h> |
| 17 | +#import <React/RCTLocalAssetImageLoader.h> |
| 18 | +#import <React/RCTNetworking.h> |
5 | 19 | #import <React/RCTRootView.h>
|
| 20 | +#import <React/JSCExecutorFactory.h> |
6 | 21 |
|
7 | 22 | #ifdef FB_SONARKIT_ENABLED
|
8 | 23 | #import <FlipperKit/FlipperClient.h>
|
|
11 | 26 | #import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
|
12 | 27 | #import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
|
13 | 28 | #import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
|
| 29 | +#endif |
14 | 30 |
|
15 |
| -static void InitializeFlipper(UIApplication *application) { |
16 |
| - FlipperClient *client = [FlipperClient sharedClient]; |
17 |
| - SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults]; |
18 |
| - [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]]; |
19 |
| - [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]]; |
20 |
| - [client addPlugin:[FlipperKitReactPlugin new]]; |
21 |
| - [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]]; |
22 |
| - [client start]; |
| 31 | +#import <react/config/ReactNativeConfig.h> |
| 32 | + |
| 33 | +#import <ReactCommon/RCTTurboModuleManager.h> |
| 34 | + |
| 35 | +#import "FabricExampleTurboModuleProvider.h" |
| 36 | + |
| 37 | + |
| 38 | +@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> { |
| 39 | + RCTSurfacePresenterBridgeAdapter *_bridgeAdapter; |
| 40 | + std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig; |
| 41 | + facebook::react::ContextContainer::Shared _contextContainer; |
| 42 | + RCTTurboModuleManager *_turboModuleManager; |
23 | 43 | }
|
24 |
| -#endif |
| 44 | +@end |
25 | 45 |
|
26 | 46 | @implementation AppDelegate
|
27 | 47 |
|
28 | 48 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
29 | 49 | {
|
30 |
| -#ifdef FB_SONARKIT_ENABLED |
31 |
| - InitializeFlipper(application); |
32 |
| -#endif |
33 | 50 |
|
34 |
| - RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; |
35 |
| - RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge |
36 |
| - moduleName:@"FabricExample" |
37 |
| - initialProperties:nil]; |
| 51 | + RCTEnableTurboModule(YES); |
38 | 52 |
|
39 |
| - if (@available(iOS 13.0, *)) { |
40 |
| - rootView.backgroundColor = [UIColor systemBackgroundColor]; |
41 |
| - } else { |
42 |
| - rootView.backgroundColor = [UIColor whiteColor]; |
43 |
| - } |
| 53 | + _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; |
| 54 | + |
| 55 | + NSDictionary *initProps = @{}; |
| 56 | + |
| 57 | + _contextContainer = std::make_shared<facebook::react::ContextContainer const>(); |
| 58 | + _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>(); |
| 59 | + |
| 60 | + _contextContainer->insert("ReactNativeConfig", _reactNativeConfig); |
| 61 | + |
| 62 | + _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:_bridge contextContainer:_contextContainer]; |
| 63 | + |
| 64 | + _bridge.surfacePresenter = _bridgeAdapter.surfacePresenter; |
44 | 65 |
|
| 66 | + UIView *rootView = [[RCTFabricSurfaceHostingProxyRootView alloc] initWithBridge:_bridge |
| 67 | + moduleName:@"FabricExample" |
| 68 | + initialProperties:initProps]; |
| 69 | + |
45 | 70 | self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
|
46 | 71 | UIViewController *rootViewController = [UIViewController new];
|
47 | 72 | rootViewController.view = rootView;
|
48 | 73 | self.window.rootViewController = rootViewController;
|
49 | 74 | [self.window makeKeyAndVisible];
|
| 75 | + [self initializeFlipper:application]; |
50 | 76 | return YES;
|
51 | 77 | }
|
52 | 78 |
|
53 |
| -- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge |
| 79 | + |
| 80 | + |
| 81 | +#ifdef FB_SONARKIT_ENABLED |
| 82 | +- (void)initializeFlipper:(UIApplication *)application { |
| 83 | + FlipperClient *client = [FlipperClient sharedClient]; |
| 84 | + SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults]; |
| 85 | + [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]]; |
| 86 | + [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]]; |
| 87 | + [client addPlugin:[FlipperKitReactPlugin new]]; |
| 88 | + [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]]; |
| 89 | + [client start]; |
| 90 | +} |
| 91 | +#endif |
| 92 | + |
| 93 | + |
| 94 | +- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge |
54 | 95 | {
|
55 |
| -#if DEBUG |
56 | 96 | return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
|
57 |
| -#else |
58 |
| - return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; |
| 97 | +} |
| 98 | + |
| 99 | +- (void)loadSourceForBridge:(RCTBridge *)bridge |
| 100 | + onProgress:(RCTSourceLoadProgressBlock)onProgress |
| 101 | + onComplete:(RCTSourceLoadBlock)loadCallback |
| 102 | +{ |
| 103 | + [RCTJavaScriptLoader loadBundleAtURL:[self sourceURLForBridge:bridge] onProgress:onProgress onComplete:loadCallback]; |
| 104 | +} |
| 105 | + |
| 106 | +#pragma mark - RCTCxxBridgeDelegate |
| 107 | + |
| 108 | +- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge |
| 109 | +{ |
| 110 | + _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge |
| 111 | + delegate:self |
| 112 | + jsInvoker:bridge.jsCallInvoker]; |
| 113 | + [bridge setRCTTurboModuleRegistry:_turboModuleManager]; |
| 114 | + |
| 115 | +#if RCT_DEV |
| 116 | + /** |
| 117 | + * Eagerly initialize RCTDevMenu so CMD + d, CMD + i, and CMD + r work. |
| 118 | + * This is a stop gap until we have a system to eagerly init Turbo Modules. |
| 119 | + */ |
| 120 | + [_turboModuleManager moduleForName:"RCTDevMenu"]; |
59 | 121 | #endif
|
| 122 | + |
| 123 | + __weak __typeof(self) weakSelf = self; |
| 124 | + return std::make_unique<facebook::react::JSCExecutorFactory>( |
| 125 | + facebook::react::RCTJSIExecutorRuntimeInstaller([weakSelf, bridge](facebook::jsi::Runtime &runtime) { |
| 126 | + if (!bridge) { |
| 127 | + return; |
| 128 | + } |
| 129 | + __typeof(self) strongSelf = weakSelf; |
| 130 | + if (strongSelf) { |
| 131 | + facebook::react::RuntimeExecutor syncRuntimeExecutor = |
| 132 | + [&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); }; |
| 133 | + [strongSelf->_turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor]; |
| 134 | + } |
| 135 | + })); |
| 136 | +} |
| 137 | + |
| 138 | +#pragma mark RCTTurboModuleManagerDelegate |
| 139 | + |
| 140 | +- (Class)getModuleClassFromName:(const char *)name |
| 141 | +{ |
| 142 | + return facebook::react::FabricExampleTurboModuleClassProvider(name); |
| 143 | +} |
| 144 | + |
| 145 | +- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name |
| 146 | + jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker |
| 147 | +{ |
| 148 | + return facebook::react::FabricExampleTurboModuleProvider(name, jsInvoker); |
| 149 | +} |
| 150 | + |
| 151 | +- (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass |
| 152 | +{ |
| 153 | + if (moduleClass == RCTImageLoader.class) { |
| 154 | + return [[moduleClass alloc] initWithRedirectDelegate:nil |
| 155 | + loadersProvider:^NSArray<id<RCTImageURLLoader>> *(RCTModuleRegistry * moduleRegistry) { |
| 156 | + return @ [[RCTLocalAssetImageLoader new]]; |
| 157 | + } |
| 158 | + decodersProvider:^NSArray<id<RCTImageDataDecoder>> *(RCTModuleRegistry * moduleRegistry) { |
| 159 | + return @ [[RCTGIFImageDecoder new]]; |
| 160 | + }]; |
| 161 | + } else if (moduleClass == RCTNetworking.class) { |
| 162 | + return [[moduleClass alloc] initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> *(RCTModuleRegistry * moduleRegistry) { |
| 163 | + return @[ |
| 164 | + [RCTHTTPRequestHandler new], |
| 165 | + [RCTDataRequestHandler new], |
| 166 | + [RCTFileRequestHandler new], |
| 167 | + ]; |
| 168 | + }]; |
| 169 | + } |
| 170 | + // No custom initializer here. |
| 171 | + return [moduleClass new]; |
60 | 172 | }
|
61 | 173 |
|
62 | 174 | @end
|
0 commit comments