File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed
binary-compatibility-validator/reference-public-api
kotlinx-coroutines-core/common Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ public final class kotlinx/coroutines/CompletableDeferredKt {
119
119
public static final fun CompletableDeferred (Ljava/lang/Object;)Lkotlinx/coroutines/CompletableDeferred;
120
120
public static final fun CompletableDeferred (Lkotlinx/coroutines/Job;)Lkotlinx/coroutines/CompletableDeferred;
121
121
public static synthetic fun CompletableDeferred$default (Lkotlinx/coroutines/Job;ILjava/lang/Object;)Lkotlinx/coroutines/CompletableDeferred;
122
+ public static final fun completeWith (Lkotlinx/coroutines/CompletableDeferred;Ljava/lang/Object;)Z
122
123
}
123
124
124
125
public abstract interface class kotlinx/coroutines/CompletableJob : kotlinx/coroutines/Job {
Original file line number Diff line number Diff line change @@ -45,6 +45,18 @@ public interface CompletableDeferred<T> : Deferred<T> {
45
45
public fun completeExceptionally (exception : Throwable ): Boolean
46
46
}
47
47
48
+ /* *
49
+ * Completes this deferred value with the value or exception in the given [result]. Returns `true` if this deferred
50
+ * was completed as a result of this invocation and `false` otherwise (if it was already completed).
51
+ *
52
+ * Subsequent invocations of this function have no effect and always produce `false`.
53
+ *
54
+ * This function transitions this deferred in the same ways described by [CompletableDeferred.complete] and
55
+ * [CompletableDeferred.completeExceptionally].
56
+ */
57
+ @ExperimentalCoroutinesApi // since 1.3.2, tentatively until 1.4.0
58
+ public fun <T > CompletableDeferred<T>.completeWith (result : Result <T >) = result.fold({ complete(it) }, { completeExceptionally(it) })
59
+
48
60
/* *
49
61
* Creates a [CompletableDeferred] in an _active_ state.
50
62
* It is optionally a child of a [parent] job.
Original file line number Diff line number Diff line change @@ -79,6 +79,26 @@ class CompletableDeferredTest : TestBase() {
79
79
assertTrue(c.getCompletionExceptionOrNull() is TestException )
80
80
}
81
81
82
+ @Test
83
+ fun testCompleteWithResultOK () {
84
+ val c = CompletableDeferred <String >()
85
+ assertEquals(true , c.completeWith(Result .success(" OK" )))
86
+ checkCompleteOk(c)
87
+ assertEquals(" OK" , c.getCompleted())
88
+ assertEquals(false , c.completeWith(Result .success(" OK" )))
89
+ checkCompleteOk(c)
90
+ assertEquals(" OK" , c.getCompleted())
91
+ }
92
+
93
+ @Test
94
+ fun testCompleteWithResultException () {
95
+ val c = CompletableDeferred <String >()
96
+ assertEquals(true , c.completeWith(Result .failure(TestException ())))
97
+ checkCancelWithException(c)
98
+ assertEquals(false , c.completeWith(Result .failure(TestException ())))
99
+ checkCancelWithException(c)
100
+ }
101
+
82
102
@Test
83
103
fun testParentCancelsChild () {
84
104
val parent = Job ()
You can’t perform that action at this time.
0 commit comments