Skip to content

Commit dfa8eb0

Browse files
luluwu2032facebook-github-bot
authored andcommitted
Rename "hasActiveCatalystInstance" to "hasActiveReactInstance" for clarification
Summary: Sometimes ```hasActiveCatalystInstance()``` is used to check if it's safe to access the CatalystInstance, which will still crash in Venice. Previously we mitigate this by changing ```reactContext.hasActiveCatalystInstance()``` to ```reactContext.hasActiveCatalystInstance() || reactContext.isBridgeless()```. To solve this for all and good the plan is: 1, Rename ```hasActiveCatalystInstance()``` to ```hasActiveReactInstance()``` so it won't sounds like CatalystInstance-only. 2, Implement hasActiveReactInstance() for Venice. D27343867 3, Remove previous mitigation. D27343952 This diff is the first step, by xbgs there are **58** non-generated callsites of ```hasActiveCatalystInstance()``` in code base which are all renamed in this diff. Changelog: [Android][Changed] - Rename "hasActiveCatalystInstance" to "hasActiveReactInstance" Reviewed By: mdvacca Differential Revision: D27335055 fbshipit-source-id: 5b8ff5e09b79a492e910bb8f197e70fa1360bcef
1 parent b86e52a commit dfa8eb0

File tree

19 files changed

+27
-26
lines changed

19 files changed

+27
-26
lines changed

ReactAndroid/src/androidTest/java/com/facebook/react/testing/idledetection/ReactIdleDetectionUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void doFrame(long frameTimeNanos) {
7979
}
8080

