Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.internal.* |
| 20 | |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 21 | /** |
| 22 | * Handler for [Job.invokeOnCompletion]. |
| 23 | * |
| 24 | * Installed handler should not throw any exceptions. If it does, they will get caught, |
| 25 | * wrapped into [CompletionHandlerException], and rethrown, potentially causing crash of unrelated code. |
| 26 | * |
| 27 | * **Note**: This type is a part of internal machinery that supports parent-child hierarchies |
| 28 | * and allows for implementation of suspending functions that wait on the Job's state. |
| 29 | * This type should not be used in general application code. |
| 30 | * Implementations of `CompletionHandler` must be fast and _lock-free_. |
| 31 | */ |
| 32 | public typealias CompletionHandler = (cause: Throwable?) -> Unit |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 33 | |
| 34 | // We want class that extends LockFreeLinkedListNode & CompletionHandler but we cannot do it on Kotlin/JS, |
| 35 | // so this expect class provides us with the corresponding abstraction in a platform-agnostic way. |
| 36 | internal expect abstract class CompletionHandlerNode() : LockFreeLinkedListNode { |
| 37 | val asHandler: CompletionHandler |
| 38 | abstract fun invoke(cause: Throwable?) |
| 39 | } |
| 40 | |
| 41 | // :KLUDGE: We have to invoke a handler in platform-specific way via `invokeIt` extension, |
| 42 | // because we play type tricks on Kotlin/JS and handler is not necessarily a function there |
| 43 | internal expect fun CompletionHandler.invokeIt(cause: Throwable?) |