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
This commit is contained in:
Ramanpreet Nara
2019-06-10 10:52:46 -07:00
committed by Facebook Github Bot
parent 1f3e4df6a7
commit 608cf6fe09
@@ -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<String, NativeModule> 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<String, ReactModuleInfo> 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);
}