|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +@file:Suppress("RedundantVisibilityModifier") |
| 6 | + |
| 7 | +package kotlinx.coroutines.experimental.tasks |
| 8 | + |
| 9 | +import com.google.android.gms.tasks.CancellationTokenSource |
| 10 | +import com.google.android.gms.tasks.RuntimeExecutionException |
| 11 | +import com.google.android.gms.tasks.Task |
| 12 | +import com.google.android.gms.tasks.TaskCompletionSource |
| 13 | +import kotlinx.coroutines.experimental.CancellationException |
| 14 | +import kotlinx.coroutines.experimental.CompletableDeferred |
| 15 | +import kotlinx.coroutines.experimental.Deferred |
| 16 | +import kotlinx.coroutines.experimental.Job |
| 17 | +import kotlinx.coroutines.experimental.suspendCancellableCoroutine |
| 18 | + |
| 19 | +/** |
| 20 | + * Converts this deferred to the instance of [Task]. |
| 21 | + * If deferred is cancelled then resulting task will be cancelled as well. |
| 22 | + */ |
| 23 | +public fun <T> Deferred<T>.asTask(): Task<T> { |
| 24 | + val cancellation = CancellationTokenSource() |
| 25 | + val source = TaskCompletionSource<T>(cancellation.token) |
| 26 | + |
| 27 | + invokeOnCompletion callback@{ |
| 28 | + if (isCancelled) { |
| 29 | + cancellation.cancel() |
| 30 | + return@callback |
| 31 | + } |
| 32 | + |
| 33 | + val t = getCompletionExceptionOrNull() |
| 34 | + if (t == null) { |
| 35 | + source.setResult(getCompleted()) |
| 36 | + } else { |
| 37 | + source.setException(t as? Exception ?: RuntimeExecutionException(t)) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return source.task |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Converts this task to an instance of [Deferred]. |
| 46 | + * If task is cancelled then resulting deferred will be cancelled as well. |
| 47 | + */ |
| 48 | +public fun <T> Task<T>.asDeferred(): Deferred<T> { |
| 49 | + if (isComplete) { |
| 50 | + val e = exception |
| 51 | + return if (e == null) { |
| 52 | + CompletableDeferred<T>().apply { if (isCanceled) cancel() else complete(result) } |
| 53 | + } else { |
| 54 | + CompletableDeferred<T>().apply { completeExceptionally(e) } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + val result = CompletableDeferred<T>() |
| 59 | + addOnCompleteListener { |
| 60 | + val e = it.exception |
| 61 | + if (e == null) { |
| 62 | + if (isCanceled) result.cancel() else result.complete(it.result) |
| 63 | + } else { |
| 64 | + result.completeExceptionally(e) |
| 65 | + } |
| 66 | + } |
| 67 | + return result |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Awaits for completion of the task without blocking a thread. |
| 72 | + * |
| 73 | + * This suspending function is cancellable. |
| 74 | + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 75 | + * stops waiting for the completion stage and immediately resumes with [CancellationException]. |
| 76 | + */ |
| 77 | +public suspend fun <T> Task<T>.await(): T { |
| 78 | + // fast path |
| 79 | + if (isComplete) { |
| 80 | + val e = exception |
| 81 | + return if (e == null) { |
| 82 | + if (isCanceled) { |
| 83 | + throw CancellationException("Task $this was cancelled normally.") |
| 84 | + } else { |
| 85 | + result |
| 86 | + } |
| 87 | + } else { |
| 88 | + throw e |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return suspendCancellableCoroutine { cont -> |
| 93 | + addOnCompleteListener { |
| 94 | + val e = exception |
| 95 | + if (e == null) { |
| 96 | + if (isCanceled) cont.cancel() else cont.resume(result) |
| 97 | + } else { |
| 98 | + cont.resumeWithException(e) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments