remove priority of subscribe

This commit is contained in:
2025-04-05 05:13:32 +02:00
parent 0bfeef8a91
commit 080ea1245d
5 changed files with 11 additions and 32 deletions

View File

@@ -9,25 +9,21 @@ 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()
private val subscribers: MutableList<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) ->
.forEach {
coroutineScope {
block(item)
it(item)
}
}
}
}
override fun subscribe(
priority: Int,
block: suspend (E) -> Unit,
) {
subscribers.add(priority to block)
override fun subscribe(block: suspend (E) -> Unit) {
subscribers.add(block)
}
}