From 76e214fc0829d378b283366341493185aea146c8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 22 Nov 2022 14:39:31 +0600 Subject: [PATCH] upfixes --- .../inmo/micro_utils/ktor/client/OnUploadCallback.kt | 2 +- .../micro_utils/ktor/client/ActualTemporalUpload.kt | 11 ++++++++--- .../inmo/micro_utils/ktor/client/ActualUniUpload.kt | 11 +++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/OnUploadCallback.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/OnUploadCallback.kt index 9a5ec689d21..abd79c93b2b 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/OnUploadCallback.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/OnUploadCallback.kt @@ -1,3 +1,3 @@ package dev.inmo.micro_utils.ktor.client -typealias OnUploadCallback = (uploaded: Long, count: Long) -> Unit +typealias OnUploadCallback = suspend (uploaded: Long, count: Long) -> Unit diff --git a/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualTemporalUpload.kt b/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualTemporalUpload.kt index 5ebb93f758c..35a9f89a6eb 100644 --- a/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualTemporalUpload.kt +++ b/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualTemporalUpload.kt @@ -1,6 +1,8 @@ package dev.inmo.micro_utils.ktor.client import dev.inmo.micro_utils.common.MPPFile +import dev.inmo.micro_utils.coroutines.LinkedSupervisorJob +import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions import dev.inmo.micro_utils.ktor.common.TemporalFileId import io.ktor.client.HttpClient import kotlinx.coroutines.* @@ -12,10 +14,11 @@ import org.w3c.xhr.XMLHttpRequest.Companion.DONE suspend fun tempUpload( fullTempUploadDraftPath: String, file: MPPFile, - onUpload: (Long, Long) -> Unit + onUpload: OnUploadCallback ): TemporalFileId { val formData = FormData() val answer = CompletableDeferred(currentCoroutineContext().job) + val subscope = CoroutineScope(currentCoroutineContext().LinkedSupervisorJob()) formData.append( "data", @@ -25,7 +28,7 @@ suspend fun tempUpload( val request = XMLHttpRequest() request.responseType = XMLHttpRequestResponseType.TEXT request.upload.onprogress = { - onUpload(it.loaded.toLong(), it.total.toLong()) + subscope.launchSafelyWithoutExceptions { onUpload(it.loaded.toLong(), it.total.toLong()) } } request.onload = { if (request.status == 200.toShort()) { @@ -48,7 +51,9 @@ suspend fun tempUpload( } } - return answer.await() + return answer.await().also { + subscope.cancel() + } } diff --git a/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualUniUpload.kt b/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualUniUpload.kt index 4d2358dee02..e2a10effbe2 100644 --- a/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualUniUpload.kt +++ b/ktor/client/src/jsMain/kotlin/dev/inmo/micro_utils/ktor/client/ActualUniUpload.kt @@ -2,10 +2,14 @@ package dev.inmo.micro_utils.ktor.client import dev.inmo.micro_utils.common.MPPFile import dev.inmo.micro_utils.common.Progress +import dev.inmo.micro_utils.coroutines.LinkedSupervisorJob +import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions import io.ktor.client.HttpClient import io.ktor.http.Headers import io.ktor.utils.io.core.readBytes import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.cancel import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.job import kotlinx.serialization.DeserializationStrategy @@ -35,6 +39,7 @@ actual suspend fun HttpClient.uniUpload( ): T? { val formData = FormData() val answer = CompletableDeferred(currentCoroutineContext().job) + val subscope = CoroutineScope(currentCoroutineContext().LinkedSupervisorJob()) data.forEach { (k, v) -> when (v) { @@ -60,7 +65,7 @@ actual suspend fun HttpClient.uniUpload( } request.responseType = XMLHttpRequestResponseType.TEXT request.upload.onprogress = { - onUpload(it.loaded.toLong(), it.total.toLong()) + subscope.launchSafelyWithoutExceptions { onUpload(it.loaded.toLong(), it.total.toLong()) } } request.onload = { if (request.status == 200.toShort()) { @@ -85,5 +90,7 @@ actual suspend fun HttpClient.uniUpload( } } - return answer.await() + return answer.await().also { + subscope.cancel() + } }