Convert most packages to use LazyReactPackage

Reviewed By: astreet

Differential Revision: D3334261

fbshipit-source-id: 8533e3dd9395ffb2fd8c9b71a0d352fe475d1cb9
This commit is contained in:
Aaron Chiu 2016-08-11 08:53:56 -07:00 committed by Facebook Github Bot 7
parent 96e41218ed
commit 541eaef4be
4 changed files with 244 additions and 64 deletions

View File

@ -45,7 +45,7 @@ public class CompositeLazyReactPackage extends LazyReactPackage {
// This would require us to use ModuleHolder here
final Map<Class<?>, ModuleSpec> moduleMap = new HashMap<>();
for (LazyReactPackage reactPackage: mChildReactPackages) {
for (ModuleSpec module: reactPackage.getNativeModules(reactContext)) {
for (ModuleSpec module : reactPackage.getNativeModules(reactContext)) {
moduleMap.put(module.getType(), module);
}
}

View File

@ -9,20 +9,23 @@
package com.facebook.react;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.devsupport.HMRClient;
import com.facebook.react.devsupport.JSCHeapCapture;
import com.facebook.react.devsupport.JSCSamplingProfiler;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.ExceptionsManagerModule;
import com.facebook.react.devsupport.HMRClient;
import com.facebook.react.modules.core.JSTimersExecution;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
import com.facebook.react.modules.core.Timing;
@ -42,7 +45,7 @@ import com.facebook.systrace.Systrace;
* require special integration with other framework parts (e.g. with the list of packages to load
* view managers from).
*/
/* package */ class CoreModulesPackage implements ReactPackage {
/* package */ class CoreModulesPackage extends LazyReactPackage {
private final ReactInstanceManager mReactInstanceManager;
private final DefaultHardwareBackBtnHandler mHardwareBackBtnHandler;
@ -58,41 +61,85 @@ import com.facebook.systrace.Systrace;
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext catalystApplicationContext) {
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule");
UIManagerModule uiManagerModule;
try {
List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers(
catalystApplicationContext);
uiManagerModule = new UIManagerModule(
catalystApplicationContext,
viewManagersList,
mUIImplementationProvider.createUIImplementation(
catalystApplicationContext,
viewManagersList));
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
List<NativeModule> nativeModulesList = new ArrayList<>(Arrays.<NativeModule>asList(
new AnimationsDebugModule(
catalystApplicationContext,
mReactInstanceManager.getDevSupportManager().getDevSettings()),
new AndroidInfoModule(),
new DeviceEventManagerModule(catalystApplicationContext, mHardwareBackBtnHandler),
new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager()),
new Timing(catalystApplicationContext, mReactInstanceManager.getDevSupportManager()),
new SourceCodeModule(mReactInstanceManager.getSourceUrl()),
uiManagerModule,
new JSCSamplingProfiler(catalystApplicationContext),
new DebugComponentOwnershipModule(catalystApplicationContext)));
public List<ModuleSpec> getNativeModules(final ReactApplicationContext reactContext) {
List<ModuleSpec> moduleSpecList = new ArrayList();
moduleSpecList.add(
new ModuleSpec(AnimationsDebugModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new AnimationsDebugModule(
reactContext,
mReactInstanceManager.getDevSupportManager().getDevSettings());
}
}));
moduleSpecList.add(
new ModuleSpec(AndroidInfoModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new AndroidInfoModule();
}
}));
moduleSpecList.add(
new ModuleSpec(DeviceEventManagerModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new DeviceEventManagerModule(reactContext, mHardwareBackBtnHandler);
}
}));
moduleSpecList.add(
new ModuleSpec(ExceptionsManagerModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager());
}
}));
moduleSpecList.add(
new ModuleSpec(Timing.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new Timing(reactContext, mReactInstanceManager.getDevSupportManager());
}
}));
moduleSpecList.add(
new ModuleSpec(SourceCodeModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new SourceCodeModule(mReactInstanceManager.getSourceUrl());
}
}));
moduleSpecList.add(
new ModuleSpec(UIManagerModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return createUIManager(reactContext);
}
}));
if (ReactBuildConfig.DEBUG) {
nativeModulesList.add(new JSCHeapCapture(catalystApplicationContext));
moduleSpecList.add(
new ModuleSpec(DebugComponentOwnershipModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new DebugComponentOwnershipModule(reactContext);
}
}));
moduleSpecList.add(
new ModuleSpec(JSCHeapCapture.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new JSCHeapCapture(reactContext);
}
}));
moduleSpecList.add(
new ModuleSpec(JSCSamplingProfiler.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new JSCSamplingProfiler(reactContext);
}
}));
}
return nativeModulesList;
return moduleSpecList;
}
@Override
@ -119,4 +166,20 @@ import com.facebook.systrace.Systrace;
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return new ArrayList<>(0);
}
private UIManagerModule createUIManager(ReactApplicationContext reactContext) {
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule");
try {
List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers(
reactContext);
return new UIManagerModule(
reactContext,
viewManagersList,
mUIImplementationProvider.createUIImplementation(
reactContext,
viewManagersList));
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
}

View File

@ -40,7 +40,7 @@ public class ModuleSpec {
try {
return getConstructor(type, EMPTY_SIGNATURE).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
throw new RuntimeException("ModuleSpec with class: " + type.getName(), e);
}
}
});
@ -58,7 +58,7 @@ public class ModuleSpec {
try {
return getConstructor(type, CONTEXT_SIGNATURE).newInstance(context);
} catch (Exception e) {
throw new RuntimeException(e);
throw new RuntimeException("ModuleSpec with class: " + type.getName(), e);
}
}
});

