diff --git a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt index 241d377e..e7233e21 100644 --- a/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt +++ b/core/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/ktor/client/content/WriteContentRepoKtorClient.kt @@ -16,15 +16,13 @@ class WriteContentRepoKtorClient( private val baseUrl: String, private val client: HttpClient = HttpClient() ) : WriteContentRepo { - override val contentCreatedFlow: Flow = createStandardWebsocketFlow( - client, + override val contentCreatedFlow: Flow = client.createStandardWebsocketFlow( "$baseUrl/$contentCreatedFlowRoute" ) { standardKtorSerialFormat.load(RegisteredContent.serializer(), it) } - override val contentDeletedFlow: Flow = createStandardWebsocketFlow( - client, + override val contentDeletedFlow: Flow = client.createStandardWebsocketFlow( "$baseUrl/$contentDeletedFlowRoute" ) { standardKtorSerialFormat.load(RegisteredContent.serializer(), it) diff --git a/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/FlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/FlowsWebsockets.kt index 9f648d3e..21fea0fe 100644 --- a/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/FlowsWebsockets.kt +++ b/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/client/FlowsWebsockets.kt @@ -11,8 +11,7 @@ import kotlinx.coroutines.flow.* * @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish * connection. Must return true in case if must be reconnected. By default always reconnecting */ -inline fun createStandardWebsocketFlow( - client: HttpClient, +inline fun HttpClient.createStandardWebsocketFlow( url: String, crossinline checkReconnection: (Throwable?) -> Boolean = { true }, crossinline conversation: suspend (ByteArray) -> T @@ -20,7 +19,7 @@ inline fun createStandardWebsocketFlow( val correctedUrl = url.asCorrectWebSocketUrl return channelFlow { - val producerScope = this + val producerScope = this@channelFlow do { val reconnect = try { safely( @@ -28,7 +27,7 @@ inline fun createStandardWebsocketFlow( throw it } ) { - client.ws( + ws( correctedUrl ) { while (true) { @@ -62,8 +61,3 @@ inline fun createStandardWebsocketFlow( } } } - -inline fun HttpClient.createStandardWebsocketFlow( - url: String, - crossinline conversation: suspend (ByteArray) -> T -) = createStandardWebsocketFlow(this, url, conversation) diff --git a/ktor/tests/src/test/kotlin/com/insanusmokrassar/postssystem/ktor/tests/WebsocketsTest.kt b/ktor/tests/src/test/kotlin/com/insanusmokrassar/postssystem/ktor/tests/WebsocketsTest.kt index a38b0b13..c94c495f 100644 --- a/ktor/tests/src/test/kotlin/com/insanusmokrassar/postssystem/ktor/tests/WebsocketsTest.kt +++ b/ktor/tests/src/test/kotlin/com/insanusmokrassar/postssystem/ktor/tests/WebsocketsTest.kt @@ -40,8 +40,7 @@ class WebsocketsTest { val client = HttpClient { install(io.ktor.client.features.websocket.WebSockets) } - val incomingWebsocketFlow = createStandardWebsocketFlow( - client, + val incomingWebsocketFlow = client.createStandardWebsocketFlow( "$serverUrl/$suburl", { false } // always skip reconnection ) {