Add name to InMemory services

This commit is contained in:
2025-04-05 04:46:02 +02:00
parent 147ae47155
commit d05bf62bf0
6 changed files with 14 additions and 4 deletions

View File

@@ -1,13 +1,19 @@
package eventDemo.libs.bus
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.withLoggingContext
import kotlinx.coroutines.coroutineScope
import kotlin.reflect.KClass
class BusInMemory<E> : Bus<E> {
class BusInMemory<E>(
val name: KClass<*> = BusInMemory::class,
) : Bus<E> {
private val logger = KotlinLogging.logger(name.qualifiedName.toString())
private val subscribers: MutableList<Pair<Int, suspend (E) -> Unit>> = mutableListOf()
override suspend fun publish(item: E) {
withLoggingContext("busItem" to item.toString()) {
logger.info { "Item sent to the bus: $item" }
subscribers
.sortedByDescending { (priority, _) -> priority }
.forEach { (_, block) ->