|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2021 Alexander Söderberg & Contributors |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +package cloud.commandframework.kotlin.coroutines |
| 25 | + |
| 26 | +import cloud.commandframework.context.CommandContext |
| 27 | +import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator |
| 28 | +import cloud.commandframework.execution.CommandExecutionCoordinator |
| 29 | +import cloud.commandframework.execution.CommandExecutionHandler |
| 30 | +import kotlinx.coroutines.CoroutineScope |
| 31 | +import kotlinx.coroutines.GlobalScope |
| 32 | +import kotlinx.coroutines.future.future |
| 33 | +import kotlin.coroutines.CoroutineContext |
| 34 | +import kotlin.coroutines.EmptyCoroutineContext |
| 35 | + |
| 36 | +/** |
| 37 | + * Suspending version of [CommandExecutionHandler] for use with |
| 38 | + * coroutines. |
| 39 | + * |
| 40 | + * NOTE: It is highly advised to not use [CommandExecutionCoordinator.SimpleCoordinator] together |
| 41 | + * with coroutine support. Consider using [AsynchronousCommandExecutionCoordinator] instead. |
| 42 | + * |
| 43 | + * @param C command sender type |
| 44 | + */ |
| 45 | +public fun interface SuspendingExecutionHandler<C : Any> { |
| 46 | + /** |
| 47 | + * Handles command execution. |
| 48 | + * |
| 49 | + * @param commandContext command context |
| 50 | + */ |
| 51 | + public suspend operator fun invoke(commandContext: CommandContext<C>) |
| 52 | + |
| 53 | + /** |
| 54 | + * Create a new [CommandExecutionHandler] for use in building commands, |
| 55 | + * backed by this [SuspendingExecutionHandler]. |
| 56 | + * |
| 57 | + * @param scope coroutine scope |
| 58 | + * @param context coroutine context |
| 59 | + * @return new [CommandExecutionHandler] |
| 60 | + */ |
| 61 | + public fun asCommandExecutionHandler( |
| 62 | + scope: CoroutineScope = GlobalScope, |
| 63 | + context: CoroutineContext = EmptyCoroutineContext, |
| 64 | + ): CommandExecutionHandler<C> = createCommandExecutionHandler(scope, context, this) |
| 65 | + |
| 66 | + public companion object { |
| 67 | + /** |
| 68 | + * Create a new [CommandExecutionHandler] for use in building commands, |
| 69 | + * backed by the given [SuspendingExecutionHandler]. |
| 70 | + * |
| 71 | + * @param scope coroutine scope |
| 72 | + * @param context coroutine context |
| 73 | + * @param handler suspending handler |
| 74 | + * @return new [CommandExecutionHandler] |
| 75 | + */ |
| 76 | + public fun <C : Any> createCommandExecutionHandler( |
| 77 | + scope: CoroutineScope = GlobalScope, |
| 78 | + context: CoroutineContext = EmptyCoroutineContext, |
| 79 | + handler: SuspendingExecutionHandler<C>, |
| 80 | + ): CommandExecutionHandler<C> = CommandExecutionHandler.FutureCommandExecutionHandler { ctx -> |
| 81 | + scope.future(context) { |
| 82 | + handler(ctx) |
| 83 | + null |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments