mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
7acf74122d
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
115 lines
3.8 KiB
Java
115 lines
3.8 KiB
Java
/**
|
|
* 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.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import com.facebook.react.bridge.JavaScriptModule;
|
|
import com.facebook.react.bridge.NativeModule;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
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 extends ReactInstancePackage {
|
|
|
|
private final List<ReactPackage> mChildReactPackages = new ArrayList<>();
|
|
|
|
/**
|
|
* The order in which packages are passed matters. It may happen that a NativeModule or
|
|
* or a ViewManager exists in two or more ReactPackages. In that case the latter will win
|
|
* i.e. the latter will overwrite the former. This re-occurrence is detected by
|
|
* comparing a name of a module.
|
|
*/
|
|
public CompositeReactPackage(ReactPackage arg1, ReactPackage arg2, ReactPackage... args) {
|
|
mChildReactPackages.add(arg1);
|
|
mChildReactPackages.add(arg2);
|
|
|
|
for (ReactPackage reactPackage: args) {
|
|
mChildReactPackages.add(reactPackage);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@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)) {
|
|
moduleMap.put(nativeModule.getName(), nativeModule);
|
|
}
|
|
}
|
|
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}
|
|
*/
|
|
@Override
|
|
public List<Class<? extends JavaScriptModule>> createJSModules() {
|
|
final Set<Class<? extends JavaScriptModule>> moduleSet = new HashSet<>();
|
|
for (ReactPackage reactPackage: mChildReactPackages) {
|
|
for (Class<? extends JavaScriptModule> jsModule: reactPackage.createJSModules()) {
|
|
moduleSet.add(jsModule);
|
|
}
|
|
}
|
|
return new ArrayList(moduleSet);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
|
final Map<String, ViewManager> viewManagerMap = new HashMap<>();
|
|
for (ReactPackage reactPackage: mChildReactPackages) {
|
|
for (ViewManager viewManager: reactPackage.createViewManagers(reactContext)) {
|
|
viewManagerMap.put(viewManager.getName(), viewManager);
|
|
}
|
|
}
|
|
return new ArrayList(viewManagerMap.values());
|
|
}
|
|
}
|