Removed dependency on ReactInstancePackage

Reviewed By: achen1

Differential Revision: D8926248

fbshipit-source-id: 8ab7f3f0cd7bdecc9b0d2cd560ed5da89075d3ba
This commit is contained in:
Ram N 2018-07-23 10:08:38 -07:00 committed by Facebook Github Bot
parent 84022321c4
commit b938cd524a
2 changed files with 20 additions and 48 deletions

View File

@ -1,10 +1,9 @@
/** /**
* Copyright (c) 2015-present, Facebook, Inc. * Copyright (c) 2015-present, Facebook, Inc.
* *
* This source code is licensed under the MIT license found in the * <p>This source code is licensed under the MIT license found in the LICENSE file in the root
* LICENSE file in the root directory of this source tree. * directory of this source tree.
*/ */
package com.facebook.react; package com.facebook.react;
import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.NativeModule;
@ -24,16 +23,15 @@ import javax.annotation.Nullable;
* {@code CompositeReactPackage} allows to create a single package composed of views and modules * {@code CompositeReactPackage} allows to create a single package composed of views and modules
* from several other packages. * from several other packages.
*/ */
public class CompositeReactPackage extends ReactInstancePackage public class CompositeReactPackage implements ViewManagerOnDemandReactPackage, ReactPackage {
implements ViewManagerOnDemandReactPackage {
private final List<ReactPackage> mChildReactPackages = new ArrayList<>(); private final List<ReactPackage> mChildReactPackages = new ArrayList<>();
/** /**
* The order in which packages are passed matters. It may happen that a NativeModule or * The order in which packages are passed matters. It may happen that a NativeModule or or a
* or a ViewManager exists in two or more ReactPackages. In that case the latter will win * ViewManager exists in two or more ReactPackages. In that case the latter will win i.e. the
* i.e. the latter will overwrite the former. This re-occurrence is detected by * latter will overwrite the former. This re-occurrence is detected by comparing a name of a
* comparing a name of a module. * module.
*/ */
public CompositeReactPackage(ReactPackage arg1, ReactPackage arg2, ReactPackage... args) { public CompositeReactPackage(ReactPackage arg1, ReactPackage arg2, ReactPackage... args) {
mChildReactPackages.add(arg1); mChildReactPackages.add(arg1);
@ -42,54 +40,25 @@ public class CompositeReactPackage extends ReactInstancePackage
Collections.addAll(mChildReactPackages, args); Collections.addAll(mChildReactPackages, args);
} }
/** /** {@inheritDoc} */
* {@inheritDoc}
*/
@Override @Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
// This is for backward compatibility. // This is for backward compatibility.
final Map<String, NativeModule> moduleMap = new HashMap<>(); final Map<String, NativeModule> moduleMap = new HashMap<>();
for (ReactPackage reactPackage: mChildReactPackages) { for (ReactPackage reactPackage : mChildReactPackages) {
for (NativeModule nativeModule: reactPackage.createNativeModules(reactContext)) { for (NativeModule nativeModule : reactPackage.createNativeModules(reactContext)) {
moduleMap.put(nativeModule.getName(), nativeModule); moduleMap.put(nativeModule.getName(), nativeModule);
} }
} }
return new ArrayList<>(moduleMap.values()); return new ArrayList<>(moduleMap.values());
} }
/** /** {@inheritDoc} */
* {@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}
*/
@Override @Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
final Map<String, ViewManager> viewManagerMap = new HashMap<>(); final Map<String, ViewManager> viewManagerMap = new HashMap<>();
for (ReactPackage reactPackage: mChildReactPackages) { for (ReactPackage reactPackage : mChildReactPackages) {
for (ViewManager viewManager: reactPackage.createViewManagers(reactContext)) { for (ViewManager viewManager : reactPackage.createViewManagers(reactContext)) {
viewManagerMap.put(viewManager.getName(), viewManager); viewManagerMap.put(viewManager.getName(), viewManager);
} }
} }
@ -103,8 +72,7 @@ public class CompositeReactPackage extends ReactInstancePackage
for (ReactPackage reactPackage : mChildReactPackages) { for (ReactPackage reactPackage : mChildReactPackages) {
if (reactPackage instanceof ViewManagerOnDemandReactPackage) { if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
List<String> names = List<String> names =
((ViewManagerOnDemandReactPackage) reactPackage) ((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(reactContext);
.getViewManagerNames(reactContext);
if (names != null) { if (names != null) {
uniqueNames.addAll(names); uniqueNames.addAll(names);
} }
@ -115,8 +83,10 @@ public class CompositeReactPackage extends ReactInstancePackage
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @Nullable ViewManager createViewManager(ReactApplicationContext reactContext, String viewManagerName) { public @Nullable ViewManager createViewManager(
ListIterator<ReactPackage> iterator = mChildReactPackages.listIterator(mChildReactPackages.size()); ReactApplicationContext reactContext, String viewManagerName) {
ListIterator<ReactPackage> iterator =
mChildReactPackages.listIterator(mChildReactPackages.size());
while (iterator.hasPrevious()) { while (iterator.hasPrevious()) {
ReactPackage reactPackage = iterator.previous(); ReactPackage reactPackage = iterator.previous();
if (reactPackage instanceof ViewManagerOnDemandReactPackage) { if (reactPackage instanceof ViewManagerOnDemandReactPackage) {

View File

@ -18,7 +18,9 @@ import com.facebook.react.bridge.ReactApplicationContext;
* the instance manager for more information, like {@link DevSupportManager}. * the instance manager for more information, like {@link DevSupportManager}.
* *
* TODO(t11394819): Consolidate this with LazyReactPackage * TODO(t11394819): Consolidate this with LazyReactPackage
* Use {@link ReactPackage} or {@link LazyReactPackage} and inject reactInstanceManager as a part of when plugins are initialized.
*/ */
@Deprecated
public abstract class ReactInstancePackage implements ReactPackage { public abstract class ReactInstancePackage implements ReactPackage {
public abstract List<NativeModule> createNativeModules( public abstract List<NativeModule> createNativeModules(