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(