Skip to content

Commit

Permalink
Add support for creating your own BridgelessDevSupportManager
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjhughes committed Oct 9, 2024
1 parent 4a39ed6 commit 046885e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.facebook.react.devsupport

import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.facebook.react.runtime.ReactHostImpl

public fun interface BridgelessDevSupportManagerFactory {
public fun create(
reactHost: ReactHostImpl
): DevSupportManager
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,44 @@
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.SurfaceDelegateFactory;
import com.facebook.react.common.annotations.FrameworkAPI;
import com.facebook.react.devsupport.DevSupportManagerBase;
import com.facebook.react.devsupport.HMRClient;
import com.facebook.react.devsupport.ReactInstanceDevHelper;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager;
import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback;
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.packagerconnection.RequestHandler;
import com.facebook.react.runtime.internal.bolts.Continuation;
import com.facebook.react.runtime.internal.bolts.Task;
import java.util.Map;

/**
* An implementation of {@link com.facebook.react.devsupport.interfaces.DevSupportManager} that
* extends the functionality in {@link DevSupportManagerBase} with some additional, more flexible
* APIs for asynchronously loading the JS bundle.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
class BridgelessDevSupportManager extends DevSupportManagerBase {
public class BridgelessDevSupportManager extends DevSupportManagerBase {

private final ReactHostImpl mReactHost;

@OptIn(markerClass = FrameworkAPI.class)
public BridgelessDevSupportManager(
ReactHostImpl host, Context context, @Nullable String packagerPathForJSBundleName) {
super(
this(
host,
context.getApplicationContext(),
createInstanceDevHelper(host),
packagerPathForJSBundleName,
Expand All @@ -50,6 +61,37 @@ public BridgelessDevSupportManager(
null /* surfaceDelegateFactory */,
null /* devLoadingViewManager */,
null /* pausedInDebuggerOverlayManager */);
}

/**
* This constructor mirrors the same constructor we have for {@link BridgeDevSupportManager} and
* is used by Expo/ExpoGo to customize the DevMenu on Bridgeless mode.
*/
public BridgelessDevSupportManager(
ReactHostImpl host,
Context applicationContext,
ReactInstanceDevHelper reactInstanceManagerHelper,
@Nullable String packagerPathForJSBundleName,
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers,
@Nullable SurfaceDelegateFactory surfaceDelegateFactory,
@Nullable DevLoadingViewManager devLoadingViewManager,
@Nullable PausedInDebuggerOverlayManager pausedInDebuggerOverlayManager) {
super(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
enableOnCreate,
redBoxHandler,
devBundleDownloadListener,
minNumShakes,
customPackagerCommandHandlers,
surfaceDelegateFactory,
devLoadingViewManager,
pausedInDebuggerOverlayManager);
mReactHost = host;
}

Expand Down Expand Up @@ -102,7 +144,7 @@ public void handleReloadJS() {
mReactHost.reload("BridgelessDevSupportManager.handleReloadJS()");
}

private static ReactInstanceDevHelper createInstanceDevHelper(final ReactHostImpl reactHost) {
public static ReactInstanceDevHelper createInstanceDevHelper(final ReactHostImpl reactHost) {
return new ReactInstanceDevHelper() {
@Override
public void onReloadWithJSDebugger(JavaJSExecutor.Factory proxyExecutorFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import com.facebook.react.bridge.queue.QueueThreadExceptionHandler;
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.common.annotations.FrameworkAPI;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.devsupport.BridgelessDevSupportManagerFactory;
import com.facebook.react.devsupport.DevSupportManagerBase;
import com.facebook.react.devsupport.InspectorFlags;
import com.facebook.react.devsupport.ReleaseDevSupportManager;
Expand Down Expand Up @@ -101,7 +103,7 @@ public class ReactHostImpl implements ReactHost {
private final Context mContext;
private final ReactHostDelegate mReactHostDelegate;
private final ComponentFactory mComponentFactory;
private final DevSupportManager mDevSupportManager;
private DevSupportManager mDevSupportManager;
private final Executor mBGExecutor;
private final Executor mUIExecutor;
private final QueueThreadExceptionHandler mQueueThreadExceptionHandler;
Expand Down Expand Up @@ -138,6 +140,24 @@ public class ReactHostImpl implements ReactHost {

private @Nullable ReactHostInspectorTarget mReactHostInspectorTarget;

public ReactHostImpl(
Context context,
ReactHostDelegate delegate,
ComponentFactory componentFactory,
boolean allowPackagerServerAccess,
boolean useDevSupport,
BridgelessDevSupportManagerFactory devSupportFactory) {
this(
context,
delegate,
componentFactory,
Executors.newSingleThreadExecutor(),
Task.UI_THREAD_EXECUTOR,
allowPackagerServerAccess,
useDevSupport,
devSupportFactory);
}

public ReactHostImpl(
Context context,
ReactHostDelegate delegate,
Expand All @@ -151,7 +171,8 @@ public ReactHostImpl(
Executors.newSingleThreadExecutor(),
Task.UI_THREAD_EXECUTOR,
allowPackagerServerAccess,
useDevSupport);
useDevSupport,
null);
}

public ReactHostImpl(
Expand All @@ -161,7 +182,8 @@ public ReactHostImpl(
Executor bgExecutor,
Executor uiExecutor,
boolean allowPackagerServerAccess,
boolean useDevSupport) {
boolean useDevSupport,
@Nullable BridgelessDevSupportManagerFactory devSupportFactory) {
mContext = context;
mReactHostDelegate = delegate;
mComponentFactory = componentFactory;
Expand All @@ -172,13 +194,14 @@ public ReactHostImpl(
mAllowPackagerServerAccess = allowPackagerServerAccess;
mUseDevSupport = useDevSupport;

if (mUseDevSupport) {
mDevSupportManager =
if (mUseDevSupport) {
mDevSupportManager = devSupportFactory != null ?
devSupportFactory.create(ReactHostImpl.this) :
new BridgelessDevSupportManager(
ReactHostImpl.this, mContext, mReactHostDelegate.getJsMainModulePath());
} else {
mDevSupportManager = new ReleaseDevSupportManager();
}
ReactHostImpl.this, mContext, mReactHostDelegate.getJsMainModulePath());
} else {
mDevSupportManager = new ReleaseDevSupportManager();
}
}

@Override
Expand Down Expand Up @@ -750,7 +773,7 @@ DefaultHardwareBackBtnHandler getDefaultBackButtonHandler() {
};
}

/* package */ Task<Boolean> loadBundle(final JSBundleLoader bundleLoader) {
public Task<Boolean> loadBundle(final JSBundleLoader bundleLoader) {
final String method = "loadBundle()";
log(method, "Schedule");

Expand Down

0 comments on commit 046885e

Please sign in to comment.