From 121e513fdd9d2da3ad890463f985441cf4512568 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 22 Jun 2022 22:36:06 +0600 Subject: [PATCH] fixes in tempUpload of js part --- .../ktor/client/ActualTemporalUpload.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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 0824c19c516..33c8a4910f5 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 @@ -4,7 +4,10 @@ import dev.inmo.micro_utils.common.MPPFile import dev.inmo.micro_utils.ktor.common.TemporalFileId import io.ktor.client.HttpClient import kotlinx.coroutines.* +import org.w3c.dom.mediasource.ENDED +import org.w3c.dom.mediasource.ReadyState import org.w3c.xhr.* +import org.w3c.xhr.XMLHttpRequest.Companion.DONE suspend fun tempUpload( fullTempUploadDraftPath: String, @@ -12,7 +15,7 @@ suspend fun tempUpload( onUpload: (Long, Long) -> Unit ): TemporalFileId { val formData = FormData() - val answer = CompletableDeferred() + val answer = CompletableDeferred(currentCoroutineContext().job) formData.append( "data", @@ -37,17 +40,15 @@ suspend fun tempUpload( request.open("POST", fullTempUploadDraftPath, true) request.send(formData) - val handle = currentCoroutineContext().job.invokeOnCompletion { + answer.invokeOnCompletion { runCatching { - request.abort() + if (request.readyState != DONE) { + request.abort() + } } } - return runCatching { - answer.await() - }.also { - handle.dispose() - }.getOrThrow() + return answer.await() }