Refactoring ReactSurface & adding ReactSurfaceImpl (#38167)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38167

1.  Moving out
- `getSurfaceID()`
- `getSurfaceHandler()`
- `getModuleName()`
- `getContext()`
- `clear()`

functions out of `ReactSurface` to interface `ReactSurface` as part of stable APIs.

2. Refactoring usages of `ReactSurface` to rely on interface `ReactSurface`.

3. `ReactSurfaceInterface` -> `ReactSurface`

4. `ReactSurface` -> `ReactSurfaceImpl`

Reviewed By: mdvacca

Differential Revision: D47109982

fbshipit-source-id: ce9fb12b33fcbb5f243f95f9e7dca8662bb64102
This commit is contained in:
Arushi Kesarwani
2023-08-03 16:29:02 -07:00
committed by Facebook GitHub Bot
parent f493adcf4a
commit b47403eb09
10 changed files with 79 additions and 52 deletions
@@ -17,7 +17,7 @@ import com.facebook.infer.annotation.Assertions;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.devsupport.DoubleTapReloadRecognizer;
import com.facebook.react.interfaces.ReactHostInterface;
import com.facebook.react.interfaces.ReactSurfaceInterface;
import com.facebook.react.interfaces.fabric.ReactSurface;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
/**
@@ -37,7 +37,7 @@ public class ReactDelegate {
@Nullable private ReactNativeHost mReactNativeHost;
@Nullable private ReactHostInterface mReactHost;
@Nullable private ReactSurfaceInterface mReactSurface;
@Nullable private ReactSurface mReactSurface;
private boolean mFabricEnabled = false;
@@ -134,7 +134,9 @@ class BridgelessDevSupportManager extends DevSupportManagerBase {
public View createRootView(String appKey) {
Activity currentActivity = getCurrentActivity();
if (currentActivity != null && !reactHost.isSurfaceWithModuleNameAttached(appKey)) {
ReactSurface reactSurface = ReactSurface.createWithView(currentActivity, appKey, null);
ReactSurfaceImpl reactSurface =
ReactSurfaceImpl.createWithView(currentActivity, appKey, null);
reactSurface.attach(reactHost);
reactSurface.start();
@@ -51,9 +51,9 @@ import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.fabric.ComponentFactory;
import com.facebook.react.fabric.FabricUIManager;
import com.facebook.react.interfaces.ReactHostInterface;
import com.facebook.react.interfaces.ReactSurfaceInterface;
import com.facebook.react.interfaces.TaskInterface;
import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler;
import com.facebook.react.interfaces.fabric.ReactSurface;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.UIManagerModule;
@@ -364,9 +364,9 @@ public class ReactHost implements ReactHostInterface {
}
@Override
public ReactSurfaceInterface createSurface(
public ReactSurface createSurface(
Context context, String moduleName, @Nullable Bundle initialProps) {
ReactSurface surface = new ReactSurface(context, moduleName, initialProps);
ReactSurfaceImpl surface = new ReactSurfaceImpl(context, moduleName, initialProps);
surface.attachView(new ReactSurfaceView(context, surface));
surface.attach(this);
return surface;
@@ -45,6 +45,7 @@ import com.facebook.react.fabric.FabricUIManager;
import com.facebook.react.fabric.ReactNativeConfig;
import com.facebook.react.fabric.events.EventBeatManager;
import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler;
import com.facebook.react.interfaces.fabric.ReactSurface;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.JavaTimerManager;
import com.facebook.react.modules.core.ReactChoreographer;
@@ -23,8 +23,8 @@ import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.bridgeless.internal.bolts.Task;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.fabric.SurfaceHandlerBinding;
import com.facebook.react.interfaces.ReactSurfaceInterface;
import com.facebook.react.interfaces.TaskInterface;
import com.facebook.react.interfaces.fabric.ReactSurface;
import com.facebook.react.interfaces.fabric.SurfaceHandler;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.uimanager.events.EventDispatcher;
@@ -34,7 +34,7 @@ import javax.annotation.Nullable;
/** A class responsible for creating and rendering a full-screen React surface. */
@Nullsafe(Nullsafe.Mode.LOCAL)
@ThreadSafe
public class ReactSurface implements ReactSurfaceInterface {
public class ReactSurfaceImpl implements ReactSurface {
private final AtomicReference<ReactSurfaceView> mSurfaceView = new AtomicReference<>(null);
@@ -50,9 +50,9 @@ public class ReactSurface implements ReactSurfaceInterface {
*/
private Context mContext;
public static ReactSurface createWithView(
public static ReactSurfaceImpl createWithView(
Context context, String moduleName, @Nullable Bundle initialProps) {
ReactSurface surface = new ReactSurface(context, moduleName, initialProps);
ReactSurfaceImpl surface = new ReactSurfaceImpl(context, moduleName, initialProps);
surface.attachView(new ReactSurfaceView(context, surface));
return surface;
}
@@ -62,7 +62,7 @@ public class ReactSurface implements ReactSurfaceInterface {
* @param moduleName The string key used to register this surface in JS with SurfaceRegistry
* @param initialProps A Bundle of properties to be passed to the root React component
*/
public ReactSurface(Context context, String moduleName, @Nullable Bundle initialProps) {
public ReactSurfaceImpl(Context context, String moduleName, @Nullable Bundle initialProps) {
this(new SurfaceHandlerBinding(moduleName), context);
NativeMap nativeProps =
@@ -83,7 +83,7 @@ public class ReactSurface implements ReactSurfaceInterface {
}
@VisibleForTesting
ReactSurface(SurfaceHandler surfaceHandler, Context context) {
ReactSurfaceImpl(SurfaceHandler surfaceHandler, Context context) {
mSurfaceHandler = surfaceHandler;
mContext = context;
}
@@ -128,6 +128,7 @@ public class ReactSurface implements ReactSurfaceInterface {
mReactHost.set(null);
}
@Override
public SurfaceHandler getSurfaceHandler() {
return mSurfaceHandler;
}
@@ -176,14 +177,17 @@ public class ReactSurface implements ReactSurfaceInterface {
return host.stopSurface(this);
}
@Override
public int getSurfaceID() {
return mSurfaceHandler.getSurfaceId();
}
@Override
public String getModuleName() {
return mSurfaceHandler.getModuleName();
}
@Override
public void clear() {
UiThreadUtil.runOnUiThread(
() -> {
@@ -222,10 +226,12 @@ public class ReactSurface implements ReactSurfaceInterface {
return mReactHost.get() != null;
}
@Override
public boolean isRunning() {
return mSurfaceHandler.isRunning();
}
@Override
public Context getContext() {
return mContext;
}
@@ -33,7 +33,7 @@ public class ReactSurfaceView extends ReactRootView {
private static final String TAG = "ReactSurfaceView";
private final ReactSurface mSurface;
private final ReactSurfaceImpl mSurface;
private final JSTouchDispatcher mJSTouchDispatcher;
private @Nullable JSPointerDispatcher mJSPointerDispatcher;
@@ -42,7 +42,7 @@ public class ReactSurfaceView extends ReactRootView {
private int mWidthMeasureSpec = 0;
private int mHeightMeasureSpec = 0;
public ReactSurfaceView(Context context, ReactSurface surface) {
public ReactSurfaceView(Context context, ReactSurfaceImpl surface) {
super(context);
mSurface = surface;
mJSTouchDispatcher = new JSTouchDispatcher(this);
@@ -15,6 +15,7 @@ import com.facebook.react.bridge.queue.ReactQueueConfiguration
import com.facebook.react.common.LifecycleState
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.facebook.react.interfaces.fabric.ReactSurface
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
/**
@@ -67,11 +68,7 @@ interface ReactHostInterface {
fun onHostDestroy(activity: Activity?)
/** To be called to create and setup an ReactSurface. */
fun createSurface(
context: Context,
moduleName: String,
initialProps: Bundle?
): ReactSurfaceInterface?
fun createSurface(context: Context, moduleName: String, initialProps: Bundle?): ReactSurface?
/**
* This function can be used to initialize the ReactInstance in a background thread before a
@@ -1,28 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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.interfaces
import android.view.ViewGroup
/** Represents a Surface in React Native. */
interface ReactSurfaceInterface {
// the API of this interface will be completed as we analyze and refactor API of ReactSurface,
// ReactRootView, etc.
// Prerender this surface
fun prerender(): TaskInterface<Void>
// Start running this surface
fun start(): TaskInterface<Void>
// Stop running this surface
fun stop(): TaskInterface<Void>
// Get React root view of this surface
fun getView(): ViewGroup?
}
@@ -0,0 +1,49 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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.interfaces.fabric
import android.content.Context
import android.view.ViewGroup
import com.facebook.react.interfaces.TaskInterface
/** Represents a Surface in React Native. */
interface ReactSurface {
// the API of this interface will be completed as we analyze and refactor API of ReactSurface,
// ReactRootView, etc.
// Returns surface ID of this surface
val surfaceID: Int
// Returns module name of this surface
val moduleName: String
// Returns whether the surface is running or not
val isRunning: Boolean
// Returns surface handler
val surfaceHandler: SurfaceHandler
// Returns React root view of this surface
val view: ViewGroup?
// Returns context associated with the surface
val context: Context
// Prerender this surface
fun prerender(): TaskInterface<Void>
// Start running this surface
fun start(): TaskInterface<Void>
// Stop running this surface
fun stop(): TaskInterface<Void>
// Clear surface
fun clear()
}
@@ -40,7 +40,7 @@ public class ReactSurfaceTest {
private ReactHost mReactHost;
private Context mContext;
private ReactSurface mReactSurface;
private ReactSurfaceImpl mReactSurface;
private TestSurfaceHandler mSurfaceHandler;
@Before
@@ -50,13 +50,13 @@ public class ReactSurfaceTest {
mContext = Robolectric.buildActivity(Activity.class).create().get();
mReactHost = spy(new ReactHost(mContext, mReactHostDelegate, null, false, null, false));
doAnswer(mockedStartSurface()).when(mReactHost).startSurface(any(ReactSurface.class));
doAnswer(mockedStartSurface()).when(mReactHost).prerenderSurface(any(ReactSurface.class));
doAnswer(mockedStopSurface()).when(mReactHost).stopSurface(any(ReactSurface.class));
doAnswer(mockedStartSurface()).when(mReactHost).startSurface(any(ReactSurfaceImpl.class));
doAnswer(mockedStartSurface()).when(mReactHost).prerenderSurface(any(ReactSurfaceImpl.class));
doAnswer(mockedStopSurface()).when(mReactHost).stopSurface(any(ReactSurfaceImpl.class));
doReturn(mEventDispatcher).when(mReactHost).getEventDispatcher();
mSurfaceHandler = new TestSurfaceHandler();
mReactSurface = new ReactSurface(mSurfaceHandler, mContext);
mReactSurface = new ReactSurfaceImpl(mSurfaceHandler, mContext);
mReactSurface.attachView(new ReactSurfaceView(mContext, mReactSurface));
}