Breaking - remove unused registration of JS modules

Summary: It's now unnecessary to declare which JS modules you want to expose on your package. To upgrade, remove all overrides of `createJSModules` and keeping calling your JS modules as before.

Reviewed By: AaaChiuuu

Differential Revision: D5229259

fbshipit-source-id: 1160826c951433722f1fe0421c1200883ba1a348
This commit is contained in:
Pieter De Baets 2017-06-14 03:33:33 -07:00 committed by Facebook Github Bot
parent 71ea94b51e
commit ce6fb337a1
30 changed files with 15 additions and 203 deletions

View File

@ -40,8 +40,7 @@ public abstract class AbstractScrollViewTestCase extends ReactAppInstrumentation
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
mScrollListenerModule = new ScrollListenerModule();
return super.createReactInstanceSpecForTest()
.addNativeModule(mScrollListenerModule)
.addJSModule(ScrollViewTestModule.class);
.addNativeModule(mScrollListenerModule);
}
// See ScrollViewListenerModule.js

View File

@ -36,11 +36,6 @@ public class InstanceSpecForTestPackage implements ReactPackage {
return mSpecForTest.getExtraNativeModulesForTest();
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return mSpecForTest.getExtraJSModulesForTest();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return mSpecForTest.getExtraViewManagers();

View File

@ -38,11 +38,6 @@ public class ReactInstanceSpecForTest {
return this;
}
public ReactInstanceSpecForTest addJSModule(Class jsClass) {
mJSModuleSpecs.add(jsClass);
return this;
}
public ReactInstanceSpecForTest setPackage(ReactPackage reactPackage) {
mReactPackage = reactPackage;
return this;
@ -57,10 +52,6 @@ public class ReactInstanceSpecForTest {
return mNativeModules;
}
public List<Class<? extends JavaScriptModule>> getExtraJSModulesForTest() {
return mJSModuleSpecs;
}
public ReactPackage getAlternativeReactPackageForTest() {
return mReactPackage;
}

View File

@ -18,7 +18,6 @@ public interface ReactTestFactory {
public static interface ReactInstanceEasyBuilder {
ReactInstanceEasyBuilder setContext(Context context);
ReactInstanceEasyBuilder addNativeModule(NativeModule module);
ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass);
CatalystInstance build();
}

View File

@ -16,6 +16,7 @@ import android.support.test.InstrumentationRegistry;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.NativeModuleRegistryBuilder;
import com.facebook.react.R;
import com.facebook.react.ReactInstanceManager;
@ -39,8 +40,6 @@ public class ReactTestHelper {
private static class DefaultReactTestFactory implements ReactTestFactory {
private static class ReactInstanceEasyBuilderImpl implements ReactInstanceEasyBuilder {
private final JavaScriptModuleRegistry.Builder mJSModuleRegistryBuilder =
new JavaScriptModuleRegistry.Builder();
private NativeModuleRegistryBuilder mNativeModuleRegistryBuilder;
private @Nullable Context mContext;
@ -59,16 +58,11 @@ public class ReactTestHelper {
null,
false);
}
Assertions.assertNotNull(nativeModule);
mNativeModuleRegistryBuilder.addNativeModule(nativeModule);
return this;
}
@Override
public ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass) {
mJSModuleRegistryBuilder.add(moduleInterfaceClass);
return this;
}
@Override
public CatalystInstance build() {
if (mNativeModuleRegistryBuilder == null) {
@ -87,7 +81,6 @@ public class ReactTestHelper {
.setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault())
.setJSExecutor(executor)
.setRegistry(mNativeModuleRegistryBuilder.build())
.setJSModuleRegistry(mJSModuleRegistryBuilder.build())
.setJSBundleLoader(JSBundleLoader.createAssetLoader(
mContext,
"assets://AndroidTestBundle.js",
@ -141,12 +134,6 @@ public class ReactTestHelper {
return this;
}
@Override
public ReactTestFactory.ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass) {
builder.addJSModule(moduleInterfaceClass);
return this;
}
@Override
public CatalystInstance build() {
final CatalystInstance instance = builder.build();

View File

@ -74,9 +74,8 @@ public class CatalystMeasureLayoutTest extends ReactAppInstrumentationTestCase {
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
mAssertModule = new AssertModule();
return new ReactInstanceSpecForTest()
.addNativeModule(mAssertModule)
.addJSModule(MeasureLayoutTestModule.class);
return super.createReactInstanceSpecForTest()
.addNativeModule(mAssertModule);
}
private void waitForBridgeIdleAndVerifyAsserts() {

View File

@ -109,7 +109,6 @@ public class CatalystNativeJSToJavaParametersTestCase extends ReactIntegrationTe
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addNativeModule(mUIManager)
.addJSModule(TestJSToJavaParametersModule.class)
.build();
}

View File

@ -85,7 +85,6 @@ public class CatalystNativeJavaToJSArgumentsTestCase extends ReactIntegrationTes
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addJSModule(TestJavaToJSArgumentsModule.class)
.addNativeModule(mUIManager)
.build();
}

View File

@ -126,7 +126,6 @@ public class CatalystNativeJavaToJSReturnValuesTestCase extends ReactIntegration
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addJSModule(TestJavaToJSReturnValuesModule.class)
.addNativeModule(mUIManager)
.addNativeModule(new TestModule())
.build();

View File

@ -51,10 +51,8 @@ public class CatalystSubviewsClippingTestCase extends ReactAppInstrumentationTes
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
ReactInstanceSpecForTest instanceSpec = new ReactInstanceSpecForTest();
instanceSpec.addJSModule(SubviewsClippingTestModule.class);
instanceSpec.addViewManager(new ClippableViewManager(mEvents));
return instanceSpec;
return super.createReactInstanceSpecForTest()
.addViewManager(new ClippableViewManager(mEvents));
}
/**

View File

@ -99,7 +99,6 @@ public class CatalystUIManagerTestCase extends ReactIntegrationTestCase {
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addJSModule(UIManagerTestModule.class)
.build()
.getJSModule(UIManagerTestModule.class);
}

View File

@ -80,8 +80,7 @@ public class DatePickerDialogTestCase extends ReactAppInstrumentationTestCase {
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
return super.createReactInstanceSpecForTest()
.addNativeModule(mRecordingModule)
.addJSModule(DatePickerDialogTestModule.class);
.addNativeModule(mRecordingModule);
}
@Override

View File

@ -66,7 +66,6 @@ public class JSLocaleTest extends ReactIntegrationTestCase {
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addJSModule(TestJSLocaleModule.class)
.build();
}

View File

@ -90,7 +90,6 @@ public class ProgressBarTestCase extends ReactIntegrationTestCase {
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addJSModule(ProgressBarTestModule.class)
.build();
mRootView = new ReactRootView(getContext());

View File

@ -5,7 +5,7 @@
* 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.tests;
import java.util.ArrayList;
@ -68,7 +68,6 @@ public class ReactPickerTestCase extends ReactAppInstrumentationTestCase {
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
mRecordingModule = new PickerAndroidRecordingModule();
return super.createReactInstanceSpecForTest()
.addJSModule(PickerAndroidTestModule.class)
.addNativeModule(mRecordingModule);
}

View File

@ -55,8 +55,7 @@ public class ReactSwipeRefreshLayoutTestCase extends ReactAppInstrumentationTest
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
return super.createReactInstanceSpecForTest()
.addNativeModule(mRecordingModule)
.addJSModule(SwipeRefreshLayoutTestModule.class);
.addNativeModule(mRecordingModule);
}
public void testRefreshNoScroll() {

View File

@ -73,8 +73,7 @@ public class ShareTestCase extends ReactAppInstrumentationTestCase {
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
return super.createReactInstanceSpecForTest()
.addNativeModule(mRecordingModule)
.addJSModule(ShareTestModule.class);
.addNativeModule(mRecordingModule);
}
@Override

View File

@ -248,7 +248,6 @@ public class TextInputTestCase extends ReactAppInstrumentationTestCase {
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
return super.createReactInstanceSpecForTest()
.addJSModule(TextInputTestModule.class)
.addNativeModule(mRecordingModule);
}

View File

@ -77,8 +77,7 @@ public class TimePickerDialogTestCase extends ReactAppInstrumentationTestCase {
@Override
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
return super.createReactInstanceSpecForTest()
.addNativeModule(mRecordingModule)
.addJSModule(TimePickerDialogTestModule.class);
.addNativeModule(mRecordingModule);
}
@Override

View File

@ -71,7 +71,6 @@ public class ViewRenderingTestCase extends ReactIntegrationTestCase {
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.addJSModule(ViewRenderingTestModule.class)
.build();
mRootView = new ReactRootView(getContext());

View File

@ -84,20 +84,6 @@ public class CompositeReactPackage extends ReactInstancePackage {
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}
*/

View File

@ -12,27 +12,21 @@ 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.bridge.ReactMarker;
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.module.annotations.ReactModuleList;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.modules.appregistry.AppRegistry;
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.modules.core.HeadlessJsTaskSupportModule;
import com.facebook.react.modules.core.JSTimersExecution;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
import com.facebook.react.modules.core.Timing;
import com.facebook.react.modules.debug.AnimationsDebugModule;
import com.facebook.react.modules.debug.SourceCodeModule;
@ -42,7 +36,6 @@ import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.debug.DebugComponentOwnershipModule;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.systrace.Systrace;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END;
@ -187,26 +180,6 @@ import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_
return moduleSpecList;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
List<Class<? extends JavaScriptModule>> jsModules = new ArrayList<>(Arrays.asList(
DeviceEventManagerModule.RCTDeviceEventEmitter.class,
JSTimersExecution.class,
RCTEventEmitter.class,
RCTNativeAppEventEmitter.class,
AppRegistry.class,
com.facebook.react.bridge.Systrace.class,
HMRClient.class));
if (ReactBuildConfig.DEBUG) {
jsModules.add(DebugComponentOwnershipModule.RCTDebugComponentOwnership.class);
jsModules.add(JSCHeapCapture.HeapCapture.class);
jsModules.add(JSCSamplingProfiler.SamplingProfiler.class);
}
return jsModules;
}
@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
// This has to be done via reflection or we break open source.

View File

@ -926,7 +926,6 @@ public class ReactInstanceManager {
reactContext,
this,
mLazyNativeModulesEnabled);
JavaScriptModuleRegistry.Builder jsModulesBuilder = new JavaScriptModuleRegistry.Builder();
if (mUseDeveloperSupport) {
reactContext.setNativeModuleCallExceptionHandler(mDevSupportManager);
}
@ -942,7 +941,7 @@ public class ReactInstanceManager {
mBackBtnHandler,
mUIImplementationProvider,
mLazyViewManagersEnabled);
processPackage(coreModulesPackage, nativeModuleRegistryBuilder, jsModulesBuilder);
processPackage(coreModulesPackage, nativeModuleRegistryBuilder);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
@ -953,7 +952,7 @@ public class ReactInstanceManager {
TRACE_TAG_REACT_JAVA_BRIDGE,
"createAndProcessCustomReactPackage");
try {
processPackage(reactPackage, nativeModuleRegistryBuilder, jsModulesBuilder);
processPackage(reactPackage, nativeModuleRegistryBuilder);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
@ -979,7 +978,6 @@ public class ReactInstanceManager {
ReactQueueConfigurationSpec.createDefault())
.setJSExecutor(jsExecutor)
.setRegistry(nativeModuleRegistry)
.setJSModuleRegistry(jsModulesBuilder.build())
.setJSBundleLoader(jsBundleLoader)
.setNativeModuleCallExceptionHandler(exceptionHandler);
@ -1010,8 +1008,7 @@ public class ReactInstanceManager {
private void processPackage(
ReactPackage reactPackage,
NativeModuleRegistryBuilder nativeModuleRegistryBuilder,
JavaScriptModuleRegistry.Builder jsModulesBuilder) {
NativeModuleRegistryBuilder nativeModuleRegistryBuilder) {
SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "processPackage")
.arg("className", reactPackage.getClass().getSimpleName())
.flush();
@ -1020,9 +1017,6 @@ public class ReactInstanceManager {
}
nativeModuleRegistryBuilder.processPackage(reactPackage);
for (Class<? extends JavaScriptModule> jsModuleClass : reactPackage.createJSModules()) {
jsModulesBuilder.add(jsModuleClass);
}
if (reactPackage instanceof ReactPackageLogger) {
((ReactPackageLogger) reactPackage).endProcessPackage();
}

View File

@ -38,15 +38,6 @@ public interface ReactPackage {
*/
List<NativeModule> createNativeModules(ReactApplicationContext reactContext);
/**
* @return list of JS modules to register with the newly created catalyst instance.
*
* IMPORTANT: Note that only modules that needs to be accessible from the native code should be
* listed here. Also listing a native module here doesn't imply that the JS implementation of it
* will be automatically included in the JS bundle.
*/
List<Class<? extends JavaScriptModule>> createJSModules();
/**
* @return a list of view managers that should be registered with {@link UIManagerModule}
*/

View File

@ -541,10 +541,6 @@ public class CatalystInstanceImpl implements CatalystInstance {
return this;
}
public Builder setJSModuleRegistry(JavaScriptModuleRegistry jsModuleRegistry) {
return this;
}
public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
mJSBundleLoader = jsBundleLoader;
return this;

View File

@ -48,16 +48,6 @@ public final class JavaScriptModuleRegistry {
return (T) interfaceProxy;
}
public static class Builder {
public Builder add(Class<? extends JavaScriptModule> moduleInterfaceClass) {
return this;
}
public JavaScriptModuleRegistry build() {
return new JavaScriptModuleRegistry();
}
}
private static class JavaScriptModuleInvocationHandler implements InvocationHandler {
private final CatalystInstance mCatalystInstance;
private final Class<? extends JavaScriptModule> mModuleInterface;

View File

@ -247,11 +247,6 @@ public class MainReactPackage extends LazyReactPackage {
}));
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
List<ViewManager> viewManagers = new ArrayList<>();

View File

@ -15,7 +15,6 @@ import java.util.HashSet;
import java.util.List;
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;
@ -68,20 +67,6 @@ public class CompositeReactPackageTest {
verify(packageNo3).createNativeModules(reactContext);
}
@Test
public void testThatCreateJSModulesIsCalledOnAllPackages() {
// Given
CompositeReactPackage composite = new CompositeReactPackage(packageNo1, packageNo2, packageNo3);
// When
composite.createJSModules();
// Then
verify(packageNo1).createJSModules();
verify(packageNo2).createJSModules();
verify(packageNo3).createJSModules();
}
@Test
public void testThatCreateViewManagersIsCalledOnAllPackages() {
// Given
@ -176,43 +161,4 @@ public class CompositeReactPackageTest {
assertEquals(expected, actual);
}
// public access level is required by Mockito
public static class JavaScriptModuleNo1 implements JavaScriptModule {};
public static class JavaScriptModuleNo2 implements JavaScriptModule {};
public static class JavaScriptModuleNo3 implements JavaScriptModule {};
@Test
public void testThatCompositeReturnsASumOfJSModules() {
// Given
CompositeReactPackage composite = new CompositeReactPackage(packageNo1, packageNo2);
Class<? extends JavaScriptModule> moduleNo1 = mock(JavaScriptModuleNo1.class).getClass();
Class<? extends JavaScriptModule> moduleNo2 = mock(JavaScriptModuleNo2.class).getClass();
Class<? extends JavaScriptModule> moduleNo3 = mock(JavaScriptModuleNo3.class).getClass();
List<Class<? extends JavaScriptModule>> l1 = new ArrayList<>();
l1.add(moduleNo1);
when(packageNo1.createJSModules()).thenReturn(l1);
List<Class<? extends JavaScriptModule>> l2 = new ArrayList<>();
l2.add(moduleNo2);
l2.add(moduleNo3);
when(packageNo2.createJSModules()).thenReturn(l2);
// When
List<Class<? extends JavaScriptModule>> compositeModules = composite.createJSModules();
// Then
// wrapping lists into sets to be order-independent
List<Class<? extends JavaScriptModule>> l3 = new ArrayList<>();
l3.add(moduleNo1);
l3.add(moduleNo2);
l3.add(moduleNo3);
Set<Class<? extends JavaScriptModule>> expected = new HashSet<>(l3);
Set<Class<? extends JavaScriptModule>> actual = new HashSet<>(compositeModules);
assertEquals(expected, actual);
}
}

View File

@ -102,7 +102,6 @@ The last step within Java is to register the Module; this happens in the `create
package com.facebook.react.modules.toast;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
@ -113,11 +112,6 @@ import java.util.List;
public class AnExampleReactPackage implements ReactPackage {
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();

View File

@ -1,7 +1,6 @@
package com.some.example;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
@ -13,8 +12,6 @@ import java.util.List;
public class SomeExamplePackage implements ReactPackage {
public SomeExamplePackage() {}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
@ -23,11 +20,6 @@ public class SomeExamplePackage implements ReactPackage {
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();