Skip to content

Commit 1e9ac29

Browse files
zhongwuzwfacebook-github-bot
authored andcommittedFeb 6, 2025·
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: #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
1 parent b54efb8 commit 1e9ac29

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
 

‎packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm

+15
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,21 @@ - (RCTRootViewFactory *)createRCTRootViewFactory
228228
};
229229
}
230230

231+
if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:onProgress:onComplete:)]) {
232+
configuration.loadSourceForBridgeWithProgress =
233+
^(RCTBridge *_Nonnull bridge,
234+
RCTSourceLoadProgressBlock _Nonnull onProgress,
235+
RCTSourceLoadBlock _Nonnull loadCallback) {
236+
[weakSelf.delegate loadSourceForBridge:bridge onProgress:onProgress onComplete:loadCallback];
237+
};
238+
}
239+
240+
if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) {
241+
configuration.loadSourceForBridge = ^(RCTBridge *_Nonnull bridge, RCTSourceLoadBlock _Nonnull loadCallback) {
242+
[weakSelf.delegate loadSourceForBridge:bridge withBlock:loadCallback];
243+
};
244+
}
245+
231246
return [[RCTRootViewFactory alloc] initWithTurboModuleDelegate:self hostDelegate:self configuration:configuration];
232247
}
233248

‎packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.h

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ typedef NSURL *_Nullable (^RCTBundleURLBlock)(void);
3131
typedef NSArray<id<RCTBridgeModule>> *_Nonnull (^RCTExtraModulesForBridgeBlock)(RCTBridge *bridge);
3232
typedef NSDictionary<NSString *, Class> *_Nonnull (^RCTExtraLazyModuleClassesForBridge)(RCTBridge *bridge);
3333
typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *moduleName);
34+
typedef void (^RCTLoadSourceForBridgeWithProgressBlock)(
35+
RCTBridge *bridge,
36+
RCTSourceLoadProgressBlock onProgress,
37+
RCTSourceLoadBlock loadCallback);
38+
typedef void (^RCTLoadSourceForBridgeBlock)(RCTBridge *bridge, RCTSourceLoadBlock loadCallback);
3439

3540
#pragma mark - RCTRootViewFactory Configuration
3641
@interface RCTRootViewFactoryConfiguration : NSObject
@@ -145,6 +150,19 @@ typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *modu
145150
*/
146151
@property (nonatomic, nullable) RCTBridgeDidNotFindModuleBlock bridgeDidNotFindModule;
147152

153+
/**
154+
* The bridge will automatically attempt to load the JS source code from the
155+
* location specified by the `sourceURLForBridge:` method, however, if you want
156+
* to handle loading the JS yourself, you can do so by setting this property.
157+
*/
158+
@property (nonatomic, nullable) RCTLoadSourceForBridgeWithProgressBlock loadSourceForBridgeWithProgress;
159+
160+
/**
161+
* Similar to loadSourceForBridgeWithProgress but without progress
162+
* reporting.
163+
*/
164+
@property (nonatomic, nullable) RCTLoadSourceForBridgeBlock loadSourceForBridge;
165+
148166
@end
149167

150168
#pragma mark - RCTRootViewFactory

‎packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm

+16
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,22 @@ - (BOOL)bridge:(RCTBridge *)bridge didNotFindModule:(NSString *)moduleName
302302
return NO;
303303
}
304304

305+
- (void)loadSourceForBridge:(RCTBridge *)bridge withBlock:(RCTSourceLoadBlock)loadCallback
306+
{
307+
if (_configuration.loadSourceForBridge != nil) {
308+
_configuration.loadSourceForBridge(bridge, loadCallback);
309+
}
310+
}
311+
312+
- (void)loadSourceForBridge:(RCTBridge *)bridge
313+
onProgress:(RCTSourceLoadProgressBlock)onProgress
314+
onComplete:(RCTSourceLoadBlock)loadCallback
315+
{
316+
if (_configuration.loadSourceForBridgeWithProgress != nil) {
317+
_configuration.loadSourceForBridgeWithProgress(bridge, onProgress, loadCallback);
318+
}
319+
}
320+
305321
- (NSURL *)bundleURL
306322
{
307323
return self->_configuration.bundleURLBlock();

0 commit comments

Comments
 (0)
Please sign in to comment.