2222import android .content .Context ;
2323import android .content .Intent ;
2424import android .net .Uri ;
25- import android .os .AsyncTask ;
2625import android .os .Bundle ;
27- import android .os .Process ;
2826import android .view .View ;
2927
3028import com .facebook .common .logging .FLog ;
@@ -130,7 +128,6 @@ public interface ReactInstanceEventListener {
130128
131129 private volatile LifecycleState mLifecycleState ;
132130 private @ Nullable @ ThreadConfined (UI ) ReactContextInitParams mPendingReactContextInitParams ;
133- private @ Nullable @ ThreadConfined (UI ) ReactContextInitAsyncTask mReactContextInitAsyncTask ;
134131 private @ Nullable @ ThreadConfined (UI ) Thread mCreateReactContextThread ;
135132
136133 /* accessed from any thread */
@@ -153,7 +150,7 @@ public interface ReactInstanceEventListener {
153150 private final JSCConfig mJSCConfig ;
154151 private final boolean mLazyNativeModulesEnabled ;
155152 private final boolean mLazyViewManagersEnabled ;
156- private final boolean mUseStartupThread ;
153+ private final boolean mSetupReactContextInBackgroundEnabled ;
157154
158155 private final ReactInstanceDevCommandsHandler mDevInterface =
159156 new ReactInstanceDevCommandsHandler () {
@@ -202,105 +199,6 @@ public JSBundleLoader getJsBundleLoader() {
202199 }
203200 }
204201
205- /*
206- * Task class responsible for (re)creating react context in the background. These tasks can only
207- * be executing one at time, see {@link #recreateReactContextInBackground()}.
208- */
209- private final class ReactContextInitAsyncTask extends
210- AsyncTask <ReactContextInitParams , Void , Result <ReactApplicationContext >> {
211- @ Override
212- protected void onPreExecute () {
213- if (mCurrentReactContext != null ) {
214- tearDownReactContext (mCurrentReactContext );
215- mCurrentReactContext = null ;
216- }
217- }
218-
219- @ Override
220- protected Result <ReactApplicationContext > doInBackground (ReactContextInitParams ... params ) {
221- // TODO(t11687218): Look over all threading
222- // Default priority is Process.THREAD_PRIORITY_BACKGROUND which means we'll be put in a cgroup
223- // that only has access to a small fraction of CPU time. The priority will be reset after
224- // this task finishes: https://android.googlesource.com/platform/frameworks/base/+/d630f105e8bc0021541aacb4dc6498a49048ecea/core/java/android/os/AsyncTask.java#256
225- Process .setThreadPriority (Process .THREAD_PRIORITY_DEFAULT );
226-
227- Assertions .assertCondition (params != null && params .length > 0 && params [0 ] != null );
228- try {
229- JavaScriptExecutor jsExecutor = params [0 ].getJsExecutorFactory ().create ();
230- ReactApplicationContext reactApplicationContext =
231- createReactContext (jsExecutor , params [0 ].getJsBundleLoader ());
232- ReactMarker .logMarker (PRE_SETUP_REACT_CONTEXT_START );
233- return Result .of (reactApplicationContext );
234- } catch (Exception e ) {
235- // Pass exception to onPostExecute() so it can be handled on the main thread
236- return Result .of (e );
237- }
238- }
239-
240- @ Override
241- protected void onPostExecute (Result <ReactApplicationContext > result ) {
242- try {
243- setupReactContext (result .get ());
244- } catch (Exception e ) {
245- mDevSupportManager .handleException (e );
246- } finally {
247- mReactContextInitAsyncTask = null ;
248- }
249-
250- // Handle enqueued request to re-initialize react context.
251- if (mPendingReactContextInitParams != null ) {
252- recreateReactContextInBackground (
253- mPendingReactContextInitParams .getJsExecutorFactory (),
254- mPendingReactContextInitParams .getJsBundleLoader ());
255- mPendingReactContextInitParams = null ;
256- }
257- }
258-
259- @ Override
260- protected void onCancelled (Result <ReactApplicationContext > reactApplicationContextResult ) {
261- try {
262- mMemoryPressureRouter .destroy (reactApplicationContextResult .get ());
263- } catch (Exception e ) {
264- FLog .w (ReactConstants .TAG , "Caught exception after cancelling react context init" , e );
265- } finally {
266- mReactContextInitAsyncTask = null ;
267- }
268- }
269- }
270-
271- private static class Result <T > {
272- @ Nullable private final T mResult ;
273- @ Nullable private final Exception mException ;
274-
275- public static <T , U extends T > Result <T > of (U result ) {
276- return new Result <T >(result );
277- }
278-
279- public static <T > Result <T > of (Exception exception ) {
280- return new Result <>(exception );
281- }
282-
283- private Result (T result ) {
284- mException = null ;
285- mResult = result ;
286- }
287-
288- private Result (Exception exception ) {
289- mException = exception ;
290- mResult = null ;
291- }
292-
293- public T get () throws Exception {
294- if (mException != null ) {
295- throw mException ;
296- }
297-
298- Assertions .assertNotNull (mResult );
299-
300- return mResult ;
301- }
302- }
303-
304202 /**
305203 * Creates a builder that is capable of creating an instance of {@link ReactInstanceManager}.
306204 */
@@ -324,7 +222,7 @@ public static ReactInstanceManagerBuilder builder() {
324222 @ Nullable RedBoxHandler redBoxHandler ,
325223 boolean lazyNativeModulesEnabled ,
326224 boolean lazyViewManagersEnabled ,
327- boolean useStartupThread ) {
225+ boolean setupReactContextInBackgroundEnabled ) {
328226
329227 initializeSoLoaderIfNecessary (applicationContext );
330228
@@ -353,7 +251,7 @@ public static ReactInstanceManagerBuilder builder() {
353251 mJSCConfig = jscConfig ;
354252 mLazyNativeModulesEnabled = lazyNativeModulesEnabled ;
355253 mLazyViewManagersEnabled = lazyViewManagersEnabled ;
356- mUseStartupThread = useStartupThread ;
254+ mSetupReactContextInBackgroundEnabled = setupReactContextInBackgroundEnabled ;
357255
358256 // Instantiate ReactChoreographer in UI thread.
359257 ReactChoreographer .initialize ();
@@ -632,10 +530,6 @@ public void destroy() {
632530
633531 moveToBeforeCreateLifecycleState ();
634532
635- if (mReactContextInitAsyncTask != null ) {
636- mReactContextInitAsyncTask .cancel (true );
637- }
638-
639533 if (mCreateReactContextThread != null ) {
640534 mCreateReactContextThread .interrupt ();
641535 mCreateReactContextThread = null ;
@@ -728,9 +622,7 @@ public void attachMeasuredRootView(ReactRootView rootView) {
728622
729623 // If react context is being created in the background, JS application will be started
730624 // automatically when creation completes, as root view is part of the attached root view list.
731- if (mReactContextInitAsyncTask == null &&
732- mCreateReactContextThread == null &&
733- mCurrentReactContext != null ) {
625+ if (mCreateReactContextThread == null && mCurrentReactContext != null ) {
734626 attachMeasuredRootViewToInstance (rootView , mCurrentReactContext .getCatalystInstance ());
735627 }
736628 }
@@ -824,29 +716,17 @@ private void onJSBundleLoadedFromServer() {
824716
825717 @ ThreadConfined (UI )
826718 private void recreateReactContextInBackground (
827- JavaScriptExecutor .Factory jsExecutorFactory ,
828- JSBundleLoader jsBundleLoader ) {
719+ JavaScriptExecutor .Factory jsExecutorFactory ,
720+ JSBundleLoader jsBundleLoader ) {
829721 UiThreadUtil .assertOnUiThread ();
830722
831723 final ReactContextInitParams initParams = new ReactContextInitParams (
832724 jsExecutorFactory ,
833725 jsBundleLoader );
834- if (mUseStartupThread ) {
835- if (mCreateReactContextThread == null ) {
836- runCreateReactContextOnNewThread (initParams );
837- } else {
838- mPendingReactContextInitParams = initParams ;
839- }
726+ if (mCreateReactContextThread == null ) {
727+ runCreateReactContextOnNewThread (initParams );
840728 } else {
841- if (mReactContextInitAsyncTask == null ) {
842- // No background task to create react context is currently running, create and execute one.
843- mReactContextInitAsyncTask = new ReactContextInitAsyncTask ();
844- mReactContextInitAsyncTask .executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR , initParams );
845- } else {
846- // Background task is currently running, queue up most recent init params to recreate
847- // context once task completes.
848- mPendingReactContextInitParams = initParams ;
849- }
729+ mPendingReactContextInitParams = initParams ;
850730 }
851731 }
852732
0 commit comments