Make Catalyst support lazy and non-lazy native modules

Summary:
An application could either have lazy, or non-lazy modules. This diff simply lets the individual reactPackages decide if they should be lazy or not, based on the variable in `ReactInstanceManagerBuilder`.

This diff also removed the method `setLazyNativeModules()` since an app can now have both native and non-native modules.

Reviewed By: achen1

Differential Revision: D8940026

fbshipit-source-id: 0399f4f39ad57f2b03e4dce117a9e2c28c4ed2b1
This commit is contained in:
Ram N 2018-07-27 11:56:19 -07:00 committed by Facebook Github Bot
parent 6b5343019c
commit 059fb2fd81
3 changed files with 57 additions and 24 deletions

View File

@ -41,16 +41,12 @@ public class NativeModuleRegistryBuilder {
boolean lazyNativeModulesEnabled) {
mReactApplicationContext = reactApplicationContext;
mReactInstanceManager = reactInstanceManager;
// TODO T32034141 Remove mLazyNativeModulesEnabled
mLazyNativeModulesEnabled = lazyNativeModulesEnabled;
}
public void processPackage(ReactPackage reactPackage) {
if (mLazyNativeModulesEnabled) {
if (!(reactPackage instanceof LazyReactPackage)) {
throw new IllegalStateException("Lazy native modules requires all ReactPackage to " +
"inherit from LazyReactPackage");
}
if (reactPackage instanceof LazyReactPackage) {
LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage;
List<ModuleSpec> moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext);
Map<Class, ReactModuleInfo> reactModuleInfoMap = lazyReactPackage.getReactModuleInfoProvider()
@ -62,8 +58,10 @@ public class NativeModuleRegistryBuilder {
ModuleHolder moduleHolder;
if (reactModuleInfo == null) {
if (BaseJavaModule.class.isAssignableFrom(type)) {
throw new IllegalStateException("Native Java module " + type.getSimpleName() +
" should be annotated with @ReactModule and added to a @ReactModuleList.");
throw new IllegalStateException(
"Native Java module "
+ type.getSimpleName()
+ " should be annotated with @ReactModule and added to a @ReactModuleList.");
}
NativeModule module;
ReactMarker.logMarker(
@ -84,15 +82,15 @@ public class NativeModuleRegistryBuilder {
}
} else {
FLog.d(
ReactConstants.TAG,
reactPackage.getClass().getSimpleName() +
" is not a LazyReactPackage, falling back to old version.");
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);
nativeModules =
reactInstancePackage.createNativeModules(
mReactApplicationContext, mReactInstanceManager);
} else {
nativeModules = reactPackage.createNativeModules(mReactApplicationContext);
}
@ -108,16 +106,22 @@ public class NativeModuleRegistryBuilder {
putModuleTypeAndHolderToModuleMaps(type, name, new ModuleHolder(nativeModule));
}
private void putModuleTypeAndHolderToModuleMaps(Class<? extends NativeModule> type, String underName,
ModuleHolder moduleHolder) throws IllegalStateException {
private void putModuleTypeAndHolderToModuleMaps(
Class<? extends NativeModule> type, String underName, ModuleHolder moduleHolder)
throws IllegalStateException {
if (namesToType.containsKey(underName)) {
Class<? extends NativeModule> existingNativeModule = namesToType.get(underName);
if (!moduleHolder.getCanOverrideExistingModule()) {
throw new IllegalStateException("Native module " + type.getSimpleName() +
" tried to override " + existingNativeModule.getSimpleName() + " for module name " +
underName + ". Check the getPackages() method in MainApplication.java, it might be " +
"that module is being created twice. " +
"If this was your intention, set canOverrideExistingModule=true");
throw new IllegalStateException(
"Native module "
+ type.getSimpleName()
+ " tried to override "
+ existingNativeModule.getSimpleName()
+ " for module name "
+ underName
+ ". 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);
@ -136,8 +140,6 @@ public class NativeModuleRegistryBuilder {
}
return new NativeModuleRegistry(
mReactApplicationContext,
mModules,
batchCompleteListenerModules);
mReactApplicationContext, mModules, batchCompleteListenerModules);
}
}

View File

@ -63,6 +63,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/views/view:view"),
react_native_target("java/com/facebook/react/views/viewpager:viewpager"),
react_native_target("java/com/facebook/react/views/webview:webview"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("res:shell"),
],
)

View File

@ -12,6 +12,7 @@ import com.facebook.react.animated.NativeAnimatedModule;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.module.annotations.ReactModuleList;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.modules.accessibilityinfo.AccessibilityInfoModule;
import com.facebook.react.modules.appstate.AppStateModule;
@ -71,6 +72,35 @@ import javax.inject.Provider;
/**
* Package defining basic modules and view managers.
*/
@ReactModuleList(nativeModules = {
AccessibilityInfoModule.class,
AppStateModule.class,
BlobModule.class,
FileReaderModule.class,
AsyncStorageModule.class,
CameraRollManager.class,
ClipboardModule.class,
DatePickerDialogModule.class,
DialogModule.class,
FrescoModule.class,
I18nManagerModule.class,
ImageEditingManager.class,
ImageLoaderModule.class,
ImageStoreManager.class,
IntentModule.class,
LocationModule.class,
NativeAnimatedModule.class,
NetworkingModule.class,
NetInfoModule.class,
PermissionsModule.class,
ShareModule.class,
StatusBarModule.class,
TimePickerDialogModule.class,
ToastModule.class,
VibrationModule.class,
WebSocketModule.class,
})
public class MainReactPackage extends LazyReactPackage {
private MainPackageConfig mConfig;