From 059fb2fd8114652b0b230499781fa64f30495c54 Mon Sep 17 00:00:00 2001 From: Ram N Date: Fri, 27 Jul 2018 11:56:19 -0700 Subject: [PATCH] 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 --- .../react/NativeModuleRegistryBuilder.java | 50 ++++++++++--------- .../main/java/com/facebook/react/shell/BUCK | 1 + .../react/shell/MainReactPackage.java | 30 +++++++++++ 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java b/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java index 85097e7a6..5a3e3564d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java @@ -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 moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext); Map 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 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 type, String underName, - ModuleHolder moduleHolder) throws IllegalStateException { + private void putModuleTypeAndHolderToModuleMaps( + Class type, String underName, ModuleHolder moduleHolder) + throws IllegalStateException { if (namesToType.containsKey(underName)) { Class 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); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK index c7293b6cd..121b765b0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK @@ -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"), ], ) diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java index 5e1ebd5c3..df65bb5b6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java @@ -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;