android: add ReactInstancePackage abstract class to pass down ReactInstanceManager to create modules

Summary:
At times, ReactPackage needs to get information from the ReactInstanceManager, e.g. to get the DevSupportManager for debugging purpose. This allows passing down the instance manager to create the native modules, in addition to just ReactApplicationContext. It is then up to the Package to use it or not.

To use this, you must make your package class extends ReactInstancePackage, instead of just implementing ReactPackage interface.

Reviewed By: mmmulani

Differential Revision: D4641997

fbshipit-source-id: 497c4408a7d2b773c49f08bff7c1bf8f9d372edb
This commit is contained in:
Kevin Gozali 2017-03-06 15:15:15 -08:00 committed by Facebook Github Bot
parent 4c79df9970
commit 7acf74122d
5 changed files with 76 additions and 3 deletions

View File

@ -37,7 +37,7 @@ public class ReactTestHelper {
private static class ReactInstanceEasyBuilderImpl implements ReactInstanceEasyBuilder {
private final NativeModuleRegistryBuilder mNativeModuleRegistryBuilder =
new NativeModuleRegistryBuilder(null, false);
new NativeModuleRegistryBuilder(null, null, false);
private final JavaScriptModuleRegistry.Builder mJSModuleRegistryBuilder =
new JavaScriptModuleRegistry.Builder();

View File

@ -25,7 +25,7 @@ import com.facebook.react.uimanager.ViewManager;
* {@code CompositeReactPackage} allows to create a single package composed of views and modules
* from several other packages.
*/
public class CompositeReactPackage implements ReactPackage {
public class CompositeReactPackage extends ReactInstancePackage {
private final List<ReactPackage> mChildReactPackages = new ArrayList<>();
@ -49,6 +49,7 @@ public class CompositeReactPackage implements ReactPackage {
*/
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
// This is for backward compatibility.
final Map<String, NativeModule> moduleMap = new HashMap<>();
for (ReactPackage reactPackage: mChildReactPackages) {
for (NativeModule nativeModule: reactPackage.createNativeModules(reactContext)) {
@ -58,6 +59,31 @@ public class CompositeReactPackage implements ReactPackage {
return new ArrayList(moduleMap.values());
}
/**
* {@inheritDoc}
*/
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext,
ReactInstanceManager reactInstanceManager) {
final Map<String, NativeModule> moduleMap = new HashMap<>();
for (ReactPackage reactPackage: mChildReactPackages) {
List<NativeModule> nativeModules;
if (reactPackage instanceof ReactInstancePackage) {
ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage;
nativeModules = reactInstancePackage.createNativeModules(
reactContext,
reactInstanceManager);
} else {
nativeModules = reactPackage.createNativeModules(reactContext);
}
for (NativeModule nativeModule: nativeModules) {
moduleMap.put(nativeModule.getName(), nativeModule);
}
}
return new ArrayList(moduleMap.values());
}
/**
* {@inheritDoc}
*/

View File

@ -26,6 +26,7 @@ import com.facebook.react.module.model.ReactModuleInfo;
public class NativeModuleRegistryBuilder {
private final ReactApplicationContext mReactApplicationContext;
private final ReactInstanceManager mReactInstanceManager;
private final boolean mLazyNativeModulesEnabled;
private final Map<Class<? extends NativeModule>, ModuleHolder> mModules = new HashMap<>();
@ -33,8 +34,10 @@ public class NativeModuleRegistryBuilder {
public NativeModuleRegistryBuilder(
ReactApplicationContext reactApplicationContext,
ReactInstanceManager reactInstanceManager,
boolean lazyNativeModulesEnabled) {
mReactApplicationContext = reactApplicationContext;
mReactInstanceManager = reactInstanceManager;
mLazyNativeModulesEnabled = lazyNativeModulesEnabled;
}
@ -94,7 +97,16 @@ public class NativeModuleRegistryBuilder {
ReactConstants.TAG,
reactPackage.getClass().getSimpleName() +
" is not a LazyReactPackage, falling back to old version.");
for (NativeModule nativeModule : reactPackage.createNativeModules(mReactApplicationContext)) {
List<NativeModule> nativeModules;
if (reactPackage instanceof ReactInstancePackage) {
ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage;
nativeModules = reactInstancePackage.createNativeModules(
mReactApplicationContext,
mReactInstanceManager);
} else {
nativeModules = reactPackage.createNativeModules(mReactApplicationContext);
}
for (NativeModule nativeModule : nativeModules) {
addNativeModule(nativeModule);
}
}

View File

@ -860,6 +860,7 @@ public class ReactInstanceManager {
final ReactApplicationContext reactContext = new ReactApplicationContext(mApplicationContext);
NativeModuleRegistryBuilder nativeModuleRegistryBuilder = new NativeModuleRegistryBuilder(
reactContext,
this,
mLazyNativeModulesEnabled);
JavaScriptModuleRegistry.Builder jsModulesBuilder = new JavaScriptModuleRegistry.Builder();
if (mUseDeveloperSupport) {

View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react;
import java.util.List;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
/**
* A simple wrapper for ReactPackage to make it aware of its {@link ReactInstanceManager}
* when creating native modules. This is useful when the package needs to ask
* the instance manager for more information, like {@link DevSupportManager}.
*
* TODO(t11394819): Consolidate this with LazyReactPackage
*/
public abstract class ReactInstancePackage implements ReactPackage {
public abstract List<NativeModule> createNativeModules(
ReactApplicationContext reactContext,
ReactInstanceManager reactInstanceManager);
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
throw new RuntimeException("ReactInstancePackage must be passed in the ReactInstanceManager.");
}
}