move subscribeToBus in configureGameListener function, and use it on test

This commit is contained in:
2025-03-30 03:08:36 +02:00
parent d5d6a48df3
commit 2fb4c778fd
10 changed files with 104 additions and 37 deletions

View File

@@ -19,8 +19,6 @@ import redis.clients.jedis.UnifiedJedis
*/
class GameStateRepositoryInRedis(
eventStore: GameEventStore,
projectionBus: GameProjectionBus,
eventBus: GameEventBus,
jedis: UnifiedJedis,
snapshotConfig: SnapshotConfig = SnapshotConfig(),
) : GameStateRepository {
@@ -36,7 +34,10 @@ class GameStateRepositoryInRedis(
jedis = jedis,
)
init {
fun subscribeToBus(
projectionBus: GameProjectionBus,
eventBus: GameEventBus,
) {
// On new event was received, build snapshot and publish it to the projection bus
eventBus.subscribe { event ->
withLoggingContext("event" to event.toString()) {

View File

@@ -12,9 +12,7 @@ import io.github.oshai.kotlinlogging.withLoggingContext
import java.util.concurrent.ConcurrentSkipListSet
class ReactionListener(
private val projectionBus: GameProjectionBus,
private val eventHandler: GameEventHandler,
private val priority: Int = DEFAULT_PRIORITY,
) {
companion object Config {
const val DEFAULT_PRIORITY = -1000
@@ -23,7 +21,10 @@ class ReactionListener(
private val logger = KotlinLogging.logger { }
fun init() {
fun subscribeToBus(
projectionBus: GameProjectionBus,
priority: Int = DEFAULT_PRIORITY,
) {
if (registeredListeners.add(projectionBus)) {
projectionBus.subscribe(priority) { projection: Projection<GameId> ->
if (projection !is GameState) return@subscribe

View File

@@ -1,10 +1,17 @@
package eventDemo.configuration.business
import eventDemo.adapter.infrastructureLayer.event.projection.GameListRepositoryInRedis
import eventDemo.adapter.infrastructureLayer.event.projection.GameStateRepositoryInRedis
import eventDemo.business.event.projection.projectionListener.ReactionListener
import io.ktor.server.application.Application
import org.koin.ktor.ext.get
import org.koin.core.Koin
fun Application.configureGameListener() {
ReactionListener(get(), get())
.init()
fun Koin.configureGameListener() {
ReactionListener(get())
.subscribeToBus(get())
get<GameStateRepositoryInRedis>()
.subscribeToBus(get(), get())
get<GameListRepositoryInRedis>()
.subscribeToBus(get(), get())
}

View File

@@ -43,7 +43,7 @@ fun Module.configureDIInfrastructure(config: Configuration) {
singleOf(::GameProjectionBusInMemory) bind GameProjectionBus::class
single {
GameStateRepositoryInRedis(get(), get(), get(), get(), snapshotConfig = SnapshotConfig())
GameStateRepositoryInRedis(get(), get(), snapshotConfig = SnapshotConfig())
} bind GameStateRepository::class
single {