Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Launch each command in its own coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron committed Aug 5, 2020
1 parent 3f582ec commit 46937df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

## Fixes

* Commands dropping out of the events flow.
* Fixed messages dropping out of the events flow buffer.

## Changes

* CommandProcessor will run commands in parallel.


# 0.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import com.gitlab.kordlib.kordx.commands.model.module.ModuleModifier
import com.gitlab.kordlib.kordx.commands.model.module.forEachModule
import com.gitlab.kordlib.kordx.commands.model.precondition.Precondition
import com.gitlab.kordlib.kordx.commands.model.prefix.Prefix
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.catch
Expand All @@ -36,7 +33,7 @@ data class CommandProcessorData(
val handlers: Map<ProcessorContext<*, *, *>, EventHandler<*>>,
var modifiers: List<ModuleModifier>,
val koin: Koin,
val dispatcher: CoroutineDispatcher = Dispatchers.IO
val dispatcher: CoroutineDispatcher = Dispatchers.Default
)

/**
Expand Down Expand Up @@ -101,7 +98,7 @@ class CommandProcessor(private val data: CommandProcessorData) : CoroutineScope
@Suppress("UNCHECKED_CAST")
fun <T : CommandEvent> getCommand(context: ProcessorContext<*, *, T>, name: String): Command<T>? {
val command = commands[name] ?: return null
return when(command.context) {
return when (command.context) {
CommonContext -> command
context -> command
else -> null
Expand Down Expand Up @@ -137,10 +134,12 @@ class CommandProcessor(private val data: CommandProcessorData) : CoroutineScope
*/
fun <S> addSource(source: EventSource<S>): Job {
return source.events.onEach {
try {
handle(it, source.context.cast())
} catch (exception: Exception) {
logger.catching(exception)
launch(data.dispatcher) {
try {
handle(it, source.context.cast())
} catch (exception: Exception) {
logger.catching(exception)
}
}
}.catch { logger.catching(it) }.launchIn(this)
}
Expand Down

0 comments on commit 46937df

Please sign in to comment.