Fleshgrinder
02/18/2020, 6:58 PMArrayBuffer
to a Kotlin array and/or vice-versa?
2. How do you await a Promise<S>
while blocking execution? (let x = await f()
)vonox7
02/18/2020, 8:46 PMsuspend fun <T> Promise<T>.await() = suspendCoroutine<T> { continuation ->
then { value -> continuation.resume(value) }
catch { exception ->
@Suppress("USELESS_ELVIS") // Necessary since reject can be called without parameter
continuation.resumeWithException(exception ?: Exception("Empty promise rejection"))
}
}
gbaldeck
02/18/2020, 8:46 PMvonox7
02/18/2020, 8:48 PMFleshgrinder
02/18/2020, 10:32 PMFleshgrinder
02/19/2020, 9:14 AMFleshgrinder
02/19/2020, 9:18 AMrunBlocking
and it is thus impossible to define a function as not being suspend
. Or is there any workaround? (searching…)Fleshgrinder
02/19/2020, 9:47 AMvonox7
02/19/2020, 2:57 PMfun launch(block: suspend () -> Unit) {
async(block).catch { exception -> console.error("Failed with $exception") }
}
vonox7
02/19/2020, 2:57 PMfun <T> async(block: suspend () -> T): Promise<T> = Promise { resolve, reject ->
block.startCoroutine(object : Continuation<T> {
override fun resumeWith(result: Result<T>) {
if (result.isSuccess) {
resolve(result.getOrThrow())
} else {
reject(result.exceptionOrNull()!!)
}
}
override val context: CoroutineContext get() = EmptyCoroutineContext
})
}
Fleshgrinder
02/19/2020, 2:59 PMfun doSomething()
and I want to have this in JS too. The doSomething
does SHA-1, hence, I want to use SubtleCrypto
to have a fast and proper impl. SubtleCrypto.digest
returns a Promise
and now I'm essentially fucked because I cannot resolve this while keeping the contract blocking for all the others.vonox7
02/19/2020, 3:03 PMFleshgrinder
02/19/2020, 3:27 PMexpect fun sha1(data: ByteArray): ByteArray
is in commonMain
.
We want to have actual fun sha1(data: ByteArray): ByteArray
in jsMain
.
We want to use https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest since it is native and tested. It returns a Promise<ArrayBuffer>
(see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#Return_value) which is fine in theory but impossible for us here.
We cannot make an actual suspend fun
unless we have an expect suspend fun
, which we do not have. 😞Fleshgrinder
02/19/2020, 3:34 PM