Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Bridgeless: Expose host delegate methods #44158

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <React/RCTRootView.h>
#import <React/RCTSurfacePresenterBridgeAdapter.h>
#import <React/RCTUtils.h>
#import <ReactCommon/RCTHost.h>
#import <objc/runtime.h>
#import <react/featureflags/ReactNativeFeatureFlags.h>
#import <react/featureflags/ReactNativeFeatureFlagsDefaults.h>
Expand All @@ -34,7 +35,7 @@
#import <react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h>
#import <react/nativemodule/microtasks/NativeMicrotasks.h>

@interface RCTAppDelegate () <RCTComponentViewFactoryComponentProvider>
@interface RCTAppDelegate () <RCTComponentViewFactoryComponentProvider, RCTHostDelegate>
@end

@implementation RCTAppDelegate
Expand Down Expand Up @@ -176,6 +177,20 @@ - (NSURL *)bundleURL
return nullptr;
}

#pragma mark - RCTHostDelegate

- (void)hostDidStart:(RCTHost *)host
{
}

- (void)host:(RCTHost *)host
didReceiveJSErrorStack:(NSArray<NSDictionary<NSString *, id> *> *)stack
message:(NSString *)message
exceptionId:(NSUInteger)exceptionId
isFatal:(BOOL)isFatal
{
}

#pragma mark - Bridge and Bridge Adapter properties

- (RCTBridge *)bridge
Expand Down Expand Up @@ -273,6 +288,19 @@ - (RCTRootViewFactory *)createRCTRootViewFactory
return [weakSelf sourceURLForBridge:bridge];
};

configuration.hostDidStartBlock = ^(RCTHost *_Nonnull host) {
[weakSelf hostDidStart:host];
};

configuration.hostDidReceiveJSErrorStackBlock =
^(RCTHost *_Nonnull host,
NSArray<NSDictionary<NSString *, id> *> *_Nonnull stack,
NSString *_Nonnull message,
NSUInteger exceptionId,
BOOL isFatal) {
[weakSelf host:host didReceiveJSErrorStack:stack message:message exceptionId:exceptionId isFatal:isFatal];
};

if ([self respondsToSelector:@selector(extraModulesForBridge:)]) {
configuration.extraModulesForBridge = ^NSArray<id<RCTBridgeModule>> *_Nonnull(RCTBridge *_Nonnull bridge)
{
Expand Down
23 changes: 23 additions & 0 deletions packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ typedef NSURL *_Nullable (^RCTBundleURLBlock)(void);
typedef NSArray<id<RCTBridgeModule>> *_Nonnull (^RCTExtraModulesForBridgeBlock)(RCTBridge *bridge);
typedef NSDictionary<NSString *, Class> *_Nonnull (^RCTExtraLazyModuleClassesForBridge)(RCTBridge *bridge);
typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *moduleName);
typedef void (^RCTHostDidStartBlock)(RCTHost *host);
typedef void (^RCTHostDidReceiveJSErrorStackBlock)(
RCTHost *host,
NSArray<NSDictionary<NSString *, id> *> *stack,
NSString *message,
NSUInteger exceptionId,
BOOL isFatal);

#pragma mark - RCTRootViewFactory Configuration
@interface RCTRootViewFactoryConfiguration : NSObject
Expand Down Expand Up @@ -132,6 +139,22 @@ typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *modu
*/
@property (nonatomic, nullable) RCTBridgeDidNotFindModuleBlock bridgeDidNotFindModule;

/**
* Called when `RCTHost` did started.
* @parameter: host - The started `RCTHost`.
*/
@property (nonatomic, nullable) RCTHostDidStartBlock hostDidStartBlock;

/**
* Called when `RCTHost` received JS error.
* @parameter: host - `RCTHost` which received js error.
* @parameter: stack - JS error stack.
* @parameter: message - Error message.
* @parameter: exceptionId - Exception ID.
* @parameter: isFatal - YES if JS error is fatal.
*/
@property (nonatomic, nullable) RCTHostDidReceiveJSErrorStackBlock hostDidReceiveJSErrorStackBlock;

@end

#pragma mark - RCTRootViewFactory
Expand Down
24 changes: 22 additions & 2 deletions packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ - (instancetype)initWithBundleURLBlock:(RCTBundleURLBlock)bundleURLBlock

@end

@interface RCTRootViewFactory () <RCTContextContainerHandling> {
@interface RCTRootViewFactory () <RCTContextContainerHandling, RCTHostDelegate> {
std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
facebook::react::ContextContainer::Shared _contextContainer;
}
Expand Down Expand Up @@ -180,6 +180,26 @@ - (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
return rootView;
}

#pragma mark - RCTHostDelegate

- (void)hostDidStart:(RCTHost *)host
{
if (self->_configuration.hostDidStartBlock) {
self->_configuration.hostDidStartBlock(host);
}
}

- (void)host:(RCTHost *)host
didReceiveJSErrorStack:(NSArray<NSDictionary<NSString *, id> *> *)stack
message:(NSString *)message
exceptionId:(NSUInteger)exceptionId
isFatal:(BOOL)isFatal
{
if (self->_configuration.hostDidReceiveJSErrorStackBlock) {
self->_configuration.hostDidReceiveJSErrorStackBlock(host, stack, message, exceptionId, isFatal);
}
}

#pragma mark - RCTCxxBridgeDelegate
- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
{
Expand Down Expand Up @@ -238,7 +258,7 @@ - (RCTHost *)createReactHost:(NSDictionary *)launchOptions
__weak __typeof(self) weakSelf = self;
RCTHost *reactHost =
[[RCTHost alloc] initWithBundleURLProvider:self->_configuration.bundleURLBlock
hostDelegate:nil
hostDelegate:self
turboModuleManagerDelegate:_turboModuleManagerDelegate
jsEngineProvider:^std::shared_ptr<facebook::react::JSRuntimeFactory>() {
return [weakSelf createJSRuntimeFactory];
Expand Down
Loading