cloud-kotlin-coroutines (#318)
* Builder coroutine support * Move coroutines to version catalog * Add kdocs * Add note about simple coordinator * Update changelog
This commit is contained in:
parent
6011bd1d63
commit
3f0ef5715c
10 changed files with 298 additions and 3 deletions
|
|
@ -0,0 +1,11 @@
|
|||
# Module cloud-kotlin-coroutines
|
||||
|
||||
Cloud extensions for Kotlin coroutine integration.
|
||||
|
||||
# Package cloud.commandframework.kotlin.coroutines
|
||||
|
||||
Cloud Kotlin coroutines classes and functions.
|
||||
|
||||
# Package cloud.commandframework.kotlin.coroutines.extension
|
||||
|
||||
Extension functions for existing Cloud types
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2021 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.kotlin.coroutines
|
||||
|
||||
import cloud.commandframework.context.CommandContext
|
||||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator
|
||||
import cloud.commandframework.execution.CommandExecutionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.future.future
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
/**
|
||||
* Suspending version of [CommandExecutionHandler] for use with
|
||||
* coroutines.
|
||||
*
|
||||
* NOTE: It is highly advised to not use [CommandExecutionCoordinator.SimpleCoordinator] together
|
||||
* with coroutine support. Consider using [AsynchronousCommandExecutionCoordinator] instead.
|
||||
*
|
||||
* @param C command sender type
|
||||
*/
|
||||
public fun interface SuspendingExecutionHandler<C : Any> {
|
||||
/**
|
||||
* Handles command execution.
|
||||
*
|
||||
* @param commandContext command context
|
||||
*/
|
||||
public suspend operator fun invoke(commandContext: CommandContext<C>)
|
||||
|
||||
/**
|
||||
* Create a new [CommandExecutionHandler] for use in building commands,
|
||||
* backed by this [SuspendingExecutionHandler].
|
||||
*
|
||||
* @param scope coroutine scope
|
||||
* @param context coroutine context
|
||||
* @return new [CommandExecutionHandler]
|
||||
*/
|
||||
public fun asCommandExecutionHandler(
|
||||
scope: CoroutineScope = GlobalScope,
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
): CommandExecutionHandler<C> = createCommandExecutionHandler(scope, context, this)
|
||||
|
||||
public companion object {
|
||||
/**
|
||||
* Create a new [CommandExecutionHandler] for use in building commands,
|
||||
* backed by the given [SuspendingExecutionHandler].
|
||||
*
|
||||
* @param scope coroutine scope
|
||||
* @param context coroutine context
|
||||
* @param handler suspending handler
|
||||
* @return new [CommandExecutionHandler]
|
||||
*/
|
||||
public fun <C : Any> createCommandExecutionHandler(
|
||||
scope: CoroutineScope = GlobalScope,
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
handler: SuspendingExecutionHandler<C>,
|
||||
): CommandExecutionHandler<C> = CommandExecutionHandler.FutureCommandExecutionHandler { ctx ->
|
||||
scope.future(context) {
|
||||
handler(ctx)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2021 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.kotlin.coroutines.extension
|
||||
|
||||
import cloud.commandframework.Command
|
||||
import cloud.commandframework.kotlin.coroutines.SuspendingExecutionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
/**
|
||||
* Specify a suspending command execution handler.
|
||||
*
|
||||
* @param scope coroutine scope
|
||||
* @param context coroutine context
|
||||
* @param handler suspending handler
|
||||
* @return modified copy of this [Command.Builder]
|
||||
* @see Command.Builder.handler
|
||||
* @see SuspendingExecutionHandler
|
||||
*/
|
||||
public fun <C : Any> Command.Builder<C>.suspendingHandler(
|
||||
scope: CoroutineScope = GlobalScope,
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
handler: SuspendingExecutionHandler<C>,
|
||||
): Command.Builder<C> = handler(SuspendingExecutionHandler.createCommandExecutionHandler(scope, context, handler))
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2021 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.kotlin.coroutines.extension
|
||||
|
||||
import cloud.commandframework.kotlin.MutableCommandBuilder
|
||||
import cloud.commandframework.kotlin.coroutines.SuspendingExecutionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
/**
|
||||
* Specify a suspending command execution handler.
|
||||
*
|
||||
* @param scope coroutine scope
|
||||
* @param context coroutine context
|
||||
* @param handler suspending handler
|
||||
* @return this [MutableCommandBuilder]
|
||||
* @see MutableCommandBuilder.handler
|
||||
* @see SuspendingExecutionHandler
|
||||
*/
|
||||
public fun <C : Any> MutableCommandBuilder<C>.suspendingHandler(
|
||||
scope: CoroutineScope = GlobalScope,
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
handler: SuspendingExecutionHandler<C>,
|
||||
): MutableCommandBuilder<C> = mutate {
|
||||
it.suspendingHandler(scope, context, handler)
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2021 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.kotlin.coroutines
|
||||
|
||||
import cloud.commandframework.CommandManager
|
||||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator
|
||||
import cloud.commandframework.internal.CommandRegistrationHandler
|
||||
import cloud.commandframework.kotlin.coroutines.extension.suspendingHandler
|
||||
import cloud.commandframework.kotlin.extension.buildAndRegister
|
||||
import cloud.commandframework.meta.CommandMeta
|
||||
import cloud.commandframework.meta.SimpleCommandMeta
|
||||
import kotlinx.coroutines.future.await
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class SuspendingHandlerTest {
|
||||
|
||||
companion object {
|
||||
val executorService: ExecutorService = Executors.newSingleThreadExecutor()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test(): Unit = runBlocking {
|
||||
val manager = TestCommandManager()
|
||||
|
||||
manager.buildAndRegister("suspend") {
|
||||
suspendingHandler {
|
||||
println("called from thread: ${Thread.currentThread().name}")
|
||||
|
||||
someSuspendingFunction()
|
||||
}
|
||||
}
|
||||
|
||||
manager.executeCommand(TestCommandSender(), "suspend").await()
|
||||
}
|
||||
|
||||
suspend fun someSuspendingFunction() {}
|
||||
|
||||
private class TestCommandSender
|
||||
|
||||
private class TestCommandManager : CommandManager<TestCommandSender>(
|
||||
AsynchronousCommandExecutionCoordinator.newBuilder<TestCommandSender>()
|
||||
.withExecutor(executorService)
|
||||
.build(),
|
||||
CommandRegistrationHandler.nullCommandRegistrationHandler()
|
||||
) {
|
||||
|
||||
override fun hasPermission(sender: TestCommandSender, permission: String): Boolean = true
|
||||
|
||||
override fun createDefaultCommandMeta(): CommandMeta = SimpleCommandMeta.empty()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue