All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsMain.dev.inmo.micro_utils.coroutines.SelectFile.kt Maven / Gradle / Ivy

There is a newer version: 0.24.0
Show newest version
package dev.inmo.micro_utils.coroutines

import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.common.selectFile
import kotlinx.coroutines.CompletableDeferred
import org.w3c.dom.HTMLInputElement

suspend fun selectFileOrThrow(
    inputSetup: (HTMLInputElement) -> Unit = {}
): MPPFile {
    val result = CompletableDeferred()

    selectFile(
        inputSetup,
        {
            result.completeExceptionally(it)
        }
    ) {
        result.complete(it)
    }

    return result.await()
}

suspend fun selectFileOrNull(
    inputSetup: (HTMLInputElement) -> Unit = {},
    onFailure: (Throwable) -> Unit = {}
): MPPFile? {
    val result = CompletableDeferred()

    selectFile(
        inputSetup,
        {
            result.complete(null)
            onFailure(it)
        }
    ) {
        result.complete(it)
    }

    return result.await()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy