Refactor TurboModule filtering in NativeModuleRegistryBuilder

Summary:
## Context
`NativeModuleRegistryBuilder` calls `TurboReactPackage.getNativeModuleIterator()` to access ModuleHolders for all the NativeModules in the `TurboReactPackage`. We then filter out the ModuleHolders that contain `TurboModules`, before using that list to make create the `NativeModuleRegistry`.

## Problem
Creating `ModuleHolders` has the side-effect of actually creating the NativeModule if it requires eager initialization. See [ModuleHolder.java](https://fburl.com/diffusion/4avdtio0):

```
class ModuleHolder {
  // ...
  public ModuleHolder(ReactModuleInfo moduleInfo, Provider<? extends NativeModule> provider) {
    mName = moduleInfo.name();
    mProvider = provider;
    mReactModuleInfo = moduleInfo;
    if (moduleInfo.needsEagerInit()) {
      mModule = create(); // HERE!
    }
  }

```

So, we need to filter out TurboModules before we even create ModuleHolders.

Changelog:
[Android][Fixed] - Refactor TurboModule filtering in NativeModuleRegistryBuilder

Reviewed By: PeteTheHeat, mdvacca

Differential Revision: D18814010

fbshipit-source-id: a120d2b619b9280ba70e21d131dccc5a9fc6346d
This commit is contained in:
Ramanpreet Nara
2019-12-05 18:57:16 -08:00
committed by Facebook Github Bot
parent e362470305
commit d9deee20e7
2 changed files with 38 additions and 15 deletions
@@ -10,7 +10,6 @@ package com.facebook.react;
import com.facebook.react.bridge.ModuleHolder;
import com.facebook.react.bridge.NativeModuleRegistry;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.config.ReactFeatureFlags;
import java.util.HashMap;
import java.util.Map;
@@ -59,18 +58,6 @@ public class NativeModuleRegistryBuilder {
}
mModules.remove(existingNativeModule);
}
if (ReactFeatureFlags.useTurboModules && moduleHolder.isTurboModule()) {
// If this module is a TurboModule, and if TurboModules are enabled, don't add this module
// This condition is after checking for overrides, since if there is already a module,
// and we want to override it with a turbo module, we would need to remove the modules thats
// already in the list, and then NOT add the new module, since that will be directly exposed
// Note that is someone uses {@link NativeModuleRegistry#registerModules}, we will NOT check
// for TurboModules - assuming that people wanted to explicitly register native modules
// there
continue;
}
mModules.put(name, moduleHolder);
}
}