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
import eventDemo.business.entity.GameId
import eventDemo.business.event.GameEventBus
import eventDemo.business.event.event.GameEvent
import eventDemo.libs.event.EventBus
import eventDemo.libs.event.EventBusInMemory
import eventDemo.libs.bus.Bus
import eventDemo.libs.bus.BusInMemory
class GameEventBusInMemory :
GameEventBus(),
EventBus<GameEvent, GameId> by EventBusInMemory()
Bus<GameEvent> by BusInMemory()

View File

@@ -1,12 +1,11 @@
package eventDemo.business.event
import eventDemo.business.entity.GameId
import eventDemo.business.event.event.GameEvent
import eventDemo.libs.event.EventBus
import eventDemo.libs.bus.Bus
import java.util.UUID
abstract class GameEventBus :
EventBus<GameEvent, GameId>,
Bus<GameEvent>,
Comparable<GameEventBus> {
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> {
fun publish(event: E)
interface Bus<E> {
fun publish(item: E)
/**
* @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 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()
override fun publish(event: E) {
override fun publish(item: E) {
subscribers
.sortedByDescending { (priority, _) -> priority }
.forEach { (_, block) ->
runBlocking {
withLoggingContext("event" to event.toString()) {
block(event)
withLoggingContext("busItem" to item.toString()) {
block(item)
}
}
}