Skip to content

Commit

Permalink
Add ReactInstanceEventListener for Venice and expose in FbReactInstan…
Browse files Browse the repository at this point in the history
…ceHolder

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
  • Loading branch information
luluwu2032 authored and facebook-github-bot committed Oct 19, 2021
1 parent ba7424d commit ce74aa4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.app.Activity;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import androidx.test.rule.ActivityTestRule;
import com.facebook.react.ReactInstanceEventListener;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactRootView;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void run() {
// This threading garbage will be replaced by Surface
final AtomicBoolean isLayoutUpdated = new AtomicBoolean(false);
mReactInstanceManager.addReactInstanceEventListener(
new ReactInstanceManager.ReactInstanceEventListener() {
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext reactContext) {
final UIManagerModule uiManagerModule =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void startTask(final HeadlessJsTaskConfig taskConfig) {
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
if (reactContext == null) {
reactInstanceManager.addReactInstanceEventListener(
new ReactInstanceManager.ReactInstanceEventListener() {
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext reactContext) {
invokeStartTask(reactContext, taskConfig);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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;

import com.facebook.react.bridge.ReactContext;

/**
* New Listener interface for react instance events. {@Link
* ReactInstanceManager.ReactInstanceEventListener will be deprecated.}
*/
public interface ReactInstanceEventListener {

/**
* Called when the react context is initialized (all modules registered). Always called on the UI
* thread.
*/
void onReactContextInitialized(ReactContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,17 @@
public class ReactInstanceManager {

private static final String TAG = ReactInstanceManager.class.getSimpleName();
/** Listener interface for react instance events. */
public interface ReactInstanceEventListener {

/**
* Called when the react context is initialized (all modules registered). Always called on the
* UI thread.
*/
void onReactContextInitialized(ReactContext context);
}

/**
* Listener interface for react instance events. This class extends {@Link
* com.facebook.react.ReactInstanceEventListener} as a mitigation for both bridgeless and OSS
* compatibility: We create a separate ReactInstanceEventListener class to remove dependency on
* ReactInstanceManager which is a bridge-specific class, but in the mean time we have to keep
* ReactInstanceManager.ReactInstanceEventListener so OSS won't break.
*/
@Deprecated
public interface ReactInstanceEventListener
extends com.facebook.react.ReactInstanceEventListener {}

private final Set<ReactRoot> mAttachedReactRoots =
Collections.synchronizedSet(new HashSet<ReactRoot>());
Expand All @@ -173,8 +175,10 @@ public interface ReactInstanceEventListener {
private final Context mApplicationContext;
private @Nullable @ThreadConfined(UI) DefaultHardwareBackBtnHandler mDefaultBackButtonImpl;
private @Nullable Activity mCurrentActivity;
private final Collection<ReactInstanceEventListener> mReactInstanceEventListeners =
Collections.synchronizedList(new ArrayList<ReactInstanceEventListener>());
private final Collection<com.facebook.react.ReactInstanceEventListener>
mReactInstanceEventListeners =
Collections.synchronizedList(
new ArrayList<com.facebook.react.ReactInstanceEventListener>());
// Identifies whether the instance manager is or soon will be initialized (on background thread)
private volatile boolean mHasStartedCreatingInitialContext = false;
// Identifies whether the instance manager destroy function is in process,
Expand Down Expand Up @@ -1001,12 +1005,14 @@ public List<ViewManager> getOrCreateViewManagers(
}

/** Add a listener to be notified of react instance events. */
public void addReactInstanceEventListener(ReactInstanceEventListener listener) {
public void addReactInstanceEventListener(
com.facebook.react.ReactInstanceEventListener listener) {
mReactInstanceEventListeners.add(listener);
}

/** Remove a listener previously added with {@link #addReactInstanceEventListener}. */
public void removeReactInstanceEventListener(ReactInstanceEventListener listener) {
public void removeReactInstanceEventListener(
com.facebook.react.ReactInstanceEventListener listener) {
mReactInstanceEventListeners.remove(listener);
}

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

// There is a race condition here - `finalListeners` can contain null entries
// See usage below for more details.
ReactInstanceEventListener[] listeners =
new ReactInstanceEventListener[mReactInstanceEventListeners.size()];
final ReactInstanceEventListener[] finalListeners =
com.facebook.react.ReactInstanceEventListener[] listeners =
new com.facebook.react.ReactInstanceEventListener[mReactInstanceEventListeners.size()];
final com.facebook.react.ReactInstanceEventListener[] finalListeners =
mReactInstanceEventListeners.toArray(listeners);

UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
for (ReactInstanceEventListener listener : finalListeners) {
for (com.facebook.react.ReactInstanceEventListener listener : finalListeners) {
// Sometimes this listener is null - probably due to race
// condition between allocating listeners with a certain
// size, and getting a `final` version of the array on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 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.uiapp;

import android.content.Context;
Expand All @@ -19,6 +20,7 @@
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
import com.facebook.flipper.plugins.react.ReactFlipperPlugin;
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
import com.facebook.react.ReactInstanceEventListener;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.modules.network.NetworkingModule;
Expand Down Expand Up @@ -51,7 +53,7 @@ public void apply(OkHttpClient.Builder builder) {
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
if (reactContext == null) {
reactInstanceManager.addReactInstanceEventListener(
new ReactInstanceManager.ReactInstanceEventListener() {
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext reactContext) {
reactInstanceManager.removeReactInstanceEventListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
import com.facebook.flipper.plugins.react.ReactFlipperPlugin;
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
import com.facebook.react.ReactInstanceEventListener;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.modules.network.NetworkingModule;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void apply(OkHttpClient.Builder builder) {
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
if (reactContext == null) {
reactInstanceManager.addReactInstanceEventListener(
new ReactInstanceManager.ReactInstanceEventListener() {
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext reactContext) {
reactInstanceManager.removeReactInstanceEventListener(this);
Expand Down

0 comments on commit ce74aa4

Please sign in to comment.