From bdc83cb129757d6adc373c11535d78055727e3fa Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 6 Feb 2025 08:55:33 -0800 Subject: [PATCH] Added custom load js block in bridge mode (#48845) Summary: `loadSourceForBridge` is broken after we refactor the appdelegate. So let's add it back. ## Changelog: [IOS] [FIXED] - Added custom load js block in bridge mode Pull Request resolved: https://github.com/facebook/react-native/pull/48845 Test Plan: Custom Appdelegate's `loadSourceForBridge` can be called in bridge mode. Reviewed By: robhogan Differential Revision: D68832046 Pulled By: cipolleschi fbshipit-source-id: dcea791e6d8243fdb2f45a33af175aee1a4e1223 --- .../AppDelegate/RCTReactNativeFactory.mm | 15 +++++++++++++++ .../Libraries/AppDelegate/RCTRootViewFactory.h | 18 ++++++++++++++++++ .../AppDelegate/RCTRootViewFactory.mm | 16 ++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm b/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm index 8c7d51fd81f..cd7a805e2a7 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm @@ -228,6 +228,21 @@ using namespace facebook::react; }; } + if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:onProgress:onComplete:)]) { + configuration.loadSourceForBridgeWithProgress = + ^(RCTBridge *_Nonnull bridge, + RCTSourceLoadProgressBlock _Nonnull onProgress, + RCTSourceLoadBlock _Nonnull loadCallback) { + [weakSelf.delegate loadSourceForBridge:bridge onProgress:onProgress onComplete:loadCallback]; + }; + } + + if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) { + configuration.loadSourceForBridge = ^(RCTBridge *_Nonnull bridge, RCTSourceLoadBlock _Nonnull loadCallback) { + [weakSelf.delegate loadSourceForBridge:bridge withBlock:loadCallback]; + }; + } + return [[RCTRootViewFactory alloc] initWithTurboModuleDelegate:self hostDelegate:self configuration:configuration]; } diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h index d3ce6212fac..20c0b3a39ce 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h +++ b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h @@ -31,6 +31,11 @@ typedef NSURL *_Nullable (^RCTBundleURLBlock)(void); typedef NSArray> *_Nonnull (^RCTExtraModulesForBridgeBlock)(RCTBridge *bridge); typedef NSDictionary *_Nonnull (^RCTExtraLazyModuleClassesForBridge)(RCTBridge *bridge); typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *moduleName); +typedef void (^RCTLoadSourceForBridgeWithProgressBlock)( + RCTBridge *bridge, + RCTSourceLoadProgressBlock onProgress, + RCTSourceLoadBlock loadCallback); +typedef void (^RCTLoadSourceForBridgeBlock)(RCTBridge *bridge, RCTSourceLoadBlock loadCallback); #pragma mark - RCTRootViewFactory Configuration @interface RCTRootViewFactoryConfiguration : NSObject @@ -145,6 +150,19 @@ typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *modu */ @property (nonatomic, nullable) RCTBridgeDidNotFindModuleBlock bridgeDidNotFindModule; +/** + * The bridge will automatically attempt to load the JS source code from the + * location specified by the `sourceURLForBridge:` method, however, if you want + * to handle loading the JS yourself, you can do so by setting this property. + */ +@property (nonatomic, nullable) RCTLoadSourceForBridgeWithProgressBlock loadSourceForBridgeWithProgress; + +/** + * Similar to loadSourceForBridgeWithProgress but without progress + * reporting. + */ +@property (nonatomic, nullable) RCTLoadSourceForBridgeBlock loadSourceForBridge; + @end #pragma mark - RCTRootViewFactory diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm index 5b3f575089b..054540765eb 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm @@ -302,6 +302,22 @@ return NO; } +- (void)loadSourceForBridge:(RCTBridge *)bridge withBlock:(RCTSourceLoadBlock)loadCallback +{ + if (_configuration.loadSourceForBridge != nil) { + _configuration.loadSourceForBridge(bridge, loadCallback); + } +} + +- (void)loadSourceForBridge:(RCTBridge *)bridge + onProgress:(RCTSourceLoadProgressBlock)onProgress + onComplete:(RCTSourceLoadBlock)loadCallback +{ + if (_configuration.loadSourceForBridgeWithProgress != nil) { + _configuration.loadSourceForBridgeWithProgress(bridge, onProgress, loadCallback); + } +} + - (NSURL *)bundleURL { return self->_configuration.bundleURLBlock();