From 098b5cc3939854f094d34f58fc95563aed4d28b2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Feb 2021 16:33:48 +0600 Subject: [PATCH] rewrite createAccumulatedUpdatesRetrieverFlow onto channelFlow --- .../utils/updates/retrieving/LongPolling.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index c5ac93a0d4..5e6057d576 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -14,6 +14,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.* import dev.inmo.tgbotapi.utils.* import io.ktor.client.features.HttpRequestTimeoutException +import io.ktor.utils.io.core.use import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.coroutines.coroutineContext @@ -107,17 +108,19 @@ fun TelegramBot.createAccumulatedUpdatesRetrieverFlow( avoidCallbackQueries: Boolean = false, exceptionsHandler: ExceptionHandler? = null, allowedUpdates: List? = null -): Flow = flow { +): Flow = channelFlow { val parentContext = kotlin.coroutines.coroutineContext - retrieveAccumulatedUpdates( - avoidInlineQueries, - avoidCallbackQueries, - CoroutineScope(parentContext), - exceptionsHandler, - allowedUpdates - ) { - withContext(parentContext) { emit(it) } - }.join() + channel.apply { + retrieveAccumulatedUpdates( + avoidInlineQueries, + avoidCallbackQueries, + CoroutineScope(parentContext), + exceptionsHandler, + allowedUpdates, + ::send + ).join() + close() + } } fun TelegramBot.retrieveAccumulatedUpdates(