Change EventBus to Bus

This commit is contained in:
2025-03-18 16:17:50 +01:00
parent cd320b31da
commit 908cc888ad
4 changed files with 13 additions and 15 deletions

View File

@@ -1,11 +1,10 @@
package eventDemo.adapter.infrastructureLayer.event package eventDemo.adapter.infrastructureLayer.event
import eventDemo.business.entity.GameId
import eventDemo.business.event.GameEventBus import eventDemo.business.event.GameEventBus
import eventDemo.business.event.event.GameEvent import eventDemo.business.event.event.GameEvent
import eventDemo.libs.event.EventBus import eventDemo.libs.bus.Bus
import eventDemo.libs.event.EventBusInMemory import eventDemo.libs.bus.BusInMemory
class GameEventBusInMemory : class GameEventBusInMemory :
GameEventBus(), GameEventBus(),
EventBus<GameEvent, GameId> by EventBusInMemory() Bus<GameEvent> by BusInMemory()

View File

@@ -1,12 +1,11 @@
package eventDemo.business.event package eventDemo.business.event
import eventDemo.business.entity.GameId
import eventDemo.business.event.event.GameEvent import eventDemo.business.event.event.GameEvent
import eventDemo.libs.event.EventBus import eventDemo.libs.bus.Bus
import java.util.UUID import java.util.UUID
abstract class GameEventBus : abstract class GameEventBus :
EventBus<GameEvent, GameId>, Bus<GameEvent>,
Comparable<GameEventBus> { Comparable<GameEventBus> {
private val instanceId: UUID = UUID.randomUUID() private val instanceId: UUID = UUID.randomUUID()

View File

@@ -1,7 +1,7 @@
package eventDemo.libs.event package eventDemo.libs.bus
interface EventBus<E : Event<ID>, ID : AggregateId> { interface Bus<E> {
fun publish(event: E) fun publish(item: E)
/** /**
* @param priority The higher the priority, the more it will be called first * @param priority The higher the priority, the more it will be called first

View File

@@ -1,18 +1,18 @@
package eventDemo.libs.event package eventDemo.libs.bus
import io.github.oshai.kotlinlogging.withLoggingContext import io.github.oshai.kotlinlogging.withLoggingContext
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
class EventBusInMemory<E : Event<ID>, ID : AggregateId> : EventBus<E, ID> { class BusInMemory<E> : Bus<E> {
private val subscribers: MutableList<Pair<Int, suspend (E) -> Unit>> = mutableListOf() private val subscribers: MutableList<Pair<Int, suspend (E) -> Unit>> = mutableListOf()
override fun publish(event: E) { override fun publish(item: E) {
subscribers subscribers
.sortedByDescending { (priority, _) -> priority } .sortedByDescending { (priority, _) -> priority }
.forEach { (_, block) -> .forEach { (_, block) ->
runBlocking { runBlocking {
withLoggingContext("event" to event.toString()) { withLoggingContext("busItem" to item.toString()) {
block(event) block(item)
} }
} }
} }