Allow init of Native Module before bridge is initialized [2/N]

Summary:
This may be controversial.

Right now, RelayPrefetcher is initialized [here](https://fburl.com/p01iunr1), after bridge is initialized. I want to create a FBRelayPrefetcherModule instance eagerly (diff 3 in stack), and then pass that into the bridge module registry. This way, native side gets to use RelayPrefetcher before bridge is init, and JS still accesses the same instance of FBRelayPrefetcherModule.

The only other option is drastically change bridge init, to allow passing in some eagerly initialized instances.

Reviewed By: shergin

Differential Revision: D13164277

fbshipit-source-id: b45111cd68d78820e61e4fca7e54a7e8df32a3f0
This commit is contained in:
Peter Argany 2018-11-26 17:26:08 -08:00 committed by Facebook Github Bot
parent fd78eee11b
commit 18f3de9dce
4 changed files with 24 additions and 3 deletions

View File

@ -107,10 +107,15 @@ RCT_EXTERN void RCTRegisterModule(Class);
- (RCTModuleData *)moduleDataForName:(NSString *)moduleName;
/**
* Registers additional classes with the ModuleRegistry.
*/
* Registers additional classes with the ModuleRegistry.
*/
- (void)registerAdditionalModuleClasses:(NSArray<Class> *)newModules;
/**
* Updates the ModuleRegistry with a pre-initialized instance.
*/
- (void)updateModuleWithInstance:(id<RCTBridgeModule>)instance;
/**
* Systrace profiler toggling methods exposed for the RCTDevMenu
*/

View File

@ -361,6 +361,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
}
}
- (void)updateModuleWithInstance:(id<RCTBridgeModule>)instance
{
[self.batchedBridge updateModuleWithInstance:instance];
}
- (void)registerAdditionalModuleClasses:(NSArray<Class> *)modules
{
[self.batchedBridge registerAdditionalModuleClasses:modules];

View File

@ -69,7 +69,7 @@ typedef id<RCTBridgeModule>(^RCTBridgeModuleProvider)(void);
* if it has not already been created. To check if the module instance exists
* without causing it to be created, use `hasInstance` instead.
*/
@property (nonatomic, strong, readonly) id<RCTBridgeModule> instance;
@property (nonatomic, strong, readwrite) id<RCTBridgeModule> instance;
/**
* Returns the module method dispatch queue. Note that this will init both the

View File

@ -547,6 +547,17 @@ struct RCTInstanceCallback : public InstanceCallback {
_moduleRegistryCreated = YES;
}
- (void)updateModuleWithInstance:(id<RCTBridgeModule>)instance;
{
NSString *const moduleName = RCTBridgeModuleNameForClass([instance class]);
if (moduleName) {
RCTModuleData *const moduleData = _moduleDataByName[moduleName];
if (moduleData) {
moduleData.instance = instance;
}
}
}
- (NSArray<RCTModuleData *> *)registerModulesForClasses:(NSArray<Class> *)moduleClasses
{
return [self _registerModulesForClasses:moduleClasses lazilyDiscovered:NO];