From 735cbbdf98befb25cef38c2b2abe6c70d6414b13 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Mon, 14 Sep 2020 00:50:29 +0200 Subject: Upgrade to Krossbow 0.41.0 --- .../sevenwonders/client/SevenWondersClient.kt | 28 +++++++++------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'sw-client/src/commonMain/kotlin/org/luxons') diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt index 05c6bf94..9a9fe356 100644 --- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt +++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt @@ -1,10 +1,6 @@ package org.luxons.sevenwonders.client -import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.serialization.DeserializationStrategy @@ -61,20 +57,17 @@ private suspend inline fun StompSessionWithKx payload: T? = null, serializer: SerializationStrategy, deserializer: DeserializationStrategy, -): U = coroutineScope { - val sub = async(start = CoroutineStart.UNDISPATCHED) { - subscribe(receiveDestination, deserializer).first() - } - delay(30) // ensures the subscription happened +): U { + val sub = subscribe(receiveDestination, deserializer) convertAndSend(sendDestination, payload, serializer) - sub.await() + return sub.first() } class SevenWondersSession(private val stompSession: StompSessionWithKxSerialization) { suspend fun disconnect() = stompSession.disconnect() - fun watchErrors(): Flow = stompSession.subscribe("/user/queue/errors", ErrorDTO.serializer()) + suspend fun watchErrors(): Flow = stompSession.subscribe("/user/queue/errors", ErrorDTO.serializer()) suspend fun chooseName(displayName: String, icon: Icon? = null): ConnectedPlayer = stompSession.request( sendDestination = "/app/chooseName", @@ -84,7 +77,7 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat deserializer = ConnectedPlayer.serializer(), ) - fun watchGames(): Flow> = + suspend fun watchGames(): Flow> = stompSession.subscribe("/topic/games", ListSerializer(LobbyDTO.serializer())) suspend fun createGame(gameName: String): LobbyDTO = stompSession.request( @@ -135,7 +128,8 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat ) } - fun watchLobbyUpdates(): Flow = stompSession.subscribe("/user/queue/lobby/updated", LobbyDTO.serializer()) + suspend fun watchLobbyUpdates(): Flow = + stompSession.subscribe("/user/queue/lobby/updated", LobbyDTO.serializer()) suspend fun awaitGameStart(gameId: Long): PlayerTurnInfo { val startEvents = stompSession.subscribe("/user/queue/lobby/$gameId/started", PlayerTurnInfo.serializer()) @@ -146,20 +140,20 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat stompSession.sendEmptyMsg("/app/lobby/startGame") } - fun watchPlayerReady(gameId: Long): Flow = + suspend fun watchPlayerReady(gameId: Long): Flow = stompSession.subscribe("/topic/game/$gameId/playerReady", String.serializer()) - fun watchPreparedCards(gameId: Long): Flow = + suspend fun watchPreparedCards(gameId: Long): Flow = stompSession.subscribe("/topic/game/$gameId/prepared", PreparedCard.serializer()) - fun watchTurns(): Flow = + suspend fun watchTurns(): Flow = stompSession.subscribe("/user/queue/game/turn", PlayerTurnInfo.serializer()) suspend fun sayReady() { stompSession.sendEmptyMsg("/app/game/sayReady") } - fun watchOwnMoves(): Flow = + suspend fun watchOwnMoves(): Flow = stompSession.subscribe("/user/queue/game/preparedMove", PlayerMove.serializer()) suspend fun prepareMove(move: PlayerMove) { -- cgit