Use getReactApplicationContextIfActiveOrWarn in modules that access JS or Native modules through ReactApplicationContext

Summary:
In D18032458 we introduce `getReactApplicationContextIfActiveOrWarn`. In this diff, modules that access a JS or Native module through ReactApplicationContext need to check if the CatalystInstance is still alive before continuing.

Changelog: [Internal]

Reviewed By: furdei

Differential Revision: D18032788

fbshipit-source-id: 5152783afd0b93b8ce0970fe4a509ea71396a54a
This commit is contained in:
Joshua Gross
2019-10-21 15:59:21 -07:00
committed by Facebook Github Bot
parent b12a29cfcb
commit 2ea33044bd
14 changed files with 291 additions and 300 deletions
@@ -19,6 +19,8 @@ import java.io.File;
// requires it to already be initialized, thus we eagerly initialize this module
@ReactModule(name = "JSCHeapCapture", needsEagerInit = true)
public class JSCHeapCapture extends ReactContextBaseJavaModule {
public static final String TAG = JSCHeapCapture.class.getSimpleName();
public interface HeapCapture extends JavaScriptModule {
void captureHeap(String path);
}
@@ -54,13 +56,18 @@ public class JSCHeapCapture extends ReactContextBaseJavaModule {
File f = new File(path + "/capture.json");
f.delete();
HeapCapture heapCapture = getReactApplicationContext().getJSModule(HeapCapture.class);
if (heapCapture == null) {
callback.onFailure(new CaptureException("Heap capture js module not registered."));
return;
ReactApplicationContext reactApplicationContext =
getReactApplicationContextIfActiveOrWarn(TAG, "captureHeap");
if (reactApplicationContext != null) {
HeapCapture heapCapture = reactApplicationContext.getJSModule(HeapCapture.class);
if (heapCapture == null) {
callback.onFailure(new CaptureException("Heap capture js module not registered."));
return;
}
mCaptureInProgress = callback;
heapCapture.captureHeap(f.getPath());
}
mCaptureInProgress = callback;
heapCapture.captureHeap(f.getPath());
}
@ReactMethod
@@ -20,7 +20,6 @@ import java.util.Map;
@ReactModule(name = JSDevSupport.MODULE_NAME)
public class JSDevSupport extends ReactContextBaseJavaModule {
public static final String MODULE_NAME = "JSDevSupport";
public static final int ERROR_CODE_EXCEPTION = 0;
@@ -55,8 +54,14 @@ public class JSDevSupport extends ReactContextBaseJavaModule {
}
public synchronized void getJSHierarchy(int reactTag, DevSupportCallback callback) {
JSDevSupportModule jsDevSupportModule =
getReactApplicationContext().getJSModule(JSDevSupportModule.class);
ReactApplicationContext reactApplicationContext =
getReactApplicationContextIfActiveOrWarn(MODULE_NAME, "getJSHierarchy");
JSDevSupportModule jsDevSupportModule = null;
if (reactApplicationContext != null) {
jsDevSupportModule = reactApplicationContext.getJSModule(JSDevSupportModule.class);
}
if (jsDevSupportModule == null) {
callback.onFailure(
ERROR_CODE_EXCEPTION,