Skip to content

Commit

Permalink
iOS: Attempt to load lazy modules when asked from native
Browse files Browse the repository at this point in the history
Summary:
Previously, asking for an instance of NativeModule from the native side gave `nil` if the lazy modules have not been loaded, which is not consistent with the access from JS. This at least attempts to force load the lazy modules when asked from native.

p.s. one asks for a module by doing `[bridge moduleForClass:[FooBar class]]`.

Reviewed By: spredolac

Differential Revision: D12931640

fbshipit-source-id: 15d2dc574067d3386ef921512ce4bc837749dabd
  • Loading branch information
fkgozali authored and facebook-github-bot committed Nov 7, 2018
1 parent e02a154 commit 1f394fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ - (id)moduleForName:(NSString *)moduleName

- (id)moduleForClass:(Class)moduleClass
{
return [self moduleForName:RCTBridgeModuleNameForClass(moduleClass)];
id module = [self.batchedBridge moduleForClass:moduleClass];
if (!module) {
module = [self moduleForName:RCTBridgeModuleNameForClass(moduleClass)];
}
return module;
}

- (NSArray *)modulesConformingToProtocol:(Protocol *)protocol
Expand Down
15 changes: 15 additions & 0 deletions React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,21 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass
return _moduleDataByName[RCTBridgeModuleNameForClass(moduleClass)].hasInstance;
}

- (id)moduleForClass:(Class)moduleClass
{
NSString *moduleName = RCTBridgeModuleNameForClass(moduleClass);
RCTModuleData *moduleData = _moduleDataByName[moduleName];
if (moduleData) {
return moduleData.instance;
}

// Module may not be loaded yet, so attempt to force load it here.
RCTAssert([moduleClass conformsToProtocol:@protocol(RCTBridgeModule)], @"Asking for a NativeModule that doesn't conform to RCTBridgeModule: %@", NSStringFromClass(moduleClass));
[self registerAdditionalModuleClasses:@[moduleClass]];

return _moduleDataByName[moduleName].instance;
}

- (std::shared_ptr<ModuleRegistry>)_buildModuleRegistryUnlocked
{
if (!self.valid) {
Expand Down

0 comments on commit 1f394fa

Please sign in to comment.