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
@@ -21,6 +21,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEm
/** Module that exposes the user's preferred color scheme. For API >= 29. */
@ReactModule(name = AppearanceModule.NAME)
public class AppearanceModule extends ReactContextBaseJavaModule {
public static final String NAME = "Appearance";
private static final String APPEARANCE_CHANGED_EVENT_NAME = "appearanceChanged";
@@ -85,8 +86,13 @@ public class AppearanceModule extends ReactContextBaseJavaModule {
WritableMap appearancePreferences = Arguments.createMap();
appearancePreferences.putString("colorScheme", colorScheme);
getReactApplicationContext()
.getJSModule(RCTDeviceEventEmitter.class)
.emit(APPEARANCE_CHANGED_EVENT_NAME, appearancePreferences);
ReactApplicationContext reactApplicationContext =
getReactApplicationContextIfActiveOrWarn(NAME, "emitAppearanceChanged");
if (reactApplicationContext != null) {
reactApplicationContext
.getJSModule(RCTDeviceEventEmitter.class)
.emit(APPEARANCE_CHANGED_EVENT_NAME, appearancePreferences);
}
}
}