Permalink
Browse files

iOS: Attempt to load lazy modules when asked from native

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 1f394fa673a876753fdc9ac2cb86a4d4a58cd8cd
Showing with 20 additions and 1 deletion.
  1. +5 −1 React/Base/RCTBridge.m
  2. +15 −0 React/CxxBridge/RCTCxxBridge.mm
@@ -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
@@ -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) {

0 comments on commit 1f394fa

Please sign in to comment.