Skip to content

Commit bf8d6da

Browse files
javachefacebook-github-bot
authored andcommitted
Make Task.call API simpler (#50545)
Summary: Pull Request resolved: #50545 All usages of Task.call involve a Runnable which returns another Task, which we then extract using `.continueWithTask(Task::getResult)`. Instead inline this behaviour inside `Task.call` to avoid allocation of unnecessary intermediate objects. Changelog: [Internal] Reviewed By: rshest Differential Revision: D72600051 fbshipit-source-id: 7e7c0c8e1de24b1aeff7addab2eb10272bc73ddb
1 parent 26e89cf commit bf8d6da

File tree

2 files changed

+61
-47
lines changed

2 files changed

+61
-47
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public LifecycleState getLifecycleState() {
231231
*/
232232
@Override
233233
public TaskInterface<Void> start() {
234-
return Task.call(this::getOrCreateStartTask, mBGExecutor).continueWithTask(Task::getResult);
234+
return Task.call(this::getOrCreateStartTask, mBGExecutor);
235235
}
236236

237237
/** Initialize and run a React Native surface in a background without mounting real views. */
@@ -488,37 +488,36 @@ public void removeReactInstanceEventListener(ReactInstanceEventListener listener
488488
public TaskInterface<Void> reload(String reason) {
489489
final String method = "reload()";
490490
return Task.call(
491-
() -> {
492-
Task<Void> reloadTask = null;
493-
if (mDestroyTask != null) {
494-
log(method, "Waiting for destroy to finish, before reloading React Native.");
495-
reloadTask =
496-
mDestroyTask
497-
.continueWithTask(task -> getOrCreateReloadTask(reason), mBGExecutor)
498-
.makeVoid();
499-
} else {
500-
reloadTask = getOrCreateReloadTask(reason).makeVoid();
501-
}
491+
() -> {
492+
Task<Void> reloadTask = null;
493+
if (mDestroyTask != null) {
494+
log(method, "Waiting for destroy to finish, before reloading React Native.");
495+
reloadTask =
496+
mDestroyTask
497+
.continueWithTask(task -> getOrCreateReloadTask(reason), mBGExecutor)
498+
.makeVoid();
499+
} else {
500+
reloadTask = getOrCreateReloadTask(reason).makeVoid();
501+
}
502502

503-
return reloadTask.continueWithTask(
504-
task -> {
505-
if (task.isFaulted()) {
506-
final Exception ex = task.getError();
507-
Assertions.assertNotNull(ex, "Reload failed without an exception");
508-
if (mUseDevSupport) {
509-
mDevSupportManager.handleException(ex);
510-
} else {
511-
mReactHostDelegate.handleInstanceException(ex);
512-
}
513-
return getOrCreateDestroyTask("Reload failed", ex);
514-
}
503+
return reloadTask.continueWithTask(
504+
task -> {
505+
if (task.isFaulted()) {
506+
final Exception ex = task.getError();
507+
Assertions.assertNotNull(ex, "Reload failed without an exception");
508+
if (mUseDevSupport) {
509+
mDevSupportManager.handleException(ex);
510+
} else {
511+
mReactHostDelegate.handleInstanceException(ex);
512+
}
513+
return getOrCreateDestroyTask("Reload failed", ex);
514+
}
515515

516-
return task;
517-
},
518-
mBGExecutor);
519-
},
520-
mBGExecutor)
521-
.continueWithTask(Task::getResult);
516+
return task;
517+
},
518+
mBGExecutor);
519+
},
520+
mBGExecutor);
522521
}
523522

524523
@DoNotStrip
@@ -601,19 +600,18 @@ public Void then(Task<Void> task) throws Exception {
601600
public TaskInterface<Void> destroy(String reason, @Nullable Exception ex) {
602601
final String method = "destroy()";
603602
return Task.call(
604-
() -> {
605-
if (mReloadTask != null) {
606-
log(
607-
method,
608-
"Reloading React Native. Waiting for reload to finish before destroying React"
609-
+ " Native.");
610-
return mReloadTask.continueWithTask(
611-
task -> getOrCreateDestroyTask(reason, ex), mBGExecutor);
612-
}
613-
return getOrCreateDestroyTask(reason, ex);
614-
},
615-
mBGExecutor)
616-
.continueWithTask(Task::getResult);
603+
() -> {
604+
if (mReloadTask != null) {
605+
log(
606+
method,
607+
"Reloading React Native. Waiting for reload to finish before destroying React"
608+
+ " Native.");
609+
return mReloadTask.continueWithTask(
610+
task -> getOrCreateDestroyTask(reason, ex), mBGExecutor);
611+
}
612+
return getOrCreateDestroyTask(reason, ex);
613+
},
614+
mBGExecutor);
617615
}
618616

619617
private MemoryPressureListener createMemoryPressureListener(ReactInstance reactInstance) {
@@ -1111,8 +1109,7 @@ private BridgelessReactContext getOrCreateReactContext() {
11111109
* destroying, will wait until destroy is finished, before creating.
11121110
*/
11131111
private Task<ReactInstance> getOrCreateReactInstance() {
1114-
return Task.call(this::waitThenCallGetOrCreateReactInstanceTask, mBGExecutor)
1115-
.continueWithTask(Task::getResult);
1112+
return Task.call(this::waitThenCallGetOrCreateReactInstanceTask, mBGExecutor);
11161113
}
11171114

11181115
@ThreadConfined("ReactHost")

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Task.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,29 @@ public class Task<TResult> : TaskInterface<TResult> {
297297
* Invokes the callable using the given executor, returning a Task to represent the operation.
298298
*/
299299
@JvmStatic
300-
public fun <TResult> call(callable: Callable<TResult>, executor: Executor): Task<TResult> {
300+
public fun <TResult> call(
301+
callable: Callable<Task<TResult>>,
302+
executor: Executor
303+
): Task<TResult> {
301304
val tcs = TaskCompletionSource<TResult>()
302305
try {
303306
executor.execute {
307+
val continuation = Continuation { task: Task<TResult> ->
308+
when {
309+
task.isCancelled() -> tcs.setCancelled()
310+
task.isFaulted() -> tcs.setError(task.getError())
311+
else -> tcs.setResult(task.getResult())
312+
}
313+
}
304314
try {
305-
tcs.setResult(callable.call())
315+
val task = callable.call()
316+
synchronized(task.lock) {
317+
if (task.isCompleted()) {
318+
continuation.then(task)
319+
} else {
320+
task.continuations.add(continuation)
321+
}
322+
}
306323
} catch (e: CancellationException) {
307324
tcs.setCancelled()
308325
} catch (e: Exception) {

0 commit comments

Comments
 (0)