Skip to content

Commit ce74aa4

Browse files
luluwu2032facebook-github-bot
authored andcommitted
Add ReactInstanceEventListener for Venice and expose in FbReactInstanceHolder
Summary: Add ```ReactInstanceEventListener``` for Venice and migrate Bridge-only callsites from - FbReactInstanceHolder.getReactInstanceManager().addReactInstanceEventListener() - FbReactInstanceHolder.getReactInstanceManager().removeReactInstanceEventListener() To: - FbReactInstanceHolder.addReactInstanceEventListener() - FbReactInstanceHolder.removeReactInstanceEventListener() Changelog: [Android][Changed] - Add ReactInstanceEventListenerV2 for migration Reviewed By: RSNara Differential Revision: D31501785 fbshipit-source-id: e1cd03f07e28fbb995ea0a1bb76400089a461879
1 parent ba7424d commit ce74aa4

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

ReactAndroid/src/androidTest/java/com/facebook/react/testing/rule/ReactNativeTestRule.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.app.Activity;
1111
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
1212
import androidx.test.rule.ActivityTestRule;
13+
import com.facebook.react.ReactInstanceEventListener;
1314
import com.facebook.react.ReactInstanceManager;
1415
import com.facebook.react.ReactPackage;
1516
import com.facebook.react.ReactRootView;
@@ -90,7 +91,7 @@ public void run() {
9091
// This threading garbage will be replaced by Surface
9192
final AtomicBoolean isLayoutUpdated = new AtomicBoolean(false);
9293
mReactInstanceManager.addReactInstanceEventListener(
93-
new ReactInstanceManager.ReactInstanceEventListener() {
94+
new ReactInstanceEventListener() {
9495
@Override
9596
public void onReactContextInitialized(ReactContext reactContext) {
9697
final UIManagerModule uiManagerModule =

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected void startTask(final HeadlessJsTaskConfig taskConfig) {
9999
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
100100
if (reactContext == null) {
101101
reactInstanceManager.addReactInstanceEventListener(
102-
new ReactInstanceManager.ReactInstanceEventListener() {
102+
new ReactInstanceEventListener() {
103103
@Override
104104
public void onReactContextInitialized(ReactContext reactContext) {
105105
invokeStartTask(reactContext, taskConfig);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react;
9+
10+
import com.facebook.react.bridge.ReactContext;
11+
12+
/**
13+
* New Listener interface for react instance events. {@Link
14+
* ReactInstanceManager.ReactInstanceEventListener will be deprecated.}
15+
*/
16+
public interface ReactInstanceEventListener {
17+
18+
/**
19+
* Called when the react context is initialized (all modules registered). Always called on the UI
20+
* thread.
21+
*/
22+
void onReactContextInitialized(ReactContext context);
23+
}

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

+23-17
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,17 @@
138138
public class ReactInstanceManager {
139139

140140
private static final String TAG = ReactInstanceManager.class.getSimpleName();
141-
/** Listener interface for react instance events. */
142-
public interface ReactInstanceEventListener {
143-
144-
/**
145-
* Called when the react context is initialized (all modules registered). Always called on the
146-
* UI thread.
147-
*/
148-
void onReactContextInitialized(ReactContext context);
149-
}
141+
142+
/**
143+
* Listener interface for react instance events. This class extends {@Link
144+
* com.facebook.react.ReactInstanceEventListener} as a mitigation for both bridgeless and OSS
145+
* compatibility: We create a separate ReactInstanceEventListener class to remove dependency on
146+
* ReactInstanceManager which is a bridge-specific class, but in the mean time we have to keep
147+
* ReactInstanceManager.ReactInstanceEventListener so OSS won't break.
148+
*/
149+
@Deprecated
150+
public interface ReactInstanceEventListener
151+
extends com.facebook.react.ReactInstanceEventListener {}
150152

151153
private final Set<ReactRoot> mAttachedReactRoots =
152154
Collections.synchronizedSet(new HashSet<ReactRoot>());
@@ -173,8 +175,10 @@ public interface ReactInstanceEventListener {
173175
private final Context mApplicationContext;
174176
private @Nullable @ThreadConfined(UI) DefaultHardwareBackBtnHandler mDefaultBackButtonImpl;
175177
private @Nullable Activity mCurrentActivity;
176-
private final Collection<ReactInstanceEventListener> mReactInstanceEventListeners =
177-
Collections.synchronizedList(new ArrayList<ReactInstanceEventListener>());
178+
private final Collection<com.facebook.react.ReactInstanceEventListener>
179+
mReactInstanceEventListeners =
180+
Collections.synchronizedList(
181+
new ArrayList<com.facebook.react.ReactInstanceEventListener>());
178182
// Identifies whether the instance manager is or soon will be initialized (on background thread)
179183
private volatile boolean mHasStartedCreatingInitialContext = false;
180184
// Identifies whether the instance manager destroy function is in process,
@@ -1001,12 +1005,14 @@ public List<ViewManager> getOrCreateViewManagers(
10011005
}
10021006

10031007
/** Add a listener to be notified of react instance events. */
1004-
public void addReactInstanceEventListener(ReactInstanceEventListener listener) {
1008+
public void addReactInstanceEventListener(
1009+
com.facebook.react.ReactInstanceEventListener listener) {
10051010
mReactInstanceEventListeners.add(listener);
10061011
}
10071012

10081013
/** Remove a listener previously added with {@link #addReactInstanceEventListener}. */
1009-
public void removeReactInstanceEventListener(ReactInstanceEventListener listener) {
1014+
public void removeReactInstanceEventListener(
1015+
com.facebook.react.ReactInstanceEventListener listener) {
10101016
mReactInstanceEventListeners.remove(listener);
10111017
}
10121018

@@ -1177,16 +1183,16 @@ private void setupReactContext(final ReactApplicationContext reactContext) {
11771183

11781184
// There is a race condition here - `finalListeners` can contain null entries
11791185
// See usage below for more details.
1180-
ReactInstanceEventListener[] listeners =
1181-
new ReactInstanceEventListener[mReactInstanceEventListeners.size()];
1182-
final ReactInstanceEventListener[] finalListeners =
1186+
com.facebook.react.ReactInstanceEventListener[] listeners =
1187+
new com.facebook.react.ReactInstanceEventListener[mReactInstanceEventListeners.size()];
1188+
final com.facebook.react.ReactInstanceEventListener[] finalListeners =
11831189
mReactInstanceEventListeners.toArray(listeners);
11841190

11851191
UiThreadUtil.runOnUiThread(
11861192
new Runnable() {
11871193
@Override
11881194
public void run() {
1189-
for (ReactInstanceEventListener listener : finalListeners) {
1195+
for (com.facebook.react.ReactInstanceEventListener listener : finalListeners) {
11901196
// Sometimes this listener is null - probably due to race
11911197
// condition between allocating listeners with a certain
11921198
// size, and getting a `final` version of the array on

packages/rn-tester/android/app/src/debug/java/com/facebook/react/uiapp/ReactNativeFlipper.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7+
78
package com.facebook.react.uiapp;
89

910
import android.content.Context;
@@ -19,6 +20,7 @@
1920
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
2021
import com.facebook.flipper.plugins.react.ReactFlipperPlugin;
2122
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
23+
import com.facebook.react.ReactInstanceEventListener;
2224
import com.facebook.react.ReactInstanceManager;
2325
import com.facebook.react.bridge.ReactContext;
2426
import com.facebook.react.modules.network.NetworkingModule;
@@ -51,7 +53,7 @@ public void apply(OkHttpClient.Builder builder) {
5153
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
5254
if (reactContext == null) {
5355
reactInstanceManager.addReactInstanceEventListener(
54-
new ReactInstanceManager.ReactInstanceEventListener() {
56+
new ReactInstanceEventListener() {
5557
@Override
5658
public void onReactContextInitialized(ReactContext reactContext) {
5759
reactInstanceManager.removeReactInstanceEventListener(this);

template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
2020
import com.facebook.flipper.plugins.react.ReactFlipperPlugin;
2121
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
22+
import com.facebook.react.ReactInstanceEventListener;
2223
import com.facebook.react.ReactInstanceManager;
2324
import com.facebook.react.bridge.ReactContext;
2425
import com.facebook.react.modules.network.NetworkingModule;
@@ -51,7 +52,7 @@ public void apply(OkHttpClient.Builder builder) {
5152
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
5253
if (reactContext == null) {
5354
reactInstanceManager.addReactInstanceEventListener(
54-
new ReactInstanceManager.ReactInstanceEventListener() {
55+
new ReactInstanceEventListener() {
5556
@Override
5657
public void onReactContextInitialized(ReactContext reactContext) {
5758
reactInstanceManager.removeReactInstanceEventListener(this);

0 commit comments

Comments
 (0)