From a4eb5794ccae4a4d54cd20d6105a84bacf91a786 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Mon, 11 Nov 2024 07:03:00 -0800 Subject: [PATCH] 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 --- .../ReactAndroid/api/ReactAndroid.api | 16 ++-- .../internal/bolts/TaskCompletionSource.java | 73 ------------------- .../internal/bolts/TaskCompletionSource.kt | 50 +++++++++++++ 3 files changed, 58 insertions(+), 81 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index bc1155d43a4..14331ddd94d 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -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 ()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 { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.java deleted file mode 100644 index 080c6c43a23..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.java +++ /dev/null @@ -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, providing - * access to the consumer side through the getTask() method while isolating the Task's completion - * mechanisms from the consumer. - */ -public class TaskCompletionSource { - - @NonNull private final Task 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 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."); - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.kt new file mode 100644 index 00000000000..ca4d9b7319f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/TaskCompletionSource.kt @@ -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, providing + * access to the consumer side through the getTask() method while isolating the Task's completion + * mechanisms from the consumer. + */ +public class TaskCompletionSource() { + + /** @return the Task associated with this TaskCompletionSource. */ + public val task: Task = 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.") + } + } +}