View File

@ -9,13 +9,16 @@
package com.facebook.react.shell;
import javax.inject.Provider;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.facebook.react.ReactPackage;
import com.facebook.react.LazyReactPackage;
import com.facebook.react.animated.NativeAnimatedModule;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.modules.appstate.AppStateModule;
@ -68,35 +71,149 @@ import com.facebook.react.views.webview.ReactWebViewManager;
/**
* Package defining basic modules and view managers.
*/
public class MainReactPackage implements ReactPackage {
public class MainReactPackage extends LazyReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
new AppStateModule(reactContext),
new AsyncStorageModule(reactContext),
new CameraRollManager(reactContext),
new ClipboardModule(reactContext),
new DatePickerDialogModule(reactContext),
new DialogModule(reactContext),
new FrescoModule(reactContext),
new I18nManagerModule(reactContext),
new ImageEditingManager(reactContext),
new ImageLoaderModule(reactContext),
new ImageStoreManager(reactContext),
new IntentModule(reactContext),
new LocationModule(reactContext),
new NativeAnimatedModule(reactContext),
new NetworkingModule(reactContext),
new NetInfoModule(reactContext),
new PermissionsModule(reactContext),
new ShareModule(reactContext),
new StatusBarModule(reactContext),
new TimePickerDialogModule(reactContext),
new ToastModule(reactContext),
new VibrationModule(reactContext),
new WebSocketModule(reactContext)
);
public List<ModuleSpec> getNativeModules(final ReactApplicationContext context) {
return Arrays.asList(
new ModuleSpec(AppStateModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new AppStateModule(context);
}
}),
new ModuleSpec(AsyncStorageModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new AsyncStorageModule(context);
}
}),
new ModuleSpec(CameraRollManager.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new CameraRollManager(context);
}
}),
new ModuleSpec(ClipboardModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ClipboardModule(context);
}
}),
new ModuleSpec(DatePickerDialogModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new DatePickerDialogModule(context);
}
}),
new ModuleSpec(DialogModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new DialogModule(context);
}
}),
new ModuleSpec(FrescoModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new FrescoModule(context);
}
}),
new ModuleSpec(I18nManagerModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new I18nManagerModule(context);
}
}),
new ModuleSpec(ImageEditingManager.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ImageEditingManager(context);
}
}),
new ModuleSpec(ImageLoaderModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ImageLoaderModule(context);
}
}),
new ModuleSpec(ImageStoreManager.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ImageStoreManager(context);
}
}),
new ModuleSpec(IntentModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new IntentModule(context);
}
}),
new ModuleSpec(LocationModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new LocationModule(context);
}
}),
new ModuleSpec(NativeAnimatedModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new NativeAnimatedModule(context);
}
}),
new ModuleSpec(NetworkingModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new NetworkingModule(context);
}
}),
new ModuleSpec(NetInfoModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new NetInfoModule(context);
}
}),
new ModuleSpec(PermissionsModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new PermissionsModule(context);
}
}),
new ModuleSpec(ShareModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ShareModule(context);
}
}),
new ModuleSpec(StatusBarModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new StatusBarModule(context);
}
}),
new ModuleSpec(TimePickerDialogModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new TimePickerDialogModule(context);
}
}),
new ModuleSpec(ToastModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ToastModule(context);
}
}),
new ModuleSpec(VibrationModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new VibrationModule(context);
}
}),
new ModuleSpec(WebSocketModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new WebSocketModule(context);
}
}));
}
@Override