Skip to content

Commit

Permalink
Migrate ReactInstanceManager to lambda's
Browse files Browse the repository at this point in the history
Summary:
We have modern Java now, so inline these lambda's to improve readability. Also removed some old logging related to a completed investigation.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D45062457

fbshipit-source-id: b29f289b84083cb8b5dd2ccb7631ac21b4d04786
  • Loading branch information
javache authored and facebook-github-bot committed Apr 18, 2023
1 parent a804c0f commit d97cca3
Showing 1 changed file with 85 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ public class ReactInstanceManager {
public interface ReactInstanceEventListener
extends com.facebook.react.ReactInstanceEventListener {}

private final Set<ReactRoot> mAttachedReactRoots =
Collections.synchronizedSet(new HashSet<ReactRoot>());
private final Set<ReactRoot> mAttachedReactRoots = Collections.synchronizedSet(new HashSet<>());

private volatile LifecycleState mLifecycleState;

Expand Down Expand Up @@ -277,12 +276,7 @@ public static ReactInstanceManagerBuilder builder() {
mPackages.add(
new CoreModulesPackage(
this,
new DefaultHardwareBackBtnHandler() {
@Override
public void invokeDefaultOnBackPressed() {
ReactInstanceManager.this.invokeDefaultOnBackPressed();
}
},
this::invokeDefaultOnBackPressed,
lazyViewManagersEnabled,
minTimeLeftInFrameForNonBatchedOperationMs));
if (mUseDeveloperSupport) {
Expand Down Expand Up @@ -343,13 +337,7 @@ public JavaScriptExecutorFactory getJavaScriptExecutorFactory() {

@Override
public void destroyRootView(View rootView) {
// TODO T62192299: remove when investigation is complete
FLog.e(TAG, "destroyRootView called");

if (rootView instanceof ReactRootView) {
// TODO T62192299: remove when investigation is complete
FLog.e(TAG, "destroyRootView called, unmountReactApplication");

((ReactRootView) rootView).unmountReactApplication();
}
}
Expand Down Expand Up @@ -456,22 +444,19 @@ private void recreateReactContextInBackgroundInner() {
@Override
public void onPackagerStatusFetched(final boolean packagerIsRunning) {
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
if (packagerIsRunning) {
mDevSupportManager.handleReloadJS();
} else if (mDevSupportManager.hasUpToDateJSBundleInCache()
&& !devSettings.isRemoteJSDebugEnabled()
&& !mUseFallbackBundle) {
// If there is a up-to-date bundle downloaded from server,
// with remote JS debugging disabled, always use that.
onJSBundleLoadedFromServer();
} else {
// If dev server is down, disable the remote JS debugging.
devSettings.setRemoteJSDebugEnabled(false);
recreateReactContextInBackgroundFromBundleLoader();
}
() -> {
if (packagerIsRunning) {
mDevSupportManager.handleReloadJS();
} else if (mDevSupportManager.hasUpToDateJSBundleInCache()
&& !devSettings.isRemoteJSDebugEnabled()
&& !mUseFallbackBundle) {
// If there is a up-to-date bundle downloaded from server,
// with remote JS debugging disabled, always use that.
onJSBundleLoadedFromServer();
} else {
// If dev server is down, disable the remote JS debugging.
devSettings.setRemoteJSDebugEnabled(false);
recreateReactContextInBackgroundFromBundleLoader();
}
});
}
Expand Down Expand Up @@ -883,8 +868,8 @@ public void attachRootView(ReactRoot reactRoot) {
}

// If react context is being created in the background, JS application will be started
// automatically when creation completes, as reactRoot reactRoot is part of the attached
// reactRoot reactRoot list.
// automatically when creation completes, as reactRoot is part of the attached
// reactRoot list.
ReactContext currentContext = getCurrentReactContext();
if (mCreateReactContextThread == null && currentContext != null) {
if (reactRoot.getState().compareAndSet(ReactRoot.STATE_STOPPED, ReactRoot.STATE_STARTED)) {
Expand Down Expand Up @@ -1086,74 +1071,58 @@ private void runCreateReactContextOnNewThread(final ReactContextInitParams initP
mCreateReactContextThread =
new Thread(
null,
new Runnable() {
@Override
public void run() {
ReactMarker.logMarker(REACT_CONTEXT_THREAD_END);
synchronized (ReactInstanceManager.this.mHasStartedDestroying) {
while (ReactInstanceManager.this.mHasStartedDestroying) {
try {
ReactInstanceManager.this.mHasStartedDestroying.wait();
} catch (InterruptedException e) {
continue;
}
() -> {
ReactMarker.logMarker(REACT_CONTEXT_THREAD_END);
synchronized (ReactInstanceManager.this.mHasStartedDestroying) {
while (ReactInstanceManager.this.mHasStartedDestroying) {
try {
ReactInstanceManager.this.mHasStartedDestroying.wait();
} catch (InterruptedException e) {
continue;
}
}
// As destroy() may have run and set this to false, ensure that it is true before we
// create
mHasStartedCreatingInitialContext = true;

final ReactApplicationContext reactApplicationContext;
try {
Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
ReactMarker.logMarker(VM_INIT);
reactApplicationContext =
createReactContext(
initParams.getJsExecutorFactory().create(),
initParams.getJsBundleLoader());
} catch (Exception e) {
// Reset state and bail out. This lets us try again later.
mHasStartedCreatingInitialContext = false;
mCreateReactContextThread = null;
mDevSupportManager.handleException(e);
return;
}
try {
mCreateReactContextThread = null;
ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_START);
final Runnable maybeRecreateReactContextRunnable =
new Runnable() {
@Override
public void run() {
if (mPendingReactContextInitParams != null) {
runCreateReactContextOnNewThread(mPendingReactContextInitParams);
mPendingReactContextInitParams = null;
}
}
};
Runnable setupReactContextRunnable =
new Runnable() {
@Override
public void run() {
try {
setupReactContext(reactApplicationContext);
} catch (Exception e) {
// TODO T62192299: remove this after investigation
FLog.e(
ReactConstants.TAG,
"ReactInstanceManager caught exception in setupReactContext",
e);

mDevSupportManager.handleException(e);
}
}
};

reactApplicationContext.runOnNativeModulesQueueThread(setupReactContextRunnable);
UiThreadUtil.runOnUiThread(maybeRecreateReactContextRunnable);
} catch (Exception e) {
mDevSupportManager.handleException(e);
}
}
// As destroy() may have run and set this to false, ensure that it is true before we
// create
mHasStartedCreatingInitialContext = true;

final ReactApplicationContext reactApplicationContext;
try {
Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
ReactMarker.logMarker(VM_INIT);
reactApplicationContext =
createReactContext(
initParams.getJsExecutorFactory().create(), initParams.getJsBundleLoader());
} catch (Exception e) {
// Reset state and bail out. This lets us try again later.
mHasStartedCreatingInitialContext = false;
mCreateReactContextThread = null;
mDevSupportManager.handleException(e);
return;
}
try {
mCreateReactContextThread = null;
ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_START);
final Runnable maybeRecreateReactContextRunnable =
() -> {
if (mPendingReactContextInitParams != null) {
runCreateReactContextOnNewThread(mPendingReactContextInitParams);
mPendingReactContextInitParams = null;
}
};
Runnable setupReactContextRunnable =
() -> {
try {
setupReactContext(reactApplicationContext);
} catch (Exception e) {
mDevSupportManager.handleException(e);
}
};

reactApplicationContext.runOnNativeModulesQueueThread(setupReactContextRunnable);
UiThreadUtil.runOnUiThread(maybeRecreateReactContextRunnable);
} catch (Exception e) {
mDevSupportManager.handleException(e);
}
},
"create_react_context");
Expand Down Expand Up @@ -1196,37 +1165,26 @@ private void setupReactContext(final ReactApplicationContext reactContext) {
mReactInstanceEventListeners.toArray(listeners);

UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
moveReactContextToCurrentLifecycleState();

for (com.facebook.react.ReactInstanceEventListener listener : finalListeners) {
// Sometimes this listener is null - probably due to race
// condition between allocating listeners with a certain
// size, and getting a `final` version of the array on
// the following line.
if (listener != null) {
listener.onReactContextInitialized(reactContext);
}
() -> {
moveReactContextToCurrentLifecycleState();

for (com.facebook.react.ReactInstanceEventListener listener : finalListeners) {
// Sometimes this listener is null - probably due to race
// condition between allocating listeners with a certain
// size, and getting a `final` version of the array on
// the following line.
if (listener != null) {
listener.onReactContextInitialized(reactContext);
}
}
});
reactContext.runOnJSQueueThread(
new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
ReactMarker.logMarker(CHANGE_THREAD_PRIORITY, "js_default");
}
() -> {
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
ReactMarker.logMarker(CHANGE_THREAD_PRIORITY, "js_default");
});
reactContext.runOnNativeModulesQueueThread(
new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
}
});
() -> Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT));

Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(SETUP_REACT_CONTEXT_END);
Expand Down Expand Up @@ -1279,13 +1237,10 @@ private void attachRootViewToInstance(final ReactRoot reactRoot) {
Systrace.beginAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE, "pre_rootView.onAttachedToReactInstance", rootTag);
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
Systrace.endAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE, "pre_rootView.onAttachedToReactInstance", rootTag);
reactRoot.onStage(ReactStage.ON_ATTACH_TO_INSTANCE);
}
() -> {
Systrace.endAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE, "pre_rootView.onAttachedToReactInstance", rootTag);
reactRoot.onStage(ReactStage.ON_ATTACH_TO_INSTANCE);
});
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
Expand Down

0 comments on commit d97cca3

Please sign in to comment.