From 608cf6fe09c9beea394654bb2f5759c7e0c80c06 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Mon, 10 Jun 2019 10:48:12 -0700 Subject: [PATCH] Make CompositeReactPackage support TurboReactPackage Summary: ## Description CompositeReactPackage will now eagerly initialize all NativeModules inside the TurboReactPackages. Before it would just crash the program because calling `createNativeModules()` on the TurboReactPackage instance. Longer term, we should make CompositeReactPackage a TurboReactPackage. ## Why am I making this change? I made MainReactPackage into a TurboReactPackge. But ExpressWifiTechnician uses MainReactPackage to create a CompositeReactPackage: **java/com/expresswifi/technician/MainActivity.java** ``` protected ReactPackage getPackage() { ReactInstanceManager reactInstanceManager = getReactInstanceManager(); Nullable DevSupportManager devSupportManager = null; if (reactInstanceManager != null) { devSupportManager = reactInstanceManager.getDevSupportManager(); } return new CompositeReactPackage( super.getPackage(), mXWFTechnicianReactPackage, new RNFusedLocationPackage(), mXWFTechnicialMobileConfigPackageProvider.get(devSupportManager)); } ``` **java/com/facebook/catalyst/shell/MainReactActivity.java** ``` public abstract class MainReactActivity extends AbstractReactActivity { protected ReactPackage getPackage() { return new MainReactPackage(); } } ``` Reviewed By: fkgozali Differential Revision: D15738677 fbshipit-source-id: 3220dfe6434de56f2917c77fb21acef4cfc79278 --- .../facebook/react/CompositeReactPackage.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/CompositeReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/CompositeReactPackage.java index d6c13684e01..9450719098e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/CompositeReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/CompositeReactPackage.java @@ -8,6 +8,8 @@ package com.facebook.react; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.module.model.ReactModuleInfo; +import com.facebook.react.module.model.ReactModuleInfoProvider; import com.facebook.react.uimanager.ViewManager; import java.util.ArrayList; import java.util.Collections; @@ -18,6 +20,7 @@ import java.util.ListIterator; import java.util.Map; import java.util.Set; import javax.annotation.Nullable; +import com.facebook.react.TurboReactPackage; /** * {@code CompositeReactPackage} allows to create a single package composed of views and modules @@ -46,6 +49,25 @@ public class CompositeReactPackage implements ViewManagerOnDemandReactPackage, R // This is for backward compatibility. final Map moduleMap = new HashMap<>(); for (ReactPackage reactPackage : mChildReactPackages) { + /** + * For now, we eagerly initialize the NativeModules inside TurboReactPackages. + * Ultimately, we should turn CompositeReactPackage into a TurboReactPackage + * and remove this eager initialization. + * + * TODO: T45627020 + */ + if (reactPackage instanceof TurboReactPackage) { + TurboReactPackage turboReactPackage = (TurboReactPackage)reactPackage; + ReactModuleInfoProvider moduleInfoProvider = turboReactPackage.getReactModuleInfoProvider(); + Map moduleInfos = moduleInfoProvider.getReactModuleInfos(); + + for (final String moduleName : moduleInfos.keySet()) { + moduleMap.put(moduleName, turboReactPackage.getModule(moduleName, reactContext)); + } + + continue; + } + for (NativeModule nativeModule : reactPackage.createNativeModules(reactContext)) { moduleMap.put(nativeModule.getName(), nativeModule); }