Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 01934df | 2017-01-31 09:18:34 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.NonCancellable.isActive |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.selects.* |
| 9 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 01934df | 2017-01-31 09:18:34 +0300 | [diff] [blame] | 10 | |
| 11 | /** |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 12 | * A non-cancelable job that is always [active][isActive]. It is designed for [withContext] function |
| 13 | * to prevent cancellation of code blocks that need to be executed without cancellation. |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame] | 14 | * |
| 15 | * Use it like this: |
Roman Elizarov | 01934df | 2017-01-31 09:18:34 +0300 | [diff] [blame] | 16 | * ``` |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 17 | * withContext(NonCancellable) { |
Roman Elizarov | 01934df | 2017-01-31 09:18:34 +0300 | [diff] [blame] | 18 | * // this code will not be cancelled |
| 19 | * } |
| 20 | * ``` |
| 21 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 22 | public object NonCancellable : AbstractCoroutineContextElement(Job), Job { |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 23 | /** Always returns `true`. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 24 | override val isActive: Boolean get() = true |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 25 | |
| 26 | /** Always returns `false`. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 27 | override val isCompleted: Boolean get() = false |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 28 | |
| 29 | /** Always returns `false`. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 30 | override val isCancelled: Boolean get() = false |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 31 | |
| 32 | /** Always returns `false`. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 33 | override fun start(): Boolean = false |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 34 | |
| 35 | /** Always throws [UnsupportedOperationException]. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 36 | override suspend fun join() { |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 37 | throw UnsupportedOperationException("This job is always active") |
| 38 | } |
| 39 | |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 40 | override val onJoin: SelectClause0 |
| 41 | get() = throw UnsupportedOperationException("This job is always active") |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 42 | |
| 43 | /** Always throws [IllegalStateException]. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 44 | override fun getCancellationException(): CancellationException = throw IllegalStateException("This job is always active") |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 45 | |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 46 | /** Always returns [NonDisposableHandle]. */ |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 47 | @Suppress("OverridingDeprecatedMember") |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 48 | override fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle = |
| 49 | NonDisposableHandle |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 50 | |
| 51 | /** Always returns [NonDisposableHandle]. */ |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 52 | @Suppress("OverridingDeprecatedMember") |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 53 | override fun invokeOnCompletion(handler: CompletionHandler, onCancelling: Boolean): DisposableHandle = |
| 54 | NonDisposableHandle |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 55 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 56 | /** Always returns [NonDisposableHandle]. */ |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 57 | @Suppress("OverridingDeprecatedMember") |
Roman Elizarov | a12ee15 | 2017-12-20 10:50:17 +0300 | [diff] [blame] | 58 | override fun invokeOnCompletion(onCancelling_: Boolean, handler: CompletionHandler): DisposableHandle = |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 59 | NonDisposableHandle |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 60 | |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 61 | /** Always returns [NonDisposableHandle]. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 62 | override fun invokeOnCompletion(onCancelling: Boolean, invokeImmediately: Boolean, handler: CompletionHandler): DisposableHandle = |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 63 | NonDisposableHandle |
Roman Elizarov | 447b88a | 2017-12-01 15:37:48 +0300 | [diff] [blame] | 64 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 65 | /** Always returns `false`. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 66 | override fun cancel(cause: Throwable?): Boolean = false |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 67 | |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 68 | /** Always returns [emptySequence]. */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 69 | override val children: Sequence<Job> |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 70 | get() = emptySequence() |
| 71 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 72 | /** Always returns [NonDisposableHandle] and does not do anything. */ |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 73 | @Suppress("OverridingDeprecatedMember") |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 74 | override fun attachChild(child: Job): DisposableHandle = NonDisposableHandle |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 75 | |
| 76 | /** Does not do anything. */ |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 77 | @Suppress("OverridingDeprecatedMember") |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 78 | override fun cancelChildren(cause: Throwable?) {} |
Roman Elizarov | 01934df | 2017-01-31 09:18:34 +0300 | [diff] [blame] | 79 | } |