Migrate TaskCompletionSource to kotlin (#47541)

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

Migrate TaskCompletionSource to kotlin

changelog: [internal] internal

Reviewed By: javache

Differential Revision: D65738330

fbshipit-source-id: 34963ae4a95dbea2fcebbdaa33436e22a7ca4751
This commit is contained in:
David Vacca 2024-11-11 07:03:00 -08:00 committed by Facebook GitHub Bot
parent 7b21b9e69c
commit a4eb5794cc
3 changed files with 58 additions and 81 deletions

View File

@ -3895,15 +3895,15 @@ public abstract interface class com/facebook/react/runtime/internal/bolts/Task$U
public abstract fun unobservedException (Lcom/facebook/react/runtime/internal/bolts/Task;Lcom/facebook/react/runtime/internal/bolts/UnobservedTaskException;)V
}
public class com/facebook/react/runtime/internal/bolts/TaskCompletionSource {
public final class com/facebook/react/runtime/internal/bolts/TaskCompletionSource {
public fun <init> ()V
public fun getTask ()Lcom/facebook/react/runtime/internal/bolts/Task;
public fun setCancelled ()V
public fun setError (Ljava/lang/Exception;)V
public fun setResult (Ljava/lang/Object;)V
public fun trySetCancelled ()Z
public fun trySetError (Ljava/lang/Exception;)Z
public fun trySetResult (Ljava/lang/Object;)Z
public final fun getTask ()Lcom/facebook/react/runtime/internal/bolts/Task;
public final fun setCancelled ()V
public final fun setError (Ljava/lang/Exception;)V
public final fun setResult (Ljava/lang/Object;)V
public final fun trySetCancelled ()Z
public final fun trySetError (Ljava/lang/Exception;)Z
public final fun trySetResult (Ljava/lang/Object;)Z
}
public final class com/facebook/react/shell/MainPackageConfig {

View File

@ -1,73 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.runtime.internal.bolts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Allows safe orchestration of a task's completion, preventing the consumer from prematurely
* completing the task. Essentially, it represents the producer side of a Task<TResult>, providing
* access to the consumer side through the getTask() method while isolating the Task's completion
* mechanisms from the consumer.
*/
public class TaskCompletionSource<TResult> {
@NonNull private final Task<TResult> task;
/**
* Creates a TaskCompletionSource that orchestrates a Task. This allows the creator of a task to
* be solely responsible for its completion.
*/
public TaskCompletionSource() {
task = new Task<>();
}
/**
* @return the Task associated with this TaskCompletionSource.
*/
public @NonNull Task<TResult> getTask() {
return task;
}
/** Sets the cancelled flag on the Task if the Task hasn't already been completed. */
public boolean trySetCancelled() {
return task.trySetCancelled();
}
/** Sets the result on the Task if the Task hasn't already been completed. */
public boolean trySetResult(@Nullable TResult result) {
return task.trySetResult(result);
}
/** Sets the error on the Task if the Task hasn't already been completed. */
public boolean trySetError(@Nullable Exception error) {
return task.trySetError(error);
}
/** Sets the cancelled flag on the task, throwing if the Task has already been completed. */
public void setCancelled() {
if (!trySetCancelled()) {
throw new IllegalStateException("Cannot cancel a completed task.");
}
}
/** Sets the result of the Task, throwing if the Task has already been completed. */
public void setResult(@Nullable TResult result) {
if (!trySetResult(result)) {
throw new IllegalStateException("Cannot set the result of a completed task.");
}
}
/** Sets the error of the Task, throwing if the Task has already been completed. */
public void setError(@Nullable Exception error) {
if (!trySetError(error)) {
throw new IllegalStateException("Cannot set the error on a completed task.");
}
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.runtime.internal.bolts
/**
* Allows safe orchestration of a task's completion, preventing the consumer from prematurely
* completing the task. Essentially, it represents the producer side of a Task<TResult>, providing
* access to the consumer side through the getTask() method while isolating the Task's completion
* mechanisms from the consumer.
*/
public class TaskCompletionSource<TResult>() {
/** @return the Task associated with this TaskCompletionSource. */
public val task: Task<TResult> = Task()
/** Sets the cancelled flag on the Task if the Task hasn't already been completed. */
public fun trySetCancelled(): Boolean = task.trySetCancelled()
/** Sets the result on the Task if the Task hasn't already been completed. */
public fun trySetResult(result: TResult?): Boolean = task.trySetResult(result)
/** Sets the error on the Task if the Task hasn't already been completed. */
public fun trySetError(error: Exception?): Boolean = task.trySetError(error)
/** Sets the cancelled flag on the task, throwing if the Task has already been completed. */
public fun setCancelled(): Unit {
if (!trySetCancelled()) {
throw IllegalStateException("Cannot cancel a completed task.")
}
}
/** Sets the result of the Task, throwing if the Task has already been completed. */
public fun setResult(result: TResult?): Unit {
if (!trySetResult(result)) {
throw IllegalStateException("Cannot set the result of a completed task.")
}
}
/** Sets the error of the Task, throwing if the Task has already been completed. */
public fun setError(error: Exception?): Unit {
if (!trySetError(error)) {
throw IllegalStateException("Cannot set the error on a completed task.")
}
}
}