Scoop API

scoop:scoop.core:0.1.0

src/cancellable_coroutine.scoop

Selected production source · src/cancellable_coroutine.scoop

1/**
2 * Arbitrates result delivery and cancellation so only one terminal outcome wins.
3 */
4public class CancellableContinuation<T> internal constructor(continuation: Continuation<T>) {
5 private val lock: CoroutineLock = CoroutineLock()
6 private var continuation: Continuation<T>? = Some(continuation)
7 private var jobRegistration: Disposable? = None
8 private var cancellation: CancellationException? = None
9 private val listeners: CoroutineListeners = CoroutineListeners()
11 internal fun bind(job: Job) {
12 val registration = job.invokeOnCancellation { error -> cancel(error) }
13 val finished =
14 lock.withLock {
15 if (coroutineAbsent(continuation))
16 return@withLock true
17 jobRegistration = Some(registration)
18 false
19 }
20 if (finished)
21 registration.dispose()
22 }
24 internal val isCompleted: Boolean get() = lock.withLock { coroutineAbsent(continuation) }
26 private fun take(reason: CancellationException?): Continuation<T>? =
27 lock.withLock {
28 val target = continuation
29 if (coroutineAbsent(target))
30 return@withLock None
31 continuation = None
32 cancellation = reason
33 when (jobRegistration) {
34 case Some(val registration) -> registration.dispose()
35 case None -> {}
36 }
37 jobRegistration = None
38 target
39 }
41 private fun discardListeners() {
42 listeners.clear()
43 }
45 /**
46 * Offers a result and returns false when another completion or cancellation already won.
47 */
48 public fun tryResume(value: T): Boolean {
49 when (take(None)) {
50 case None -> return false
51 case Some(val target) -> {
52 discardListeners()
53 target.resume(value)
54 return true
55 }
56 }
57 }
59 /**
60 * Offers an exception and returns false when the continuation has already been claimed.
61 */
62 public fun tryResumeWithException(exception: Throwable): Boolean {
63 when (take(None)) {
64 case None -> return false
65 case Some(val target) -> {
66 discardListeners()
67 target.resumeWithException(exception)
68 return true
69 }
70 }
71 }
73 private fun cancel(error: CancellationException) {
74 when (take(Some(error))) {
75 case None -> {}
76 case Some(val target) -> {
77 try {
78 notifyCoroutineListeners(listeners.take())
79 }
80 finally {
81 target.resumeWithException(error)
82 }
83 }
84 }
85 }
87 /**
88 * Registers cancellation cleanup and returns a disposable registration. An already-cancelled
89 * continuation invokes the handler immediately.
90 */
91 public fun invokeOnCancellation(handler: (CancellationException) -> Unit): Disposable {
92 val listener =
93 lock.withLock {
94 val entry = listeners.add { handler(cancellation!!) }
95 if (coroutineAbsent(continuation) && coroutineAbsent(cancellation))
96 entry.dispose()
97 entry
98 }
99 val cancelled = lock.withLock { !coroutineAbsent(cancellation) }
100 if (cancelled)
101 listener.fire()
102 return listener
103 }
106/**
107 * Registers a suspension tied to the current job when one exists. Cancellation competes with
108 * result delivery through the supplied continuation.
109 */
110public suspend fun <T> suspendCancellableCoroutine(
111 registration: (CancellableContinuation<T>) -> Unit
112): T =
113 suspendCoroutine<T> {
114 raw ->
115 val continuation = CancellableContinuation<T>(raw)
116 when (currentJobOrNone()) {
117 case Some(val job) -> continuation.bind(job)
118 case None -> {}
119 }
120 if (!continuation.isCompleted) {
121 try {
122 registration(continuation)
123 }
124 catch (error: Throwable) {
125 if (!continuation.tryResumeWithException(error))
126 throw error
127 }
128 }
129 }