Skip to content

Commit

Permalink
fix(react-native-host): handle RCTHost interface changes (#3081)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Apr 11, 2024
1 parent 745d937 commit 9f51524
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-days-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/react-native-host": patch
---

Handle `launchOptions` being added to `RCTHost` initializer in 0.74 (see https://github.com/facebook/react-native/pull/43757)
15 changes: 15 additions & 0 deletions packages/react-native-host/cocoa/RNXBridgelessHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ using SharedJSRuntimeFactory = std::shared_ptr<facebook::react::JSRuntimeFactory
@interface RCTHost (Compatibility)
@property (nonatomic, readonly) RCTModuleRegistry *moduleRegistry; // Introduced in 0.74
@property (nonatomic, readonly) RCTSurfacePresenter *surfacePresenter; // Introduced in 0.74

// Introduced in 0.74
- (instancetype)initWithBundleURLProvider:(RCTHostBundleURLProvider)provider
hostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:
(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider
launchOptions:(nullable NSDictionary *)launchOptions;

// Deprecated in 0.74
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
hostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider;

- (RCTModuleRegistry *)getModuleRegistry; // Deprecated in 0.74, and removed in 0.75
- (RCTSurfacePresenter *)getSurfacePresenter; // Deprecated in 0.74, and removed in 0.75
@end
Expand Down
7 changes: 5 additions & 2 deletions packages/react-native-host/cocoa/ReactNativeHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype)init NS_UNAVAILABLE;

- (instancetype)initWithConfig:(id<RNXHostConfig>)config NS_DESIGNATED_INITIALIZER
NS_SWIFT_NAME(init(_:));
- (instancetype)initWithConfig:(id<RNXHostConfig>)config NS_SWIFT_NAME(init(_:));

- (instancetype)initWithConfig:(id<RNXHostConfig>)config
launchOptions:(nullable NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER
NS_SWIFT_NAME(init(_:launchOptions:));

- (void)shutdown;

Expand Down
33 changes: 27 additions & 6 deletions packages/react-native-host/cocoa/ReactNativeHost.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ @interface ReactNativeHost () <RCTCxxBridgeDelegate>

@implementation ReactNativeHost {
__weak id<RNXHostConfig> _config;
NSDictionary *_launchOptions;
RNXTurboModuleAdapter *_turboModuleAdapter;
RCTSurfacePresenterBridgeAdapter *_surfacePresenterBridgeAdapter;
RCTBridge *_bridge;
Expand All @@ -41,6 +42,11 @@ @implementation ReactNativeHost {
}

- (instancetype)initWithConfig:(id<RNXHostConfig>)config
{
return [self initWithConfig:config launchOptions:nil];
}

- (instancetype)initWithConfig:(id<RNXHostConfig>)config launchOptions:(NSDictionary *)launchOptions
{
if (self = [super init]) {
if ([config respondsToSelector:@selector(isDevLoadingViewEnabled)]) {
Expand All @@ -64,6 +70,7 @@ - (instancetype)initWithConfig:(id<RNXHostConfig>)config
}

_config = config;
_launchOptions = launchOptions;
[self enableTurboModule];
_isShuttingDown = [[NSLock alloc] init];

Expand All @@ -90,7 +97,7 @@ - (RCTBridge *)bridge

@try {
if (_bridge == nil) {
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
_surfacePresenterBridgeAdapter = RNXInstallSurfacePresenterBridgeAdapter(_bridge);
[_hostReleaser setBridge:_bridge];
}
Expand Down Expand Up @@ -250,12 +257,26 @@ - (void)initializeReactHost
#endif // USE_HERMES
};

_reactHost = [[RCTHost alloc] initWithBundleURL:[self sourceURLForBridge:nil]
hostDelegate:nil
turboModuleManagerDelegate:_turboModuleAdapter
jsEngineProvider:jsEngineProvider];

__weak __typeof(self) weakSelf = self;
if ([RCTHost instancesRespondToSelector:@selector
(initWithBundleURLProvider:
hostDelegate:turboModuleManagerDelegate:jsEngineProvider
:launchOptions:)]) {
_reactHost = [[RCTHost alloc]
initWithBundleURLProvider:^{
return [weakSelf sourceURLForBridge:nil];
}
hostDelegate:nil
turboModuleManagerDelegate:_turboModuleAdapter
jsEngineProvider:jsEngineProvider
launchOptions:_launchOptions];
} else {
_reactHost = [[RCTHost alloc] initWithBundleURL:[self sourceURLForBridge:nil]
hostDelegate:nil
turboModuleManagerDelegate:_turboModuleAdapter
jsEngineProvider:jsEngineProvider];
}

[_reactHost setBundleURLProvider:^NSURL *() {
return [weakSelf sourceURLForBridge:nil];
}];
Expand Down

0 comments on commit 9f51524

Please sign in to comment.