Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.bladehunt.kotstom.coroutines

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.bladehunt.kotstom.dsl.EventBuilder
import net.minestom.server.event.Event
Expand All @@ -17,6 +16,19 @@ inline fun <T : Event> EventBuilder<T>.blockingHandler(crossinline block: suspen
runBlocking { block(it) }
}

/**
* A coroutine handler that runs asynchronously in [scope]. Cancelling [scope] cancels running
* handlers, and handler failures are reported to the `ExceptionManager` without cancelling [scope].
*
* @param scope The `CoroutineScope` that owns the launched coroutines
* @param block The suspending code to run
* @author oglassdev
*/
inline fun <T : Event> EventBuilder<T>.asyncHandler(
scope: CoroutineScope,
crossinline block: suspend (T) -> Unit
) = handler { scope.launchReporting { block(it) } }

/**
* A coroutine handler that runs asynchronously using the `MinestomDispatcher`
*
Expand All @@ -27,4 +39,4 @@ inline fun <T : Event> EventBuilder<T>.blockingHandler(crossinline block: suspen
inline fun <T : Event> EventBuilder<T>.asyncHandler(
context: CoroutineContext = MinestomDispatcher,
crossinline block: suspend (T) -> Unit
) = handler { CoroutineScope(context).launch { block(it) } }
) = asyncHandler(CoroutineScope(context), block)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.bladehunt.kotstom.coroutines

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import net.bladehunt.kotstom.ExceptionManager
import kotlin.coroutines.cancellation.CancellationException

/**
* Launches [block] in this scope, reporting failures to Minestom's `ExceptionManager` instead of
* cancelling the scope.
*/
@PublishedApi
internal fun CoroutineScope.launchReporting(block: suspend CoroutineScope.() -> Unit): Job = launch {
try {
block()
} catch (e: CancellationException) {
throw e
} catch (e: Throwable) {
ExceptionManager.handleException(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package net.bladehunt.kotstom.coroutines.kommand

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.bladehunt.kotstom.command.Kommand
import net.bladehunt.kotstom.coroutines.MinestomDispatcher
import net.bladehunt.kotstom.coroutines.launchReporting
import net.bladehunt.kotstom.dsl.kommand.KommandDSL
import net.minestom.server.command.CommandSender
import net.minestom.server.command.builder.CommandContext
Expand All @@ -15,21 +15,34 @@ import kotlin.coroutines.CoroutineContext
internal typealias SuspendKommandExecutor = suspend context(CommandContext) (CommandSender) -> Unit

/**
* Sets the async default executor of the Kommand
* Sets the async default executor of the Kommand. Cancelling [scope] cancels running executions,
* and failures are reported to the `ExceptionManager` without cancelling [scope].
*
* @param context The `CoroutineContext` to launch the coroutine in
* @param scope The `CoroutineScope` that owns the launched coroutines
* @param block The suspending code to run
* @author oglassdev
*/
inline fun Kommand.defaultExecutorAsync(
context: CoroutineContext = MinestomDispatcher,
scope: CoroutineScope,
crossinline block: @KommandDSL SuspendKommandExecutor
) =
CommandExecutor { sender, ctx ->
CoroutineScope(context).launch { block(ctx, sender) }
scope.launchReporting { block(ctx, sender) }
}
.also { defaultExecutor = it }

/**
* Sets the async default executor of the Kommand
*
* @param context The `CoroutineContext` to launch the coroutine in
* @param block The suspending code to run
* @author oglassdev
*/
inline fun Kommand.defaultExecutorAsync(
context: CoroutineContext = MinestomDispatcher,
crossinline block: @KommandDSL SuspendKommandExecutor
) = defaultExecutorAsync(CoroutineScope(context), block)

/**
* Sets the blocking default executor of the Kommand
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
package net.bladehunt.kotstom.coroutines.kommand

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.bladehunt.kotstom.coroutines.MinestomDispatcher
import net.bladehunt.kotstom.coroutines.launchReporting
import net.bladehunt.kotstom.dsl.kommand.KommandDSL
import net.bladehunt.kotstom.dsl.kommand.KommandSyntaxBuilder
import net.minestom.server.command.builder.CommandExecutor
import kotlin.coroutines.CoroutineContext

/**
* Sets the async executor for the command
* Sets the async executor for the command. Cancelling [scope] cancels running executions, and
* failures are reported to the `ExceptionManager` without cancelling [scope].
*
* @param scope The `CoroutineScope` that owns the launched coroutines
* @param block The executor logic
* @return The command executor
* @author oglassdev
*/
inline fun KommandSyntaxBuilder.executorAsync(
context: CoroutineContext = MinestomDispatcher,
scope: CoroutineScope,
crossinline block: @KommandDSL SuspendKommandExecutor
) =
CommandExecutor { sender, ctx ->
CoroutineScope(context).launch { block(ctx, sender) }
scope.launchReporting { block(ctx, sender) }
}
.also { executor = it }

/**
* Sets the async executor for the command
*
* @param block The executor logic
* @return The command executor
* @author oglassdev
*/
inline fun KommandSyntaxBuilder.executorAsync(
context: CoroutineContext = MinestomDispatcher,
crossinline block: @KommandDSL SuspendKommandExecutor
) = executorAsync(CoroutineScope(context), block)

/**
* Sets the blocking executor for the command
*
Expand Down
41 changes: 41 additions & 0 deletions src/test/kotlin/net/bladehunt/kotstom/test/CoroutineEventsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.bladehunt.kotstom.test

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.job
import net.bladehunt.kotstom.coroutines.asyncHandler
import net.bladehunt.kotstom.dsl.builder
import net.minestom.server.event.Event
import net.minestom.server.event.EventNode

private class TestEvent : Event

class CoroutineEventsTest :
FunSpec({
beforeAny { SERVER }
test("a failing async handler doesn't cancel its scope") {
val scope = CoroutineScope(Job() + Dispatchers.Unconfined)
val node = EventNode.all("failing")
node.builder<TestEvent> { asyncHandler(scope) { error("boom") } }

node.call(TestEvent())

scope.isActive shouldBe true
}
test("cancelling the scope cancels running async handlers") {
val scope = CoroutineScope(Job() + Dispatchers.Unconfined)
val node = EventNode.all("cancelling")
node.builder<TestEvent> { asyncHandler(scope) { delay(60_000) } }

node.call(TestEvent())
scope.cancel()

scope.coroutineContext.job.children.all { it.isCancelled } shouldBe true
}
})
Loading