Skip to content

Commit eed035a

Browse files
Kudookwasniewskicortinico
authored
fix(iOS): add missing forward blocks to RCTRootViewFactory (#43638)
* fix(iOS): add missing forward blocks to RCTRootViewFactory (#43526) Summary: This PR adds missing forwarding blocks to RCTRootViewFactory, currently when a user tries to override `sourceURLForBridge` in AppDelegate it isn't overridden. ## Changelog: [IOS] [FIXED] - add missing forward blocks to RCTRootViewFactory Pull Request resolved: #43526 Test Plan: Override: `extraModulesForBridge`, `extraLazyModuleClassesForBridge`, `bridge didNotFindModule`, `sourceURLForBridge:` methods in AppDelegate and check if they are called on old architecture Reviewed By: philIip Differential Revision: D55186872 Pulled By: cortinico fbshipit-source-id: 5988c7bab1439ccc4885b7337336c1e120ba9ea6 (cherry picked from commit 9d79f05) * Follow-up with Review Feedback on RCTAppDelegate from #43526 (#43607) Summary: Pull Request resolved: #43607 PR #43526 was accidentally merged with several changes excluded. I'm following up on those here. Changelog: [Internal] [Changed] - Follow-up with Review Feedback on RCTAppDelegate from #43526 Reviewed By: dmytrorykun Differential Revision: D55240435 fbshipit-source-id: c296a1e14b7032b211551334ca7b5a6824e8d45c (cherry picked from commit 84c1c6e) --------- Co-authored-by: Oskar Kwaśniewski <[email protected]> Co-authored-by: Nicola Corti <[email protected]>
1 parent f3b9226 commit eed035a

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,48 @@ - (RCTRootViewFactory *)createRCTRootViewFactory
250250
return [weakSelf createBridgeWithDelegate:delegate launchOptions:launchOptions];
251251
};
252252

253+
configuration.sourceURLForBridge = ^NSURL *_Nullable(RCTBridge *_Nonnull bridge)
254+
{
255+
return [weakSelf sourceURLForBridge:bridge];
256+
};
257+
258+
if ([self respondsToSelector:@selector(extraModulesForBridge:)]) {
259+
configuration.extraModulesForBridge = ^NSArray<id<RCTBridgeModule>> *_Nonnull(RCTBridge *_Nonnull bridge)
260+
{
261+
return [weakSelf extraModulesForBridge:bridge];
262+
};
263+
}
264+
265+
if ([self respondsToSelector:@selector(extraLazyModuleClassesForBridge:)]) {
266+
configuration.extraLazyModuleClassesForBridge =
267+
^NSDictionary<NSString *, Class> *_Nonnull(RCTBridge *_Nonnull bridge)
268+
{
269+
return [weakSelf extraLazyModuleClassesForBridge:bridge];
270+
};
271+
}
272+
273+
if ([self respondsToSelector:@selector(bridge:didNotFindModule:)]) {
274+
configuration.bridgeDidNotFindModule = ^BOOL(RCTBridge *_Nonnull bridge, NSString *_Nonnull moduleName) {
275+
return [weakSelf bridge:bridge didNotFindModule:moduleName];
276+
};
277+
}
278+
279+
if ([self respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) {
280+
configuration.loadSourceForBridgeBlock =
281+
^void(RCTBridge *_Nonnull bridge, RCTSourceLoadBlock _Nonnull loadCallback) {
282+
[weakSelf loadSourceForBridge:bridge withBlock:loadCallback];
283+
};
284+
}
285+
286+
if ([self respondsToSelector:@selector(loadSourceForBridge:onProgress:onComplete:)]) {
287+
configuration.loadSourceForBridgeProgressBlock =
288+
^(RCTBridge *_Nonnull bridge,
289+
RCTSourceLoadProgressBlock _Nonnull onProgress,
290+
RCTSourceLoadBlock _Nonnull loadCallback) {
291+
[weakSelf loadSourceForBridge:bridge onProgress:onProgress onComplete:loadCallback];
292+
};
293+
}
294+
253295
return [[RCTRootViewFactory alloc] initWithConfiguration:configuration andTurboModuleManagerDelegate:self];
254296
}
255297

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ typedef UIView *_Nonnull (
2323
^RCTCreateRootViewWithBridgeBlock)(RCTBridge *bridge, NSString *moduleName, NSDictionary *initProps);
2424
typedef RCTBridge *_Nonnull (
2525
^RCTCreateBridgeWithDelegateBlock)(id<RCTBridgeDelegate> delegate, NSDictionary *launchOptions);
26+
typedef NSURL *_Nullable (^RCTSourceURLForBridgeBlock)(RCTBridge *bridge);
27+
typedef NSArray<id<RCTBridgeModule>> *_Nonnull (^RCTExtraModulesForBridgeBlock)(RCTBridge *bridge);
28+
typedef NSDictionary<NSString *, Class> *_Nonnull (^RCTExtraLazyModuleClassesForBridge)(RCTBridge *bridge);
29+
typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *moduleName);
30+
typedef void (^RCTLoadSourceForBridgeBlock)(RCTBridge *bridge, RCTSourceLoadBlock loadCallback);
31+
typedef void (^RCTLoadSourceForBridgeProgressBlock)(
32+
RCTBridge *bridge,
33+
RCTSourceLoadProgressBlock onProgress,
34+
RCTSourceLoadBlock loadCallback);
2635

