From c41ab475b1cc73d49131429af15143541fbef7b8 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Fri, 13 Sep 2024 11:35:24 -0700 Subject: [PATCH] Back out "Remove some Tasks overhead" (#46483) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46483 Original commit changeset: 631d741bd2ec This is breaking the RedBox on React Native 0.76 on Android when not connected to Metro. Original Phabricator Diff: D62213722 Changelog: [Internal] [Changed] - Back out "[react-native] Remove some Tasks overhead" Reviewed By: cipolleschi Differential Revision: D62644614 fbshipit-source-id: a092614da78bef65546c2539a3ebc9bff5e807b2 --- .../runtime/BridgelessDevSupportManager.java | 23 +++++---- .../facebook/react/runtime/ReactHostImpl.java | 47 ++++++++++--------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDevSupportManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDevSupportManager.java index c9780967bfa..b411f73f9e5 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDevSupportManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDevSupportManager.java @@ -23,6 +23,8 @@ import com.facebook.react.devsupport.HMRClient; import com.facebook.react.devsupport.ReactInstanceDevHelper; import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback; import com.facebook.react.modules.core.DeviceEventManagerModule; +import com.facebook.react.runtime.internal.bolts.Continuation; +import com.facebook.react.runtime.internal.bolts.Task; /** * An implementation of {@link com.facebook.react.devsupport.interfaces.DevSupportManager} that @@ -67,17 +69,20 @@ class BridgelessDevSupportManager extends DevSupportManagerBase { mReactHost .loadBundle(bundleLoader) .onSuccess( - task -> { - if (task.isCompleted()) { - String bundleURL = - getDevServerHelper().getDevServerSplitBundleURL(bundlePath); - ReactContext reactContext = mReactHost.getCurrentReactContext(); - if (reactContext != null) { - reactContext.getJSModule(HMRClient.class).registerBundle(bundleURL); + new Continuation() { + @Override + public Void then(Task task) { + if (task.getResult().equals(Boolean.TRUE)) { + String bundleURL = + getDevServerHelper().getDevServerSplitBundleURL(bundlePath); + ReactContext reactContext = mReactHost.getCurrentReactContext(); + if (reactContext != null) { + reactContext.getJSModule(HMRClient.class).registerBundle(bundleURL); + } + callback.onSuccess(); } - callback.onSuccess(); + return null; } - return null; }); } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java index f987addd053..f105dea178b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java @@ -9,6 +9,8 @@ package com.facebook.react.runtime; import static com.facebook.infer.annotation.Assertions.assertNotNull; import static com.facebook.infer.annotation.ThreadConfined.UI; +import static java.lang.Boolean.FALSE; +import static java.lang.Boolean.TRUE; import android.app.Activity; import android.content.Context; @@ -250,12 +252,13 @@ public class ReactHostImpl implements ReactHost { detachSurface(surface); return callWithExistingReactInstance( - method, - reactInstance -> { - log(method, "Execute"); - reactInstance.stopSurface(surface); - }, - mBGExecutor); + method, + reactInstance -> { + log(method, "Execute"); + reactInstance.stopSurface(surface); + }, + mBGExecutor) + .makeVoid(); } /** @@ -776,7 +779,7 @@ public class ReactHostImpl implements ReactHost { }; } - /* package */ Task loadBundle(final JSBundleLoader bundleLoader) { + /* package */ Task loadBundle(final JSBundleLoader bundleLoader) { final String method = "loadBundle()"; log(method, "Schedule"); @@ -789,7 +792,7 @@ public class ReactHostImpl implements ReactHost { null); } - /* package */ Task registerSegment( + /* package */ Task registerSegment( final int segmentId, final String path, final Callback callback) { final String method = "registerSegment(segmentId = \"" + segmentId + "\", path = \"" + path + "\")"; @@ -825,7 +828,7 @@ public class ReactHostImpl implements ReactHost { * @param args Arguments to be passed to the function * @return A Task that will complete when the function call has been enqueued on the JS thread. */ - /* package */ Task callFunctionOnModule( + /* package */ Task callFunctionOnModule( final String moduleName, final String methodName, final NativeArray args) { final String method = "callFunctionOnModule(\"" + moduleName + "\", \"" + methodName + "\")"; return callWithExistingReactInstance( @@ -947,9 +950,9 @@ public class ReactHostImpl implements ReactHost { } /** Schedule work on a ReactInstance that is already created. */ - private Task callWithExistingReactInstance( + private Task callWithExistingReactInstance( final String callingMethod, - final ReactInstanceCalback callback, + final ReactInstanceCalback continuation, @Nullable Executor executor) { final String method = "callWithExistingReactInstance(" + callingMethod + ")"; @@ -959,18 +962,19 @@ public class ReactHostImpl implements ReactHost { return mCreateReactInstanceTaskRef .get() - .continueWith( + .onSuccess( task -> { final ReactInstance reactInstance = ReactNativeFeatureFlags.completeReactInstanceCreationOnBgThreadOnAndroid() ? task.getResult() : mReactInstance; - if (reactInstance == null || task.isFaulted()) { + if (reactInstance == null) { raiseSoftException(method, "Execute: reactInstance is null. Dropping work."); - } else { - callback.then(reactInstance); + return FALSE; } - return null; + + continuation.then(reactInstance); + return TRUE; }, executor); } @@ -978,7 +982,7 @@ public class ReactHostImpl implements ReactHost { /** Create a ReactInstance if it doesn't exist already, and schedule work on it. */ private Task callAfterGetOrCreateReactInstance( final String callingMethod, - final ReactInstanceCalback callback, + final ReactInstanceCalback runnable, @Nullable Executor executor) { final String method = "callAfterGetOrCreateReactInstance(" + callingMethod + ")"; @@ -987,17 +991,18 @@ public class ReactHostImpl implements ReactHost { } return getOrCreateReactInstance() - .continueWith( + .onSuccess( task -> { final ReactInstance reactInstance = ReactNativeFeatureFlags.completeReactInstanceCreationOnBgThreadOnAndroid() ? task.getResult() : mReactInstance; - if (reactInstance == null || task.isFaulted()) { + if (reactInstance == null) { raiseSoftException(method, "Execute: reactInstance is null. Dropping work."); - } else { - callback.then(reactInstance); + return null; } + + runnable.then(reactInstance); return null; }, executor)