From 2670da7aa033cd18d38d697739d04ca758008d41 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Sat, 12 Dec 2020 19:56:08 +0100 Subject: Cleanup helper method in sw-client --- .../sevenwonders/client/SevenWondersClient.kt | 36 ++++++++-------------- 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'sw-client') 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 02f7f2da..090d1ee2 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,12 +1,9 @@ package org.luxons.sevenwonders.client -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map -import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.builtins.serializer import org.hildan.krossbow.stomp.StompClient import org.hildan.krossbow.stomp.config.HeartBeat @@ -39,33 +36,24 @@ class SevenWondersClient { } } -// TODO replace these calls by actual HTTP endpoints -@OptIn(ExperimentalCoroutinesApi::class) -private suspend inline fun StompSessionWithKxSerialization.request( - sendDestination: String, - receiveDestination: String, - payload: T? = null, - serializer: SerializationStrategy, - deserializer: DeserializationStrategy, -): U { - val sub = subscribe(receiveDestination, deserializer) - convertAndSend(sendDestination, payload, serializer) - return sub.first() -} - class SevenWondersSession(private val stompSession: StompSessionWithKxSerialization) { suspend fun disconnect() = stompSession.disconnect() 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", - receiveDestination = "/user/queue/nameChoice", - payload = ChooseNameAction(displayName, icon), - serializer = ChooseNameAction.serializer(), - deserializer = ConnectedPlayer.serializer(), - ) + suspend fun chooseName(displayName: String, icon: Icon? = null): ConnectedPlayer { + val sub = stompSession.subscribe( + destination = "/user/queue/nameChoice", + deserializer = ConnectedPlayer.serializer(), + ) + stompSession.convertAndSend( + destination = "/app/chooseName", + body = ChooseNameAction(displayName, icon), + serializer = ChooseNameAction.serializer(), + ) + return sub.first() + } suspend fun watchGames(): Flow = stompSession.subscribe("/topic/games", GameListEventWrapper.serializer()).map { it.event } -- cgit