Skip to content

Commit

Permalink
Refactoring ReactSurface & adding ReactSurfaceImpl (#38167)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
arushikesarwani94 authored and facebook-github-bot committed Aug 3, 2023
1 parent f493adc commit b47403e
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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;

/**
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public JavaScriptExecutorFactory getJavaScriptExecutorFactory() {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
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;
Expand Down Expand Up @@ -364,9 +364,9 @@ public DevSupportManager getDevSupportManager() {
}

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
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;
Expand All @@ -34,7 +34,7 @@
/** 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);

Expand All @@ -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;
}
Expand All @@ -62,7 +62,7 @@ public static ReactSurface createWithView(
* @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 =
Expand All @@ -83,7 +83,7 @@ public ReactSurface(Context context, String moduleName, @Nullable Bundle initial
}

@VisibleForTesting
ReactSurface(SurfaceHandler surfaceHandler, Context context) {
ReactSurfaceImpl(SurfaceHandler surfaceHandler, Context context) {
mSurfaceHandler = surfaceHandler;
mContext = context;
}
Expand Down Expand Up @@ -128,6 +128,7 @@ public void detach() {
mReactHost.set(null);
}

@Override
public SurfaceHandler getSurfaceHandler() {
return mSurfaceHandler;
}
Expand Down Expand Up @@ -176,14 +177,17 @@ public TaskInterface<Void> stop() {
return host.stopSurface(this);
}

@Override
public int getSurfaceID() {
return mSurfaceHandler.getSurfaceId();
}

@Override
public String getModuleName() {
return mSurfaceHandler.getModuleName();
}

@Override
public void clear() {
UiThreadUtil.runOnUiThread(
() -> {
Expand Down Expand Up @@ -222,10 +226,12 @@ boolean isAttached() {
return mReactHost.get() != null;
}

@Override
public boolean isRunning() {
return mSurfaceHandler.isRunning();
}

@Override
public Context getContext() {
return mContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ReactSurfaceTest {

private ReactHost mReactHost;
private Context mContext;
private ReactSurface mReactSurface;
private ReactSurfaceImpl mReactSurface;
private TestSurfaceHandler mSurfaceHandler;

@Before
Expand All @@ -50,13 +50,13 @@ public void setUp() {
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));
}

Expand Down

0 comments on commit b47403e

Please sign in to comment.