Refactored NativeModuleRegistryBuilder

Summary: The logic in NativeModuleRegistryBuilder.processPackage was getting pretty complex with lot of branches. Moved the logic to get actual packages into each of the packages, instead of having it in NativeModuleRegistryBuilder.

Reviewed By: achen1

Differential Revision: D9618140

fbshipit-source-id: 4be82ec65b0bd92f11f8b77044004e10c9d3b1a1
This commit is contained in:
Ram N
2018-09-05 22:53:03 -07:00
committed by Facebook Github Bot
parent b1d205a28f
commit 875f70b8b6
5 changed files with 194 additions and 119 deletions

View File

@@ -5,23 +5,13 @@
package com.facebook.react;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ModuleHolder;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.NativeModuleRegistry;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.module.model.ReactModuleInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Helper class to build NativeModuleRegistry.
*/
/** Helper class to build NativeModuleRegistry. */
public class NativeModuleRegistryBuilder {
private final ReactApplicationContext mReactApplicationContext;
@@ -30,91 +20,43 @@ public class NativeModuleRegistryBuilder {
private final Map<String, ModuleHolder> mModules = new HashMap<>();
public NativeModuleRegistryBuilder(
ReactApplicationContext reactApplicationContext,
ReactInstanceManager reactInstanceManager) {
ReactApplicationContext reactApplicationContext, ReactInstanceManager reactInstanceManager) {
mReactApplicationContext = reactApplicationContext;
mReactInstanceManager = reactInstanceManager;
}
public void processPackage(ReactPackage reactPackage) {
// We use an iterable instead of an iterator here to ensure thread safety, and that this list
// cannot be modified
Iterable<ModuleHolder> moduleHolders;
if (reactPackage instanceof LazyReactPackage) {
LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage;
List<ModuleSpec> moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext);
List<String> eagerNativeModules = lazyReactPackage.getEagerNativeModules();
Map<String, ReactModuleInfo> reactModuleInfoMap =
lazyReactPackage.getReactModuleInfoProvider().getReactModuleInfos();
for (ModuleSpec moduleSpec : moduleSpecs) {
String name = moduleSpec.getName();
ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(name);
ModuleHolder moduleHolder;
if (reactModuleInfo == null || eagerNativeModules.contains(name)) {
NativeModule module;
ReactMarker.logMarker(
ReactMarkerConstants.CREATE_MODULE_START,
name);
try {
module = moduleSpec.getProvider().get();
} finally {
ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END);
}
moduleHolder = new ModuleHolder(module);
} else {
moduleHolder = new ModuleHolder(reactModuleInfo, moduleSpec.getProvider());
}
putModuleTypeAndHolderToModuleMaps(name, moduleHolder);
}
moduleHolders =
((LazyReactPackage) reactPackage).getNativeModuleIterator(mReactApplicationContext);
} else {
FLog.d(
ReactConstants.TAG,
reactPackage.getClass().getSimpleName()
+ " is not a LazyReactPackage, falling back to old version.");
List<NativeModule> nativeModules;
if (reactPackage instanceof ReactInstancePackage) {
ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage;
nativeModules =
reactInstancePackage.createNativeModules(
mReactApplicationContext, mReactInstanceManager);
} else {
nativeModules = reactPackage.createNativeModules(mReactApplicationContext);
}
for (NativeModule nativeModule : nativeModules) {
addNativeModule(nativeModule);
}
}
}
public void addNativeModule(NativeModule nativeModule) {
String name = nativeModule.getName();
putModuleTypeAndHolderToModuleMaps(name, new ModuleHolder(nativeModule));
}
private void putModuleTypeAndHolderToModuleMaps(
String name, ModuleHolder moduleHolder)
throws IllegalStateException {
if (mModules.containsKey(name)) {
ModuleHolder existingNativeModule = mModules.get(name);
if (!moduleHolder.getCanOverrideExistingModule()) {
throw new IllegalStateException(
"Native module "
+ name
+ " tried to override "
+ existingNativeModule.getClassName()
+ " for module name "
+ ". Check the getPackages() method in MainApplication.java, it might be "
+ "that module is being created twice. "
+ "If this was your intention, set canOverrideExistingModule=true");
}
mModules.remove(existingNativeModule);
moduleHolders =
ReactPackageHelper.getNativeModuleIterator(
reactPackage, mReactApplicationContext, mReactInstanceManager);
}
mModules.put(name, moduleHolder);
for (ModuleHolder moduleHolder : moduleHolders) {
String name = moduleHolder.getName();
if (mModules.containsKey(name)) {
ModuleHolder existingNativeModule = mModules.get(name);
if (!moduleHolder.getCanOverrideExistingModule()) {
throw new IllegalStateException(
"Native module "
+ name
+ " tried to override "
+ existingNativeModule.getClassName()
+ " for module name .Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExistingModule=true");
}
mModules.remove(existingNativeModule);
}
mModules.put(name, moduleHolder);
}
}
public NativeModuleRegistry build() {
return new NativeModuleRegistry(
mReactApplicationContext, mModules);
return new NativeModuleRegistry(mReactApplicationContext, mModules);
}
}