Skip to content

Commit e780347

Browse files
committed
Job.onCompletion is renamed to Job.invokeOnCompletion for consistency
1 parent be4cae3 commit e780347

File tree

12 files changed

+42
-33
lines changed

12 files changed

+42
-33
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CancellableContinuation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public inline suspend fun <T> suspendCancellableCoroutine(
125125
* @suppress **This is unstable API and it is subject to change.**
126126
*/
127127
public fun CancellableContinuation<*>.removeOnCancel(node: LockFreeLinkedListNode): Job.Registration =
128-
onCompletion(RemoveOnCancel(this, node))
128+
invokeOnCompletion(RemoveOnCancel(this, node))
129129

130130
// --------------- implementation details ---------------
131131

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Deferred.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public interface Deferred<out T> : Job {
7171
* [completed][isCompleted] yet. It throws the corresponding exception if this deferred has
7272
* [completed exceptionally][isCompletedExceptionally].
7373
*
74-
* This function is designed to be used from [onCompletion] handlers, when there is an absolute certainty that
74+
* This function is designed to be used from [invokeOnCompletion] handlers, when there is an absolute certainty that
7575
* the value is already complete.
7676
*/
7777
public fun getCompleted(): T
@@ -142,7 +142,7 @@ private open class DeferredCoroutine<T>(
142142

143143
@Suppress("UNCHECKED_CAST")
144144
private suspend fun awaitSuspend(): T = suspendCancellableCoroutine { cont ->
145-
cont.unregisterOnCompletion(onCompletion {
145+
cont.unregisterOnCompletion(invokeOnCompletion {
146146
val state = getState()
147147
check(state !is Incomplete)
148148
if (state is CompletedExceptionally)

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Job.kt

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,20 @@ public interface Job : CoroutineContext.Element {
9696
fun getCompletionException(): Throwable
9797

9898
/**
99-
* Registers completion handler. The action depends on the state of this job.
100-
* When job is cancelled with [cancel], then the handler is immediately invoked
101-
* with a cancellation cause or with a fresh [CancellationException].
102-
* Otherwise, handler will be invoked once when this job is complete
103-
* (cancellation also is a form of completion).
99+
* Registers handler that is **synchronously** invoked on completion of this job.
100+
* When job is already complete, then the handler is immediately invoked
101+
* with a cancellation cause or `null`. Otherwise, handler will be invoked once when this
102+
* job is complete. Note, that [cancellation][cancel] is also a form of completion).
104103
*
105104
* The resulting [Registration] can be used to [Registration.unregister] if this
106105
* registration is no longer needed. There is no need to unregister after completion.
107106
*/
107+
public fun invokeOnCompletion(handler: CompletionHandler): Registration
108+
109+
/**
110+
* @suppress **Deprecated**: Renamed to `invokeOnCompletion`
111+
*/
112+
@Deprecated(message = "Renamed to `invokeOnCompletion`", replaceWith = ReplaceWith("invokeOnCompletion"))
108113
public fun onCompletion(handler: CompletionHandler): Registration
109114

110115
/**
@@ -142,7 +147,7 @@ public interface Job : CoroutineContext.Element {
142147
public operator fun plus(other: Job) = other
143148

144149
/**
145-
* Registration object for [onCompletion]. It can be used to [unregister] if needed.
150+
* Registration object for [invokeOnCompletion]. It can be used to [unregister] if needed.
146151
* There is no need to unregister after completion.
147152
*/
148153
public interface Registration {
@@ -154,7 +159,7 @@ public interface Job : CoroutineContext.Element {
154159
}
155160

156161
/**
157-
* Handler for [Job.onCompletion].
162+
* Handler for [Job.invokeOnCompletion].
158163
*/
159164
public typealias CompletionHandler = (Throwable?) -> Unit
160165

@@ -168,22 +173,22 @@ public typealias CancellationException = java.util.concurrent.CancellationExcept
168173
*
169174
* This is a shortcut for the following code with slightly more efficient implementation (one fewer object created).
170175
* ```
171-
* onCompletion { registration.unregister() }
176+
* invokeOnCompletion { registration.unregister() }
172177
* ```
173178
*/
174179
public fun Job.unregisterOnCompletion(registration: Job.Registration): Job.Registration =
175-
onCompletion(UnregisterOnCompletion(this, registration))
180+
invokeOnCompletion(UnregisterOnCompletion(this, registration))
176181

177182
/**
178183
* Cancels a specified [future] when this job is complete.
179184
*
180185
* This is a shortcut for the following code with slightly more efficient implementation (one fewer object created).
181186
* ```
182-
* onCompletion { future.cancel(false) }
187+
* invokeOnCompletion { future.cancel(false) }
183188
* ```
184189
*/
185190
public fun Job.cancelFutureOnCompletion(future: Future<*>): Job.Registration =
186-
onCompletion(CancelFutureOnCompletion(this, future))
191+
invokeOnCompletion(CancelFutureOnCompletion(this, future))
187192

188193
/**
189194
* @suppress **Deprecated**: `join` is now a member function of `Job`.
@@ -285,7 +290,7 @@ public open class JobSupport(active: Boolean) : AbstractCoroutineContextElement(
285290
return
286291
}
287292
// directly pass HandlerNode to parent scope to optimize one closure object (see makeNode)
288-
val newRegistration = parent.onCompletion(CancelOnCompletion(parent, this))
293+
val newRegistration = parent.invokeOnCompletion(CancelOnCompletion(parent, this))
289294
registration = newRegistration
290295
// now check our state _after_ registering (see updateState order of actions)
291296
if (isCompleted) newRegistration.unregister()
@@ -336,7 +341,7 @@ public open class JobSupport(active: Boolean) : AbstractCoroutineContextElement(
336341
// otherwise -- do nothing (it was Empty*)
337342
else -> check(expect is Empty)
338343
}
339-
// handle onCompletion exceptions
344+
// handle invokeOnCompletion exceptions
340345
completionException?.let { handleCompletionException(it) }
341346
// Do other (overridable) processing after completion handlers
342347
afterCompletion(update)
@@ -393,7 +398,9 @@ public open class JobSupport(active: Boolean) : AbstractCoroutineContextElement(
393398
}
394399
}
395400

396-
final override fun onCompletion(handler: CompletionHandler): Job.Registration {
401+
override fun onCompletion(handler: CompletionHandler): Job.Registration = invokeOnCompletion(handler)
402+
403+
final override fun invokeOnCompletion(handler: CompletionHandler): Job.Registration {
397404
var nodeCache: JobNode<*>? = null
398405
while (true) { // lock-free loop on state
399406
val state = this.state
@@ -441,7 +448,7 @@ public open class JobSupport(active: Boolean) : AbstractCoroutineContextElement(
441448
}
442449

443450
private suspend fun joinSuspend() = suspendCancellableCoroutine<Unit> { cont ->
444-
cont.unregisterOnCompletion(onCompletion(ResumeOnCompletion(this, cont)))
451+
cont.unregisterOnCompletion(invokeOnCompletion(ResumeOnCompletion(this, cont)))
445452
}
446453

447454
internal fun removeNode(node: JobNode<*>) {
@@ -475,7 +482,7 @@ public open class JobSupport(active: Boolean) : AbstractCoroutineContextElement(
475482
}
476483

477484
/**
478-
* Override to process any exceptions that were encountered while invoking [onCompletion] handlers.
485+
* Override to process any exceptions that were encountered while invoking [invokeOnCompletion] handlers.
479486
*/
480487
protected open fun handleCompletionException(closeException: Throwable) {
481488
throw closeException

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/NonCancellable.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ object NonCancellable : AbstractCoroutineContextElement(Job), Job {
4646
/** Always throws [IllegalStateException]. */
4747
override fun getCompletionException(): CancellationException = throw IllegalStateException("This job is always active")
4848

49+
override fun onCompletion(handler: CompletionHandler): Job.Registration = invokeOnCompletion(handler)
50+
4951
/** Always returns [EmptyRegistration]. */
50-
override fun onCompletion(handler: CompletionHandler): Job.Registration = EmptyRegistration
52+
override fun invokeOnCompletion(handler: CompletionHandler): Job.Registration = EmptyRegistration
5153

5254
/** Always returns `false`. */
5355
override fun cancel(cause: Throwable?): Boolean = false

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/ThreadPoolDispatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private class ThreadPoolDispatcher(
5656
}
5757

5858
init {
59-
job.onCompletion { executor.shutdown() }
59+
job.invokeOnCompletion { executor.shutdown() }
6060
}
6161

6262
override fun dispatch(context: CoroutineContext, block: Runnable) = executor.execute(block)

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/AbstractChannel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public abstract class AbstractChannel<E> : Channel<E> {
273273
}
274274

275275
private fun removeReceiveOnCancel(cont: CancellableContinuation<*>, receive: Receive<*>) {
276-
cont.onCompletion {
276+
cont.invokeOnCompletion {
277277
if (cont.isCancelled && receive.remove())
278278
onCancelledReceive()
279279
}

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/JobTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class JobTest {
3232
fun testHandler() {
3333
val job = Job()
3434
var fireCount = 0
35-
job.onCompletion { fireCount++ }
35+
job.invokeOnCompletion { fireCount++ }
3636
check(job.isActive)
3737
assertEquals(0, fireCount)
3838
// cancel once
@@ -50,7 +50,7 @@ class JobTest {
5050
val job = Job()
5151
val n = 100
5252
val fireCount = IntArray(n)
53-
for (i in 0 until n) job.onCompletion { fireCount[i]++ }
53+
for (i in 0 until n) job.invokeOnCompletion { fireCount[i]++ }
5454
check(job.isActive)
5555
for (i in 0 until n) assertEquals(0, fireCount[i])
5656
// cancel once
@@ -70,7 +70,7 @@ class JobTest {
7070
val fireCount = IntArray(n)
7171
for (i in 0 until n) {
7272
var registration: Job.Registration? = null
73-
registration = job.onCompletion {
73+
registration = job.invokeOnCompletion {
7474
fireCount[i]++
7575
registration!!.unregister()
7676
}
@@ -92,7 +92,7 @@ class JobTest {
9292
val job = Job()
9393
val n = 100
9494
val fireCount = IntArray(n)
95-
val registrations = Array<Job.Registration>(n) { i -> job.onCompletion { fireCount[i]++ } }
95+
val registrations = Array<Job.Registration>(n) { i -> job.invokeOnCompletion { fireCount[i]++ } }
9696
check(job.isActive)
9797
fun unreg(i: Int) = i % 4 <= 1
9898
for (i in 0 until n) if (unreg(i)) registrations[i].unregister()
@@ -108,7 +108,7 @@ class JobTest {
108108
val n = 100
109109
val fireCount = IntArray(n)
110110
class TestException : Throwable()
111-
for (i in 0 until n) job.onCompletion {
111+
for (i in 0 until n) job.invokeOnCompletion {
112112
fireCount[i]++
113113
throw TestException()
114114
}
@@ -125,7 +125,7 @@ class JobTest {
125125
val job = Job()
126126
val n = 10_000_000
127127
var fireCount = 0
128-
for (i in 0 until n) job.onCompletion { fireCount++ }.unregister()
128+
for (i in 0 until n) job.invokeOnCompletion { fireCount++ }.unregister()
129129
}
130130

131131
@Test

kotlinx-coroutines-javafx/src/main/kotlin/kotlinx/coroutines/experimental/javafx/JavaFx.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ object JavaFx : CoroutineDispatcher(), Delay {
5555
}
5656
val timeline = Timeline(KeyFrame(Duration.millis(unit.toMillis(time).toDouble()), handler))
5757
timeline.play()
58-
continuation.onCompletion { timeline.stop() }
58+
continuation.invokeOnCompletion { timeline.stop() }
5959
}
6060

6161
private class PulseTimer : AnimationTimer() {

kotlinx-coroutines-javafx/src/test/kotlin/examples/FxExampleApp.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class FxTestApp : Application() {
7373
root.children += node
7474
val job = launch(JavaFx, block = block)
7575
animations += job
76-
job.onCompletion { root.children -= node }
76+
job.invokeOnCompletion { root.children -= node }
7777
}
7878

7979
fun doRect() {

kotlinx-coroutines-jdk8/src/main/kotlin/kotlinx/coroutines/experimental/future/Future.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public fun <T> future(context: CoroutineContext = CommonPool, block: suspend ()
5151
public fun <T> Deferred<T>.toCompletableFuture(): CompletableFuture<T> {
5252
val future = CompletableFuture<T>()
5353
future.whenComplete { _, exception -> cancel(exception) }
54-
onCompletion {
54+
invokeOnCompletion {
5555
try {
5656
future.complete(getCompleted())
5757
} catch (exception: Exception) {

kotlinx-coroutines-nio/src/main/kotlin/kotlinx/coroutines/experimental/nio/Nio.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ suspend fun AsynchronousSocketChannel.aWrite(
136136
// ---------------- private details ----------------
137137

138138
private fun Channel.closeOnCancel(cont: CancellableContinuation<*>) {
139-
cont.onCompletion {
139+
cont.invokeOnCompletion {
140140
if (cont.isCancelled)
141141
try {
142142
close()

kotlinx-coroutines-swing/src/main/kotlin/kotlinx/coroutines/experimental/swing/Swing.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object Swing : CoroutineDispatcher(), Delay {
4141
isRepeats = false
4242
start()
4343
}
44-
continuation.onCompletion { timer.stop() }
44+
continuation.invokeOnCompletion { timer.stop() }
4545
}
4646

4747
override fun toString() = "Swing"

0 commit comments

Comments
 (0)