Convert SourceCode module to TurboModule

Summary: Converting the SourceCode native module to TurboModules. Checking in the Java class generated from the JS spec and extending it in the module implementation.

Reviewed By: fkgozali

Differential Revision: D17586276

fbshipit-source-id: 3d2080f1280791e81a0366d0aab101d960d11157
This commit is contained in:
Emily Janzer
2019-09-30 18:03:37 -07:00
committed by Facebook Github Bot
parent 3e23a21025
commit 774502510b
3 changed files with 57 additions and 10 deletions
@@ -18,6 +18,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/modules/core:core"),
react_native_target("java/com/facebook/react/modules/debug:interfaces"),
react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
],
)
@@ -0,0 +1,50 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
* directory of this source tree.
*/
package com.facebook.react.modules.debug;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactModuleWithSpec;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
public abstract class NativeSourceCodeSpec extends ReactContextBaseJavaModule
implements ReactModuleWithSpec, TurboModule {
public NativeSourceCodeSpec(ReactApplicationContext reactContext) {
super(reactContext);
}
protected abstract Map<String, Object> getTypedExportedConstants();
@Override
public final @Nullable Map<String, Object> getConstants() {
Map<String, Object> constants = getTypedExportedConstants();
if (ReactBuildConfig.DEBUG || ReactBuildConfig.IS_INTERNAL_BUILD) {
Set<String> obligatoryFlowConstants = new HashSet<>(Arrays.asList("scriptURL"));
Set<String> optionalFlowConstants = new HashSet<>();
Set<String> undeclaredConstants = new HashSet<>(constants.keySet());
undeclaredConstants.removeAll(obligatoryFlowConstants);
undeclaredConstants.removeAll(optionalFlowConstants);
if (!undeclaredConstants.isEmpty()) {
throw new IllegalStateException(
String.format("Native Module Flow doesn't declare constants: %s", undeclaredConstants));
}
undeclaredConstants = obligatoryFlowConstants;
undeclaredConstants.removeAll(constants.keySet());
if (!undeclaredConstants.isEmpty()) {
throw new IllegalStateException(
String.format("Native Module doesn't fill in constants: %s", undeclaredConstants));
}
}
return constants;
}
}
@@ -6,10 +6,8 @@
*/
package com.facebook.react.modules.debug;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.module.annotations.ReactModule;
import java.util.HashMap;
import java.util.Map;
@@ -18,14 +16,12 @@ import java.util.Map;
* Module that exposes the URL to the source code map (used for exception stack trace parsing) to JS
*/
@ReactModule(name = SourceCodeModule.NAME)
public class SourceCodeModule extends BaseJavaModule {
public class SourceCodeModule extends NativeSourceCodeSpec {
public static final String NAME = "SourceCode";
private final ReactContext mReactContext;
public SourceCodeModule(ReactContext reactContext) {
mReactContext = reactContext;
public SourceCodeModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
@@ -34,12 +30,12 @@ public class SourceCodeModule extends BaseJavaModule {
}
@Override
public @Nullable Map<String, Object> getConstants() {
protected Map<String, Object> getTypedExportedConstants() {
HashMap<String, Object> constants = new HashMap<>();
String sourceURL =
Assertions.assertNotNull(
mReactContext.getCatalystInstance().getSourceURL(),
getReactApplicationContext().getCatalystInstance().getSourceURL(),
"No source URL loaded, have you initialised the instance?");
constants.put("scriptURL", sourceURL);