Permalink
Browse files

don't run setupReactContext() on UI thread

Reviewed By: achen1

Differential Revision: D4816125

fbshipit-source-id: e1da5de166bc29d635ffa22e8747a5b61eaf1491
  • Loading branch information...
AaaChiuuu authored and facebook-github-bot committed Apr 20, 2017
1 parent 5fa33d4 commit 350b6c6d7fbefb3d06451abdfd14e69a6577af5c
@@ -128,7 +128,7 @@
private volatile LifecycleState mLifecycleState;
private @Nullable @ThreadConfined(UI) ReactContextInitParams mPendingReactContextInitParams;
private @Nullable @ThreadConfined(UI) Thread mCreateReactContextThread;
private volatile @Nullable Thread mCreateReactContextThread;
/* accessed from any thread */
private final @Nullable JSBundleLoader mBundleLoader; /* path to JS bundle on file system */
@@ -745,23 +745,45 @@ public void run() {
final ReactApplicationContext reactApplicationContext = createReactContext(
initParams.getJsExecutorFactory().create(),
initParams.getJsBundleLoader());
if (mSetupReactContextInBackgroundEnabled) {
mCreateReactContextThread = null;
}
ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_START);
UiThreadUtil.runOnUiThread(new Runnable() {
final Runnable maybeRecreateReactContextRunnable = new Runnable() {
@Override
public void run() {
mCreateReactContextThread = null;
if (mPendingReactContextInitParams != null) {
runCreateReactContextOnNewThread(mPendingReactContextInitParams);
mPendingReactContextInitParams = null;
}
}
};
Runnable setupReactContextRunnable = new Runnable() {
@Override
public void run() {
if (!mSetupReactContextInBackgroundEnabled) {
mCreateReactContextThread = null;
}
try {
setupReactContext(reactApplicationContext);
if (!mSetupReactContextInBackgroundEnabled) {
maybeRecreateReactContextRunnable.run();
}
} catch (Exception e) {
mDevSupportManager.handleException(e);
}
if (mPendingReactContextInitParams != null) {
runCreateReactContextOnNewThread(mPendingReactContextInitParams);
mPendingReactContextInitParams = null;
}
}
});
};
if (mSetupReactContextInBackgroundEnabled) {
reactApplicationContext.runOnNativeModulesQueueThread(setupReactContextRunnable);
UiThreadUtil.runOnUiThread(maybeRecreateReactContextRunnable);
} else {
UiThreadUtil.runOnUiThread(setupReactContextRunnable);
}
} catch (Exception e) {
mDevSupportManager.handleException(e);
}
@@ -775,11 +797,13 @@ private void setupReactContext(ReactApplicationContext reactContext) {
ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_END);
ReactMarker.logMarker(SETUP_REACT_CONTEXT_START);
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "setupReactContext");
UiThreadUtil.assertOnUiThread();
if (!mSetupReactContextInBackgroundEnabled) {
UiThreadUtil.assertOnUiThread();
}
Assertions.assertCondition(mCurrentReactContext == null);
mCurrentReactContext = Assertions.assertNotNull(reactContext);
CatalystInstance catalystInstance =
Assertions.assertNotNull(reactContext.getCatalystInstance());
Assertions.assertNotNull(reactContext.getCatalystInstance());
catalystInstance.initialize();
mDevSupportManager.onNewReactContextCreated(reactContext);
@@ -804,10 +828,12 @@ private void setupReactContext(ReactApplicationContext reactContext) {
}
private void attachMeasuredRootViewToInstance(
ReactRootView rootView,
final ReactRootView rootView,
CatalystInstance catalystInstance) {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance");
UiThreadUtil.assertOnUiThread();
if (!mSetupReactContextInBackgroundEnabled) {
UiThreadUtil.assertOnUiThread();
}
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
int rootTag = uiManagerModule.addMeasuredRootView(rootView);
@@ -820,7 +846,12 @@ private void attachMeasuredRootViewToInstance(
appParams.putDouble("rootTag", rootTag);
appParams.putMap("initialProps", initialProps);
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
rootView.onAttachedToReactInstance();
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
rootView.onAttachedToReactInstance();
}
});
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
@@ -186,7 +186,6 @@ public void removeActivityEventListener(ActivityEventListener listener) {
* Should be called by the hosting Fragment in {@link Fragment#onResume}
*/
public void onHostResume(@Nullable Activity activity) {
UiThreadUtil.assertOnUiThread();
mLifecycleState = LifecycleState.RESUMED;
mCurrentActivity = new WeakReference(activity);
ReactMarker.logMarker(ReactMarkerConstants.ON_HOST_RESUME_START);
@@ -216,7 +215,6 @@ public void onNewIntent(@Nullable Activity activity, Intent intent) {
* Should be called by the hosting Fragment in {@link Fragment#onPause}
*/
public void onHostPause() {
UiThreadUtil.assertOnUiThread();
mLifecycleState = LifecycleState.BEFORE_RESUME;
ReactMarker.logMarker(ReactMarkerConstants.ON_HOST_PAUSE_START);
for (LifecycleEventListener listener : mLifecycleEventListeners) {
@@ -323,7 +323,6 @@ public boolean isDestroyed() {
@VisibleForTesting
@Override
public void initialize() {
UiThreadUtil.assertOnUiThread();
Assertions.assertCondition(
!mInitialized,
"This catalyst instance has already been initialized");
@@ -154,11 +154,10 @@ public void removeListener(EventDispatcherListener listener) {
@Override
public void onHostResume() {
UiThreadUtil.assertOnUiThread();
if (mRCTEventEmitter == null) {
mRCTEventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
}
mCurrentFrameCallback.maybePost();
mCurrentFrameCallback.maybePostFromNonUI();
}
@Override

0 comments on commit 350b6c6

Please sign in to comment.