From 6842a503099a42c88c98df60c3e4db9529e9e624 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Sat, 13 Feb 2021 16:46:02 +0100 Subject: Fix potential race in chooseName --- .../sevenwonders/client/SevenWondersClient.kt | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'sw-client/src/commonMain/kotlin/org') 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 3a3cb989..50b9b70c 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 @@ -42,16 +42,21 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat suspend fun watchErrors(): Flow = stompSession.subscribe("/user/queue/errors", ErrorDTO.serializer()) suspend fun chooseName(displayName: String, icon: Icon? = null, isHuman: Boolean = true): ConnectedPlayer { - val sub = stompSession.subscribe( - destination = "/user/queue/nameChoice", - deserializer = ConnectedPlayer.serializer(), + return doAndWaitForEvent( + send = { + stompSession.convertAndSend( + destination = "/app/chooseName", + body = ChooseNameAction(displayName, icon, isHuman), + serializer = ChooseNameAction.serializer(), + ) + }, + subscribe = { + stompSession.subscribe( + destination = "/user/queue/nameChoice", + deserializer = ConnectedPlayer.serializer(), + ) + } ) - stompSession.convertAndSend( - destination = "/app/chooseName", - body = ChooseNameAction(displayName, icon, isHuman), - serializer = ChooseNameAction.serializer(), - ) - return sub.first() } suspend fun watchGames(): Flow = -- cgit