main.kotlin.com.intuit.playerui.j2v8.extensions.Lock.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2v8 Show documentation
Show all versions of j2v8 Show documentation
A cross-platform semantic rendering engine
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) }