Skip to content

Commit

Permalink
On Android, seperate logic to initialize JS from starting the app
Browse files Browse the repository at this point in the history
Reviewed By: achen1

Differential Revision: D6606265

fbshipit-source-id: d432661b5f8aa2b7600b1140e1617aab852f343e
  • Loading branch information
axe-fb authored and facebook-github-bot committed Dec 20, 2017
1 parent 19a9c5e commit 4996b9a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ private void attachRootViewToInstance(
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
final int rootTag = uiManagerModule.addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.runApplication();
rootView.invokeJSEntryPoint();
Systrace.beginAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE,
"pre_rootView.onAttachedToReactInstance",
Expand Down
76 changes: 52 additions & 24 deletions ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public interface ReactRootViewEventListener {
private boolean mWasMeasured = false;
private int mWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
private int mHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
private @Nullable Runnable mJSEntryPoint;

public ReactRootView(Context context) {
super(context);
Expand Down Expand Up @@ -379,42 +380,69 @@ public void setAppProperties(@Nullable Bundle appProperties) {
UiThreadUtil.assertOnUiThread();
mAppProperties = appProperties;
if (getRootViewTag() != 0) {
runApplication();
invokeJSEntryPoint();
}
}

/**
* Calls into JS to start the React application. Can be called multiple times with the
* same rootTag, which will re-render the application from the root.
*/
/* package */ void runApplication() {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication");
try {
if (mReactInstanceManager == null || !mIsAttachedToInstance) {
return;
}
/*package */ void invokeJSEntryPoint() {
if (mJSEntryPoint == null) {
defaultJSEntryPoint();
} else {
mJSEntryPoint.run();
}
}

ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
if (reactContext == null) {
return;
}
/**
* Set a custom entry point for invoking JS. By default, this is AppRegistry.runApplication
* @param jsEntryPoint
*/
public void setJSEntryPoint(Runnable jsEntryPoint) {
mJSEntryPoint = jsEntryPoint;
}

CatalystInstance catalystInstance = reactContext.getCatalystInstance();
public void invokeDefaultJSEntryPoint(@Nullable Bundle appProperties) {
UiThreadUtil.assertOnUiThread();
if (appProperties != null) {
mAppProperties = appProperties;
}
defaultJSEntryPoint();
}

WritableNativeMap appParams = new WritableNativeMap();
appParams.putDouble("rootTag", getRootViewTag());
@Nullable Bundle appProperties = getAppProperties();
if (appProperties != null) {
appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
}
/**
* Calls the default entry point into JS which is AppRegistry.runApplication()
*/
private void defaultJSEntryPoint() {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication");
try {
if (mReactInstanceManager == null || !mIsAttachedToInstance) {
return;
}

mShouldLogContentAppeared = true;
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
if (reactContext == null) {
return;
}

String jsAppModuleName = getJSModuleName();
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
CatalystInstance catalystInstance = reactContext.getCatalystInstance();

WritableNativeMap appParams = new WritableNativeMap();
appParams.putDouble("rootTag", getRootViewTag());
@Nullable Bundle appProperties = getAppProperties();
if (appProperties != null) {
appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
}

mShouldLogContentAppeared = true;

String jsAppModuleName = getJSModuleName();
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
}

/**
Expand Down

0 comments on commit 4996b9a

Please sign in to comment.