Skip to content

Commit 4996b9a

Browse files
axe-fbfacebook-github-bot
authored andcommitted
On Android, seperate logic to initialize JS from starting the app
Reviewed By: achen1 Differential Revision: D6606265 fbshipit-source-id: d432661b5f8aa2b7600b1140e1617aab852f343e
1 parent 19a9c5e commit 4996b9a

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ private void attachRootViewToInstance(
10441044
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
10451045
final int rootTag = uiManagerModule.addRootView(rootView);
10461046
rootView.setRootViewTag(rootTag);
1047-
rootView.runApplication();
1047+
rootView.invokeJSEntryPoint();
10481048
Systrace.beginAsyncSection(
10491049
TRACE_TAG_REACT_JAVA_BRIDGE,
10501050
"pre_rootView.onAttachedToReactInstance",

ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java

+52-24
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public interface ReactRootViewEventListener {
8787
private boolean mWasMeasured = false;
8888
private int mWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
8989
private int mHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
90+
private @Nullable Runnable mJSEntryPoint;
9091

9192
public ReactRootView(Context context) {
9293
super(context);
@@ -379,42 +380,69 @@ public void setAppProperties(@Nullable Bundle appProperties) {
379380
UiThreadUtil.assertOnUiThread();
380381
mAppProperties = appProperties;
381382
if (getRootViewTag() != 0) {
382-
runApplication();
383+
invokeJSEntryPoint();
383384
}
384385
}
385386

386387
/**
387388
* Calls into JS to start the React application. Can be called multiple times with the
388389
* same rootTag, which will re-render the application from the root.
389390
*/
390-
/* package */ void runApplication() {
391-
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication");
392-
try {
393-
if (mReactInstanceManager == null || !mIsAttachedToInstance) {
394-
return;
395-
}
391+
/*package */ void invokeJSEntryPoint() {
392+
if (mJSEntryPoint == null) {
393+
defaultJSEntryPoint();
394+
} else {
395+
mJSEntryPoint.run();
396+
}
397+
}
396398

397-
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
398-
if (reactContext == null) {
399-
return;
400-
}
399+
/**
400+
* Set a custom entry point for invoking JS. By default, this is AppRegistry.runApplication
401+
* @param jsEntryPoint
402+
*/
403+
public void setJSEntryPoint(Runnable jsEntryPoint) {
404+
mJSEntryPoint = jsEntryPoint;
405+
}
401406

402-
CatalystInstance catalystInstance = reactContext.getCatalystInstance();
407+
public void invokeDefaultJSEntryPoint(@Nullable Bundle appProperties) {
408+
UiThreadUtil.assertOnUiThread();
409+
if (appProperties != null) {
410+
mAppProperties = appProperties;
411+
}
412+
defaultJSEntryPoint();
413+
}
403414

404-
WritableNativeMap appParams = new WritableNativeMap();
405-
appParams.putDouble("rootTag", getRootViewTag());
406-
@Nullable Bundle appProperties = getAppProperties();
407-
if (appProperties != null) {
408-
appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
409-
}
415+
/**
416+
* Calls the default entry point into JS which is AppRegistry.runApplication()
417+
*/
418+
private void defaultJSEntryPoint() {
419+
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication");
420+
try {
421+
if (mReactInstanceManager == null || !mIsAttachedToInstance) {
422+
return;
423+
}
410424

411-
mShouldLogContentAppeared = true;
425+
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
426+
if (reactContext == null) {
427+
return;
428+
}
412429

413-
String jsAppModuleName = getJSModuleName();
414-
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
415-
} finally {
416-
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
417-
}
430+
CatalystInstance catalystInstance = reactContext.getCatalystInstance();
431+
432+
WritableNativeMap appParams = new WritableNativeMap();
433+
appParams.putDouble("rootTag", getRootViewTag());
434+
@Nullable Bundle appProperties = getAppProperties();
435+
if (appProperties != null) {
436+
appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
437+
}
438+
439+
mShouldLogContentAppeared = true;
440+
441+
String jsAppModuleName = getJSModuleName();
442+
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
443+
} finally {
444+
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
445+
}
418446
}
419447

420448
/**

0 commit comments

Comments
 (0)