8181
private static void waitForJSIdle(ReactContext reactContext) {
82-
if (!reactContext.hasActiveCatalystInstance()) {
82+
if (!reactContext.hasActiveReactInstance()) {
8383
return;
8484
}
8585
final CountDownLatch latch = new CountDownLatch(1);

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public void onNewIntent(Intent intent) {
518518

519519
private void toggleElementInspector() {
520520
ReactContext currentContext = getCurrentReactContext();
521-
if (currentContext != null && currentContext.hasActiveCatalystInstance()) {
521+
if (currentContext != null && currentContext.hasActiveReactInstance()) {
522522
currentContext
523523
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
524524
.emit("toggleElementInspector", null);
@@ -859,7 +859,7 @@ public void detachRootView(ReactRoot reactRoot) {
859859
if (mAttachedReactRoots.contains(reactRoot)) {
860860
ReactContext currentContext = getCurrentReactContext();
861861
mAttachedReactRoots.remove(reactRoot);
862-
if (currentContext != null && currentContext.hasActiveCatalystInstance()) {
862+
if (currentContext != null && currentContext.hasActiveReactInstance()) {
863863
detachViewFromInstance(reactRoot, currentContext.getCatalystInstance());
864864
}
865865
}
@@ -894,7 +894,7 @@ public List<ViewManager> getOrCreateViewManagers(
894894
ReactApplicationContext context;
895895
synchronized (mReactContextLock) {
896896
context = (ReactApplicationContext) getCurrentReactContext();
897-
if (context == null || !context.hasActiveCatalystInstance()) {
897+
if (context == null || !context.hasActiveReactInstance()) {
898898
return null;
899899
}
900900
}
@@ -920,7 +920,7 @@ public List<ViewManager> getOrCreateViewManagers(
920920
ReactApplicationContext context;
921921
synchronized (mReactContextLock) {
922922
context = (ReactApplicationContext) getCurrentReactContext();
923-
if (context == null || !context.hasActiveCatalystInstance()) {
923+
if (context == null || !context.hasActiveReactInstance()) {
924924
return null;
925925
}
926926
}

ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ public CatalystInstance getCatalystInstance() {
170170
return Assertions.assertNotNull(mCatalystInstance);
171171
}
172172

173-
public boolean hasActiveCatalystInstance() {
173+
/** @return true if there is an non-null, alive react native instance */
174+
public boolean hasActiveReactInstance() {
174175
return mCatalystInstance != null && !mCatalystInstance.isDestroyed();
175176
}
176177

@@ -184,7 +185,7 @@ public LifecycleState getLifecycleState() {
184185

185186
public void addLifecycleEventListener(final LifecycleEventListener listener) {
186187
mLifecycleEventListeners.add(listener);
187-
if (hasActiveCatalystInstance() || isBridgeless()) {
188+
if (hasActiveReactInstance() || isBridgeless()) {
188189
switch (mLifecycleState) {
189190
case BEFORE_CREATE:
190191
case BEFORE_RESUME:
@@ -452,7 +453,7 @@ public JavaScriptContextHolder getJavaScriptContextHolder() {
452453
}
453454

454455
public @Nullable JSIModule getJSIModule(JSIModuleType moduleType) {
455-
if (!hasActiveCatalystInstance()) {
456+
if (!hasActiveReactInstance()) {
456457
throw new IllegalStateException(
457458
"Unable to retrieve a JSIModule if CatalystInstance is not active.");
458459
}

ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContextBaseJavaModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected final ReactApplicationContext getReactApplicationContext() {
5252
*/
5353
@ThreadConfined(ANY)
5454
protected @Nullable final ReactApplicationContext getReactApplicationContextIfActiveOrWarn() {
55-
if (mReactApplicationContext.hasActiveCatalystInstance()
55+
if (mReactApplicationContext.hasActiveReactInstance()
5656
|| mReactApplicationContext.isBridgeless()) {
5757
return mReactApplicationContext;
5858
}

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ public void run() {
915915

916916
@Nullable ReactContext context = mCurrentContext;
917917
if (context == null
918-
|| (!context.isBridgeless() && !context.hasActiveCatalystInstance())) {
918+
|| (!context.isBridgeless() && !context.hasActiveReactInstance())) {
919919
return;
920920
}
921921

ReactAndroid/src/main/java/com/facebook/react/jstasks/HeadlessJsTaskContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private synchronized void startTask(final HeadlessJsTaskConfig taskConfig, int t
107107
}
108108
mActiveTasks.add(taskId);
109109
mActiveTaskConfigs.put(taskId, new HeadlessJsTaskConfig(taskConfig));
110-
if (reactContext.hasActiveCatalystInstance()) {
110+
if (reactContext.hasActiveReactInstance()) {
111111
reactContext
112112
.getJSModule(AppRegistry.class)
113113
.startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());

ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void onChange(boolean selfChange) {
5656

5757
@Override
5858
public void onChange(boolean selfChange, Uri uri) {
59-
if (getReactApplicationContext().hasActiveCatalystInstance()) {
59+
if (getReactApplicationContext().hasActiveReactInstance()) {
6060
AccessibilityInfoModule.this.updateAndSendReduceMotionChangeEvent();
6161
}
6262
}

ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void sendEvent(String eventName, @Nullable Object data) {
100100
// We don't gain anything interesting from logging here, and it's an extremely common
101101
// race condition for an AppState event to be triggered as the Catalyst instance is being
102102
// set up or torn down. So, just fail silently here.
103-
if (!reactApplicationContext.hasActiveCatalystInstance()) {
103+
if (!reactApplicationContext.hasActiveReactInstance()) {
104104
return;
105105
}
106106
reactApplicationContext.getJSModule(RCTDeviceEventEmitter.class).emit(eventName, data);

ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public DatePickerDialogListener(final Promise promise) {
6161

6262
@Override
6363
public void onDateSet(DatePicker view, int year, int month, int day) {
64-
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
64+
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
6565
WritableMap result = new WritableNativeMap();
6666
result.putString("action", ACTION_DATE_SET);
6767
result.putInt("year", year);
@@ -74,7 +74,7 @@ public void onDateSet(DatePicker view, int year, int month, int day) {
7474

7575
@Override
7676
public void onDismiss(DialogInterface dialog) {
77-
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
77+
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
7878
WritableMap result = new WritableNativeMap();
7979
result.putString("action", ACTION_DISMISSED);
8080
mPromise.resolve(result);

ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void emitUpdateDimensionsEvent() {
8383
return;
8484
}
8585

86-
if (mReactApplicationContext.hasActiveCatalystInstance()) {
86+
if (mReactApplicationContext.hasActiveReactInstance()) {
8787
// Don't emit an event to JS if the dimensions haven't changed
8888
WritableNativeMap displayMetrics =
8989
DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale);

ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public AlertFragmentListener(Callback callback) {
130130
public void onClick(DialogInterface dialog, int which) {
131131
if (!mCallbackConsumed) {
132132
if (getReactApplicationContext().isBridgeless()
133-
|| getReactApplicationContext().hasActiveCatalystInstance()) {
133+
|| getReactApplicationContext().hasActiveReactInstance()) {
134134
mCallback.invoke(ACTION_BUTTON_CLICKED, which);
135135
mCallbackConsumed = true;
136136
}
@@ -141,7 +141,7 @@ public void onClick(DialogInterface dialog, int which) {
141141
public void onDismiss(DialogInterface dialog) {
142142
if (!mCallbackConsumed) {
143143
if (getReactApplicationContext().isBridgeless()
144-
|| getReactApplicationContext().hasActiveCatalystInstance()) {
144+
|| getReactApplicationContext().hasActiveReactInstance()) {
145145
mCallback.invoke(ACTION_DISMISSED);
146146
mCallbackConsumed = true;
147147
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public boolean performAccessibilityAction(View host, int action, Bundle args) {
292292
final WritableMap event = Arguments.createMap();
293293
event.putString("actionName", mAccessibilityActionsMap.get(action));
294294
ReactContext reactContext = (ReactContext) host.getContext();
295-
if (reactContext.hasActiveCatalystInstance()) {
295+
if (reactContext.hasActiveReactInstance()) {
296296
final int reactTag = host.getId();
297297
final int surfaceId = UIManagerHelper.getSurfaceId(reactContext);
298298
UIManager uiManager = UIManagerHelper.getUIManager(reactContext, reactTag);

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private static UIManager getUIManager(
7272
}
7373
// TODO T60461551: add tests to verify emission of events when the ReactContext is being turn
7474
// down.
75-
if (!context.hasActiveCatalystInstance()) {
75+
if (!context.hasActiveReactInstance()) {
7676
ReactSoftException.logSoftException(
7777
"UIManagerHelper",
7878
new ReactNoCrashSoftException(

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private RCTEventEmitter getEventEmitter(int reactTag) {
8989
int type = ViewUtil.getUIManagerType(reactTag);
9090
assert type == UIManagerType.DEFAULT;
9191
if (mRCTEventEmitter == null) {
92-
if (mReactContext.hasActiveCatalystInstance()) {
92+
if (mReactContext.hasActiveReactInstance()) {
9393
mRCTEventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
9494
} else {
9595
ReactSoftException.logSoftException(

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public long measure(
111111
text, layout, sTextPaintInstance, themedReactContext);
112112
WritableMap event = Arguments.createMap();
113113
event.putArray("lines", lines);
114-
if (themedReactContext.hasActiveCatalystInstance()) {
114+
if (themedReactContext.hasActiveReactInstance()) {
115115
themedReactContext
116116
.getJSModule(RCTEventEmitter.class)
117117
.receiveEvent(getReactTag(), "topTextLayout", event);

ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
9090
mFrameTimeNanos = INITIAL_FRAME_TIME_NANOS;
9191

9292
mReactApplicationContextMock = mock(ReactApplicationContext.class);
93-
PowerMockito.when(mReactApplicationContextMock.hasActiveCatalystInstance())
93+
PowerMockito.when(mReactApplicationContextMock.hasActiveReactInstance())
9494
.thenAnswer(
9595
new Answer<Boolean>() {
9696
@Override

ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void setUp() throws Exception {
6060
mActivity = mActivityController.create().start().resume().get();
6161

6262
final ReactApplicationContext context = PowerMockito.mock(ReactApplicationContext.class);
63-
PowerMockito.when(context.hasActiveCatalystInstance()).thenReturn(true);
63+
PowerMockito.when(context.hasActiveReactInstance()).thenReturn(true);
6464
PowerMockito.when(context, "getCurrentActivity").thenReturn(mActivity);
6565

6666
mDialogModule = new DialogModule(context);

ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
9696
CatalystInstance reactInstance = mock(CatalystInstance.class);
9797
ReactApplicationContext reactContext = mock(ReactApplicationContext.class);
9898
when(reactContext.getCatalystInstance()).thenReturn(reactInstance);
99-
when(reactContext.hasActiveCatalystInstance()).thenReturn(true);
99+
when(reactContext.hasActiveReactInstance()).thenReturn(true);
100100
when(reactContext.getJSModule(any(Class.class))).thenReturn(mEmitter);
101101
mNetworkingModule = new NetworkingModule(reactContext, "", mHttpClient);
102102
}

ReactAndroid/src/test/java/com/facebook/react/modules/timing/TimingModuleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
7575
CatalystInstance reactInstance = mock(CatalystInstance.class);
7676
ReactApplicationContext reactContext = mock(ReactApplicationContext.class);
7777
when(reactContext.getCatalystInstance()).thenReturn(reactInstance);
78-
when(reactContext.hasActiveCatalystInstance()).thenReturn(true);
78+
when(reactContext.hasActiveReactInstance()).thenReturn(true);
7979

8080
mCurrentTimeNs = 0;
8181
mPostFrameCallbackHandler = new PostFrameCallbackHandler();

0 commit comments

Comments
 (0)