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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Oskar Kwaśniewski
2024-03-21 15:29:25 -07:00
committed by Facebook GitHub Bot
parent 1021448d7c
commit 9d79f05e68
3 changed files with 88 additions and 0 deletions
@@ -260,6 +260,25 @@
return [weakSelf createBridgeWithDelegate:delegate launchOptions:launchOptions];
};
configuration.sourceURLForBridge = ^NSURL *_Nullable(RCTBridge *_Nonnull bridge)
{
return [weakSelf sourceURLForBridge:bridge];
};
configuration.extraModulesForBridge = ^NSArray<id<RCTBridgeModule>> *_Nonnull(RCTBridge *_Nonnull bridge)
{
return [weakSelf extraModulesForBridge:bridge];
};
configuration.extraLazyModuleClassesForBridge = ^NSDictionary<NSString *, Class> *_Nonnull(RCTBridge *_Nonnull bridge)
{
return [weakSelf extraLazyModuleClassesForBridge:bridge];
};
configuration.bridgeDidNotFindModule = ^BOOL(RCTBridge *_Nonnull bridge, NSString *_Nonnull moduleName) {
return [weakSelf bridge:bridge didNotFindModule:moduleName];
};
return [[RCTRootViewFactory alloc] initWithConfiguration:configuration andTurboModuleManagerDelegate:self];
}
@@ -23,6 +23,10 @@ typedef UIView *_Nonnull (
^RCTCreateRootViewWithBridgeBlock)(RCTBridge *bridge, NSString *moduleName, NSDictionary *initProps);
typedef RCTBridge *_Nonnull (
^RCTCreateBridgeWithDelegateBlock)(id<RCTBridgeDelegate> delegate, NSDictionary *launchOptions);
typedef NSURL *_Nullable (^RCTSourceURLForBridgeBlock)(RCTBridge *bridge);
typedef NSArray<id<RCTBridgeModule>> *_Nonnull (^RCTExtraModulesForBridgeBlock)(RCTBridge *bridge);
typedef NSDictionary<NSString *, Class> *_Nonnull (^RCTExtraLazyModuleClassesForBridge)(RCTBridge *bridge);
typedef BOOL (^RCTBridgeDidNotFindModuleBlock)(RCTBridge *bridge, NSString *moduleName);
#pragma mark - RCTRootViewFactory Configuration
@interface RCTRootViewFactoryConfiguration : NSObject
@@ -81,6 +85,44 @@ typedef RCTBridge *_Nonnull (
*/
@property (nonatomic, nullable) RCTCreateBridgeWithDelegateBlock createBridgeWithDelegate;
/**
* Block that returns the location of the JavaScript source file. When running from the packager
* this should be an absolute URL, e.g. `http://localhost:8081/index.ios.bundle`.
* When running from a locally bundled JS file, this should be a `file://` url
* pointing to a path inside the app resources, e.g. `file://.../main.jsbundle`.
*/
@property (nonatomic, nullable) RCTSourceURLForBridgeBlock sourceURLForBridge;
/**
* The bridge initializes any registered RCTBridgeModules automatically, however
* if you wish to instantiate your own module instances, you can return them
* from this block.
*
* Note: You should always return a new instance for each call, rather than
* returning the same instance each time the bridge is reloaded. Module instances
* should not be shared between bridges, and this may cause unexpected behavior.
*
* It is also possible to override standard modules with your own implementations
* by returning a class with the same `moduleName` from this method, but this is
* not recommended in most cases - if the module methods and behavior do not
* match exactly, it may lead to bugs or crashes.
*/
@property (nonatomic, nullable) RCTExtraModulesForBridgeBlock extraModulesForBridge;
/**
* Retrieve the list of lazy-native-modules names for the given bridge.
*/
@property (nonatomic, nullable) RCTExtraLazyModuleClassesForBridge extraLazyModuleClassesForBridge;
/**
* The bridge will call this block when a module been called from JS
* cannot be found among registered modules.
* It should return YES if the module with name 'moduleName' was registered
* in the implementation, and the system must attempt to look for it again among registered.
* If the module was not registered, return NO to prevent further searches.
*/
@property (nonatomic, nullable) RCTBridgeDidNotFindModuleBlock bridgeDidNotFindModule;
@end
#pragma mark - RCTRootViewFactory
@@ -241,11 +241,38 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri
contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
}
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
{
if (_configuration.extraModulesForBridge != nil) {
return _configuration.extraModulesForBridge(bridge);
}
return nil;
}
- (NSDictionary<NSString *, Class> *)extraLazyModuleClassesForBridge:(RCTBridge *)bridge
{
if (_configuration.extraLazyModuleClassesForBridge != nil) {
return _configuration.extraLazyModuleClassesForBridge(bridge);
}
return nil;
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
if (_configuration.sourceURLForBridge != nil) {
return _configuration.sourceURLForBridge(bridge);
}
return [self bundleURL];
}
- (BOOL)bridge:(RCTBridge *)bridge didNotFindModule:(NSString *)moduleName
{
if (_configuration.bridgeDidNotFindModule != nil) {
return _configuration.bridgeDidNotFindModule(bridge, moduleName);
}
return NO;
}
- (NSURL *)bundleURL
{
return self->_configuration.bundleURL;