2736
#pragma mark - RCTRootViewFactory Configuration
2837
@interface RCTRootViewFactoryConfiguration : NSObject
@@ -80,6 +89,56 @@ typedef RCTBridge *_Nonnull (
8089
* @returns: a newly created instance of RCTBridge.
8190
*/
8291
@property (nonatomic, nullable) RCTCreateBridgeWithDelegateBlock createBridgeWithDelegate;
92+
/**
93+
* Block that returns the location of the JavaScript source file. When running from the packager
94+
* this should be an absolute URL, e.g. `http://localhost:8081/index.ios.bundle`.
95+
* When running from a locally bundled JS file, this should be a `file://` url
96+
* pointing to a path inside the app resources, e.g. `file://.../main.jsbundle`.
97+
*/
98+
@property (nonatomic, nullable) RCTSourceURLForBridgeBlock sourceURLForBridge;
99+
100+
/**
101+
* The bridge initializes any registered RCTBridgeModules automatically, however
102+
* if you wish to instantiate your own module instances, you can return them
103+
* from this block.
104+
*
105+
* Note: You should always return a new instance for each call, rather than
106+
* returning the same instance each time the bridge is reloaded. Module instances
107+
* should not be shared between bridges, and this may cause unexpected behavior.
108+
*
109+
* It is also possible to override standard modules with your own implementations
110+
* by returning a class with the same `moduleName` from this method, but this is
111+
* not recommended in most cases - if the module methods and behavior do not
112+
* match exactly, it may lead to bugs or crashes.
113+
*/
114+
@property (nonatomic, nullable) RCTExtraModulesForBridgeBlock extraModulesForBridge;
115+
116+
/**
117+
* Retrieve the list of lazy-native-modules names for the given bridge.
118+
*/
119+
@property (nonatomic, nullable) RCTExtraLazyModuleClassesForBridge extraLazyModuleClassesForBridge;
120+
121+
/**
122+
* The bridge will call this block when a module been called from JS
123+
* cannot be found among registered modules.
124+
* It should return YES if the module with name 'moduleName' was registered
125+
* in the implementation, and the system must attempt to look for it again among registered.
126+
* If the module was not registered, return NO to prevent further searches.
127+
*/
128+
@property (nonatomic, nullable) RCTBridgeDidNotFindModuleBlock bridgeDidNotFindModule;
129+
130+
/**
131+
* The bridge will automatically attempt to load the JS source code from the
132+
* location specified by the `sourceURLForBridge:` method, however, if you want
133+
* to handle loading the JS yourself, you can do so by implementing this method.
134+
*/
135+
@property (nonatomic, nullable) RCTLoadSourceForBridgeProgressBlock loadSourceForBridgeProgressBlock;
136+
137+
/**
138+
* Similar to loadSourceForBridge:onProgress:onComplete: but without progress
139+
* reporting.
140+
*/
141+
@property (nonatomic, nullable) RCTLoadSourceForBridgeBlock loadSourceForBridgeBlock;
83142

84143
@end
85144

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,54 @@ - (void)didCreateContextContainer:(std::shared_ptr<facebook::react::ContextConta
240240
contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
241241
}
242242

243+
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
244+
{
245+
if (_configuration.extraModulesForBridge != nil) {
246+
return _configuration.extraModulesForBridge(bridge);
247+
}
248+
return nil;
249+
}
250+
251+
- (NSDictionary<NSString *, Class> *)extraLazyModuleClassesForBridge:(RCTBridge *)bridge
252+
{
253+
if (_configuration.extraLazyModuleClassesForBridge != nil) {
254+
return _configuration.extraLazyModuleClassesForBridge(bridge);
255+
}
256+
return nil;
257+
}
258+
243259
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
244260
{
261+
if (_configuration.sourceURLForBridge != nil) {
262+
return _configuration.sourceURLForBridge(bridge);
263+
}
245264
return [self bundleURL];
246265
}
247266

267+
- (BOOL)bridge:(RCTBridge *)bridge didNotFindModule:(NSString *)moduleName
268+
{
269+
if (_configuration.bridgeDidNotFindModule != nil) {
270+
return _configuration.bridgeDidNotFindModule(bridge, moduleName);
271+
}
272+
return NO;
273+
}
274+
275+
- (void)loadSourceForBridge:(RCTBridge *)bridge withBlock:(RCTSourceLoadBlock)loadCallback
276+
{
277+
if (_configuration.loadSourceForBridgeBlock != nil) {
278+
_configuration.loadSourceForBridgeBlock(bridge, loadCallback);
279+
}
280+
}
281+
282+
- (void)loadSourceForBridge:(RCTBridge *)bridge
283+
onProgress:(RCTSourceLoadProgressBlock)onProgress
284+
onComplete:(RCTSourceLoadBlock)loadCallback
285+
{
286+
if (_configuration.loadSourceForBridgeProgressBlock != nil) {
287+
_configuration.loadSourceForBridgeProgressBlock(bridge, onProgress, loadCallback);
288+
}
289+
}
290+
248291
- (NSURL *)bundleURL
249292
{
250293
return self->_configuration.bundleURL;

0 commit comments

Comments
 (0)