Inline Task.call without Executor (#49906)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49906

Task.call is equivalent to a try-catch and wrapping the result in a Task. Inlining this is cheaper than creating a TaskCompletionSource and a Runnable.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D70803554

fbshipit-source-id: 0886176be974eff67c983ce1475ad685a8021b14
This commit is contained in:
Pieter De Baets authored and Facebook GitHub Bot committed 2025-03-10 07:51:23 -07:00
1 parent d998b7b330
commit d8181721f6
3 files changed
+26 -44

No files matched your search

@@ -1303,7 +1303,11 @@ public class ReactHostImpl implements ReactHost {
* throws an exception, the task will fault, and we'll go through the ReactHost error
* reporting pipeline.
*/
return Task.call(() -> mReactHostDelegate.getJsBundleLoader());
try {
return Task.forResult(mReactHostDelegate.getJsBundleLoader());
} catch (Exception e) {
return Task.forError(e);
}
}
}
@@ -252,16 +252,6 @@ public class Task<TResult> implements TaskInterface<TResult> {
return tcs.getTask();
}
/**
* Invokes the callable on the current thread, producing a Task.
*
* <p>If you want to cancel the resulting Task throw a {@link
* java.util.concurrent.CancellationException} from the callable.
*/
public static <TResult> Task<TResult> call(final Callable<TResult> callable) {
return call(callable, IMMEDIATE_EXECUTOR);
}
/**
* Adds a continuation that will be scheduled using the executor, returning a new task that
* completes after the continuation has finished running. This allows the continuation to be
@@ -20,10 +20,12 @@ import org.assertj.core.api.Assertions
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.stubbing.Answer
import org.mockito.ArgumentMatchers.any
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@@ -41,19 +43,13 @@ class ReactSurfaceTest {
@Before
fun setUp() {
eventDispatcher = mock(EventDispatcher::class.java)
eventDispatcher = mock()
context = Robolectric.buildActivity(Activity::class.java).create().get()
reactHost = Mockito.mock(ReactHostImpl::class.java)
Mockito.doAnswer(mockedStartSurface())
.`when`(reactHost)
.startSurface(ArgumentMatchers.any(ReactSurfaceImpl::class.java))
Mockito.doAnswer(mockedStartSurface())
.`when`(reactHost)
.prerenderSurface(ArgumentMatchers.any(ReactSurfaceImpl::class.java))
Mockito.doAnswer(mockedStopSurface())
.`when`(reactHost)
.stopSurface(ArgumentMatchers.any(ReactSurfaceImpl::class.java))
Mockito.doReturn(eventDispatcher).`when`(reactHost).eventDispatcher
reactHost = mock()
whenever(reactHost.startSurface(any())).thenAnswer(this::mockedStartSurface)
whenever(reactHost.prerenderSurface(any())).thenAnswer(this::mockedStartSurface)
whenever(reactHost.stopSurface(any())).thenAnswer(this::mockedStopSurface)
whenever(reactHost.eventDispatcher).doReturn(eventDispatcher)
surfaceHandler = TestSurfaceHandler()
reactSurface = ReactSurfaceImpl(surfaceHandler, context)
reactSurface.attachView(ReactSurfaceView(context, reactSurface))
@@ -79,7 +75,7 @@ class ReactSurfaceTest {
reactSurface.attach(reactHost)
val task = reactSurface.prerender() as Task<Void>
task.waitForCompletion()
Mockito.verify(reactHost).prerenderSurface(reactSurface)
verify(reactHost).prerenderSurface(reactSurface)
Assertions.assertThat(surfaceHandler.isRunning).isTrue()
}
@@ -90,7 +86,7 @@ class ReactSurfaceTest {
Assertions.assertThat(reactHost.isSurfaceAttached(reactSurface)).isFalse()
val task = reactSurface.start() as Task<Void>
task.waitForCompletion()
Mockito.verify(reactHost).startSurface(reactSurface)
verify(reactHost).startSurface(reactSurface)
Assertions.assertThat(surfaceHandler.isRunning).isTrue()
}
@@ -102,7 +98,7 @@ class ReactSurfaceTest {
task.waitForCompletion()
task = reactSurface.stop() as Task<*>
task.waitForCompletion()
Mockito.verify(reactHost).stopSurface(reactSurface)
verify(reactHost).stopSurface(reactSurface)
}
@Test
@@ -145,22 +141,14 @@ class ReactSurfaceTest {
Assertions.assertThat(reactSurface.isRunning).isFalse()
}
private fun mockedStartSurface(): Answer<Task<Void>> {
return Answer {
Task.call {
surfaceHandler.start()
null
}
}
private fun mockedStartSurface(inv: InvocationOnMock): Task<Void> {
surfaceHandler.start()
return Task.forResult(null)
}
private fun mockedStopSurface(): Answer<Task<Boolean>> {
return Answer {
Task.call {
surfaceHandler.stop()
true
}
}
private fun mockedStopSurface(inv: InvocationOnMock): Task<Boolean> {
surfaceHandler.stop()
return Task.forResult(true)
}
internal class TestSurfaceHandler : SurfaceHandler {