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

main.kotlin.com.intuit.playerui.j2v8.extensions.Lock.kt Maven / Gradle / Ivy

There is a newer version: 0.10.0-next.5
Show newest version
package com.intuit.playerui.j2v8.extensions

import com.eclipsesource.v8.V8Value
import com.intuit.playerui.core.bridge.PlayerRuntimeException
import com.intuit.playerui.core.bridge.runtime.Runtime
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout

internal suspend fun  Context.evaluateInJSThread(
    runtime: Runtime,
    block: suspend Context.() -> T,
): T = withTimeout(runtime.config.timeout) {
    if (runtime.isReleased()) throw PlayerRuntimeException(runtime, "Runtime object has been released!")
    withContext(runtime.dispatcher) {
        runtime.scope.ensureActive()
        block()
    }
}

internal fun  Context.evaluateInJSThreadBlocking(
    runtime: Runtime,
    block: Context.() -> T,
): T {
    if (runtime.isReleased()) throw PlayerRuntimeException(runtime, "Runtime object has been released!")
    // if we're already on the dispatcher thread, DON'T BLOCK
    return if ([email protected]()) {
        block()
    } else {
        runtime.checkBlockingThread(Thread.currentThread())
        runBlocking {
            evaluateInJSThread(runtime, block)
        }
    }
}

internal suspend fun  Context.evaluateInJSThreadIfDefined(
    runtime: Runtime,
    block: suspend Context.() -> T,
): T? = mapUndefinedToNull()?.let { evaluateInJSThread(runtime, block) }

internal fun  Context.evaluateInJSThreadIfDefinedBlocking(
    runtime: Runtime,
    block: Context.() -> T,
): T? = mapUndefinedToNull()?.let { evaluateInJSThreadBlocking(runtime, block) }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy