merge module info into ModuleHolder

Reviewed By: javache

Differential Revision: D4512982

fbshipit-source-id: e1272812f95d08f2c3eb834a18da842d425b3edb
This commit is contained in:
Aaron Chiu 2017-02-08 11:13:54 -08:00 committed by Facebook Github Bot
parent 7555ae13d1
commit 9f3e928f83
6 changed files with 43 additions and 86 deletions

View File

@ -14,7 +14,6 @@ import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.OnBatchCompleteListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.cxxbridge.LegacyModuleInfo;
import com.facebook.react.cxxbridge.ModuleHolder;
import com.facebook.react.cxxbridge.NativeModuleRegistry;
import com.facebook.react.module.model.ReactModuleInfo;
@ -58,17 +57,20 @@ public class NativeModuleRegistryBuilder {
throw new IllegalStateException("Native Java module " + type.getSimpleName() +
" should be annotated with @ReactModule and added to a @ReactModuleList.");
}
NativeModule nativeModule = moduleSpec.getProvider().get();
LegacyModuleInfo legacyModuleInfo = new LegacyModuleInfo(type, nativeModule);
moduleHolder = new ModuleHolder(legacyModuleInfo, nativeModule);
moduleHolder = new ModuleHolder(moduleSpec.getProvider().get());
} else {
moduleHolder = new ModuleHolder(reactModuleInfo, moduleSpec.getProvider());
moduleHolder = new ModuleHolder(
reactModuleInfo.name(),
reactModuleInfo.canOverrideExistingModule(),
reactModuleInfo.supportsWebWorkers(),
reactModuleInfo.needsEagerInit(),
moduleSpec.getProvider());
}
String name = moduleHolder.getInfo().name();
String name = moduleHolder.getName();
if (namesToType.containsKey(name)) {
Class<? extends NativeModule> existingNativeModule = namesToType.get(name);
if (!moduleHolder.getInfo().canOverrideExistingModule()) {
if (!moduleHolder.getCanOverrideExistingModule()) {
throw new IllegalStateException("Native module " + type.getSimpleName() +
" tried to override " + existingNativeModule.getSimpleName() + " for module name " +
name + ". If this was your intention, set canOverrideExistingModule=true");
@ -106,8 +108,7 @@ public class NativeModuleRegistryBuilder {
}
namesToType.put(name, type);
LegacyModuleInfo legacyModuleInfo = new LegacyModuleInfo(type, nativeModule);
ModuleHolder moduleHolder = new ModuleHolder(legacyModuleInfo, nativeModule);
ModuleHolder moduleHolder = new ModuleHolder(nativeModule);
mModules.put(type, moduleHolder);
}

View File

@ -67,7 +67,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
@DoNotStrip
public String getName() {
return mModuleHolder.getInfo().name();
return mModuleHolder.getName();
}
@DoNotStrip

View File

@ -1,40 +0,0 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.cxxbridge;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.module.model.Info;
/**
* Module info for non-lazy native modules.
*/
public class LegacyModuleInfo implements Info {
public final Class<?> mType;
public final NativeModule mNativeModule;
public LegacyModuleInfo(Class<?> type, NativeModule nativeModule) {
mType = type;
mNativeModule = nativeModule;
}
@Override
public String name() {
return mNativeModule.getName();
}
@Override
public boolean canOverrideExistingModule() {
return mNativeModule.canOverrideExistingModule();
}
@Override
public boolean supportsWebWorkers() {
return mNativeModule.supportsWebWorkers();
}
@Override
public boolean needsEagerInit() {
return true;
}
}

View File

@ -10,8 +10,6 @@ import java.util.concurrent.ExecutionException;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.common.futures.SimpleSettableFuture;
import com.facebook.react.module.model.Info;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
@ -31,21 +29,33 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
*/
public class ModuleHolder {
private final Info mInfo;
private final String mName;
private final boolean mCanOverrideExistingModule;
private final boolean mSupportsWebWorkers;
private @Nullable Provider<? extends NativeModule> mProvider;
private @Nullable NativeModule mModule;
private boolean mInitializeNeeded;
public ModuleHolder(ReactModuleInfo info, Provider<? extends NativeModule> provider) {
mInfo = info;
public ModuleHolder(
String name,
boolean canOverrideExistingModule,
boolean supportsWebWorkers,
boolean needsEagerInit,
Provider<? extends NativeModule> provider) {
mName = name;
mCanOverrideExistingModule = canOverrideExistingModule;
mSupportsWebWorkers = supportsWebWorkers;
mProvider = provider;
if (mInfo.needsEagerInit()) {
if (needsEagerInit) {
mModule = doCreate();
}
}
public ModuleHolder(LegacyModuleInfo info, NativeModule nativeModule) {
mInfo = info;
public ModuleHolder(NativeModule nativeModule) {
mName = nativeModule.getName();
mCanOverrideExistingModule = nativeModule.canOverrideExistingModule();
mSupportsWebWorkers = nativeModule.supportsWebWorkers();
mModule = nativeModule;
}
@ -63,8 +73,16 @@ public class ModuleHolder {
}
}
public Info getInfo() {
return mInfo;
public String getName() {
return mName;
}
public boolean getCanOverrideExistingModule() {
return mCanOverrideExistingModule;
}
public boolean getSupportsWebWorkers() {
return mSupportsWebWorkers;
}
public synchronized NativeModule getModule() {
@ -81,13 +99,12 @@ public class ModuleHolder {
}
private NativeModule create() {
boolean isEagerModule = mInfo instanceof LegacyModuleInfo;
String name = isEagerModule ? ((LegacyModuleInfo) mInfo).mType.getSimpleName() : mInfo.name();
boolean isEagerModule = mModule != null;
if (!isEagerModule) {
ReactMarker.logMarker(CREATE_MODULE_START);
}
SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createModule")
.arg("name", name)
.arg("name", mName)
.flush();
NativeModule module = assertNotNull(mProvider).get();
if (mInitializeNeeded) {
@ -107,7 +124,7 @@ public class ModuleHolder {
if (module instanceof CxxModuleWrapper) {
section.arg("className", module.getClass().getSimpleName());
} else {
section.arg("name", mInfo.name());
section.arg("name", mName);
}
section.flush();
callInitializeOnUiThread(module);

View File

@ -1,17 +0,0 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.module.model;
/**
* Interface for static information about native modules.
*/
public interface Info {
String name();
boolean canOverrideExistingModule();
boolean supportsWebWorkers();
boolean needsEagerInit();
}

View File

@ -6,7 +6,7 @@ package com.facebook.react.module.model;
* Data holder class holding native module specifications. {@link ReactModuleSpecProcessor} creates
* these so Java modules don't have to be instantiated at React Native start up.
*/
public class ReactModuleInfo implements Info {
public class ReactModuleInfo {
private final String mName;
private final boolean mCanOverrideExistingModule;
@ -24,22 +24,18 @@ public class ReactModuleInfo implements Info {
mNeedsEagerInit = needsEagerInit;
}
@Override
public String name() {
return mName;
}
@Override
public boolean canOverrideExistingModule() {
return mCanOverrideExistingModule;
}
@Override
public boolean supportsWebWorkers() {
return mSupportsWebWorkers;
}
@Override
public boolean needsEagerInit() {
return mNeedsEagerInit;
}