fixes in tempUpload of js part

This commit is contained in:
InsanusMokrassar 2022-06-22 22:36:06 +06:00
parent 9cf01ab54f
commit 121e513fdd

View File

@ -4,7 +4,10 @@ import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.ktor.common.TemporalFileId import dev.inmo.micro_utils.ktor.common.TemporalFileId
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.w3c.dom.mediasource.ENDED
import org.w3c.dom.mediasource.ReadyState
import org.w3c.xhr.* import org.w3c.xhr.*
import org.w3c.xhr.XMLHttpRequest.Companion.DONE
suspend fun tempUpload( suspend fun tempUpload(
fullTempUploadDraftPath: String, fullTempUploadDraftPath: String,
@ -12,7 +15,7 @@ suspend fun tempUpload(
onUpload: (Long, Long) -> Unit onUpload: (Long, Long) -> Unit
): TemporalFileId { ): TemporalFileId {
val formData = FormData() val formData = FormData()
val answer = CompletableDeferred<TemporalFileId>() val answer = CompletableDeferred<TemporalFileId>(currentCoroutineContext().job)
formData.append( formData.append(
"data", "data",
@ -37,17 +40,15 @@ suspend fun tempUpload(
request.open("POST", fullTempUploadDraftPath, true) request.open("POST", fullTempUploadDraftPath, true)
request.send(formData) request.send(formData)
val handle = currentCoroutineContext().job.invokeOnCompletion { answer.invokeOnCompletion {
runCatching { runCatching {
if (request.readyState != DONE) {
request.abort() request.abort()
} }
} }
}
return runCatching { return answer.await()
answer.await()
}.also {
handle.dispose()
}.getOrThrow()
} }