Skip to content

Commit

Permalink
rn-android | Allow injecting a custom ChoreographerProvider via React…
Browse files Browse the repository at this point in the history
…NativeHost into construction of ReactInstanceManager.

Summary:
Further propagating extension to the Android choreographer, now allowing to override it from the perspective of ReactNativeHost/ReactInstanceManager(Builder).

Changelog:
[Android][Added] ReactChoreographer can now use an implementation substitution instead of relying on android.view.Choreographer directly.

Reviewed By: javache

Differential Revision: D50827973

fbshipit-source-id: 42efaa3ece2c2b45fe4ee04a4bbc87c9d59132c8
  • Loading branch information
nlutsenko authored and facebook-github-bot committed Oct 31, 2023
1 parent 751f7e9 commit 37e509f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.internal.AndroidChoreographerProvider;
import com.facebook.react.internal.ChoreographerProvider;
import com.facebook.react.internal.turbomodule.core.TurboModuleManager;
import com.facebook.react.internal.turbomodule.core.TurboModuleManagerDelegate;
import com.facebook.react.modules.appearance.AppearanceModule;
Expand Down Expand Up @@ -236,7 +237,8 @@ public static ReactInstanceManagerBuilder builder() {
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers,
@Nullable ReactPackageTurboModuleManagerDelegate.Builder tmmDelegateBuilder,
@Nullable SurfaceDelegateFactory surfaceDelegateFactory,
@Nullable DevLoadingViewManager devLoadingViewManager) {
@Nullable DevLoadingViewManager devLoadingViewManager,
@Nullable ChoreographerProvider choreographerProvider) {
FLog.d(TAG, "ReactInstanceManager.ctor()");
initializeSoLoaderIfNecessary(applicationContext);

Expand Down Expand Up @@ -294,7 +296,10 @@ public void invokeDefaultOnBackPressed() {
mJSIModulePackage = jsiModulePackage;

// Instantiate ReactChoreographer in UI thread.
ReactChoreographer.initialize(AndroidChoreographerProvider.getInstance());
ReactChoreographer.initialize(
choreographerProvider != null
? choreographerProvider
: AndroidChoreographerProvider.getInstance());
if (mUseDeveloperSupport) {
mDevSupportManager.startInspector();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.internal.ChoreographerProvider;
import com.facebook.react.jscexecutor.JSCExecutor;
import com.facebook.react.jscexecutor.JSCExecutorFactory;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class ReactInstanceManagerBuilder {
private @Nullable SurfaceDelegateFactory mSurfaceDelegateFactory;
private @Nullable DevLoadingViewManager mDevLoadingViewManager;
private @Nullable JSEngineResolutionAlgorithm mJSEngineResolutionAlgorithm = null;
private @Nullable ChoreographerProvider mChoreographerProvider = null;

/* package protected */ ReactInstanceManagerBuilder() {}

Expand Down Expand Up @@ -287,6 +289,12 @@ public ReactInstanceManagerBuilder setReactPackageTurboModuleManagerDelegateBuil
return this;
}

public ReactInstanceManagerBuilder setChoreographerProvider(
@Nullable ChoreographerProvider choreographerProvider) {
mChoreographerProvider = choreographerProvider;
return this;
}

/**
* Instantiates a new {@link ReactInstanceManager}. Before calling {@code build}, the following
* must be called:
Expand Down Expand Up @@ -350,7 +358,8 @@ public ReactInstanceManager build() {
mCustomPackagerCommandHandlers,
mTMMDelegateBuilder,
mSurfaceDelegateFactory,
mDevLoadingViewManager);
mDevLoadingViewManager,
mChoreographerProvider);
}

private JavaScriptExecutorFactory getDefaultJSExecutorFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.react.devsupport.DevSupportManagerFactory;
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.internal.ChoreographerProvider;
import java.util.List;

/**
Expand Down Expand Up @@ -87,7 +88,8 @@ protected ReactInstanceManager createReactInstanceManager() {
.setInitialLifecycleState(LifecycleState.BEFORE_CREATE)
.setReactPackageTurboModuleManagerDelegateBuilder(
getReactPackageTurboModuleManagerDelegateBuilder())
.setJSEngineResolutionAlgorithm(getJSEngineResolutionAlgorithm());
.setJSEngineResolutionAlgorithm(getJSEngineResolutionAlgorithm())
.setChoreographerProvider(getChoreographerProvider());

for (ReactPackage reactPackage : getPackages()) {
builder.addPackage(reactPackage);
Expand Down Expand Up @@ -213,4 +215,12 @@ protected String getJSMainModuleName() {
protected @Nullable JSEngineResolutionAlgorithm getJSEngineResolutionAlgorithm() {
return null;
}

/**
* Returns a custom implementation of ChoreographerProvider to be used this host. If null - React
* will use default direct android.view.Choreographer-based provider.
*/
protected @Nullable ChoreographerProvider getChoreographerProvider() {
return null;
}
}

0 comments on commit 37e509f

Please sign in to comment.