Skip to content

Commit

Permalink
Merge pull request #359 from A1shK/debuggable-timeout
Browse files Browse the repository at this point in the history
[Android] Allowing timeout to be configured through PlayerRuntimeConfig
  • Loading branch information
nancywu1 authored May 22, 2024
2 parents cef2a5f + 3e8b091 commit 5eab9a8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,6 @@ public class AndroidPlayer private constructor(
public data class Config(
override var debuggable: Boolean = false,
override var coroutineExceptionHandler: CoroutineExceptionHandler? = null,
override var timeout: Long = if (debuggable) Int.MAX_VALUE.toLong() else 5000,
) : PlayerRuntimeConfig()
}
13 changes: 12 additions & 1 deletion docs/site/pages/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,19 @@ var body: some View {

```kotlin
// create Android Player with reference assets plugin
val player = AndroidPlayer(ReferenceAssetsPlugin())
val player = AndroidPlayer(
listOf(
ReferenceAssetsPlugin(),
// Any other plugins
)
)
```
Apart from providing a list of plugins while setting up `AndroidPlayer`, you can also provide a `config` object that has the following options:
- `debuggable` - Indicates if the runtime is debuggable on android through chromium devtools, enabling this would let you set breakpoints in js code as you're running it on android.
- `coroutineExceptionHandler` - [CoroutineExceptionHandler](https://kotlinlang.org/docs/exception-handling.html#coroutineexceptionhandler) should handle all uncaught exceptions while using the runtime coroutine scope.
- `timeout` - Timeout for the JS thread. If none is provided then it is set as `if (debuggable) Int.MAX_VALUE.toLong() else 5000`.


</android>
</PlatformTabs>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import kotlinx.coroutines.CoroutineExceptionHandler
public open class PlayerRuntimeConfig {
public open var debuggable: Boolean = false
public open var coroutineExceptionHandler: CoroutineExceptionHandler? = null
public open var timeout: Long = if (debuggable) Int.MAX_VALUE.toLong() else 5000
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public data class J2V8RuntimeConfig(
private val explicitExecutorService: ExecutorService? = null,
override var debuggable: Boolean = false,
override var coroutineExceptionHandler: CoroutineExceptionHandler? = null,
override var timeout: Long = if (debuggable) Int.MAX_VALUE.toLong() else 5000,
) : PlayerRuntimeConfig() {
public val executorService: ExecutorService by lazy {
explicitExecutorService ?: Executors.newSingleThreadExecutor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import kotlinx.coroutines.withTimeout

internal suspend fun <Context : V8Value, T> Context.evaluateInJSThread(
runtime: Runtime<V8Value>,
timeout: Long = 5000,
block: suspend Context.() -> T,
): T = withTimeout(timeout) {
): T = withTimeout(runtime.config.timeout) {
if (runtime.isReleased()) throw PlayerRuntimeException(runtime, "Runtime object has been released!")
withContext(runtime.dispatcher) {
runtime.scope.ensureActive()
Expand All @@ -22,7 +21,6 @@ internal suspend fun <Context : V8Value, T> Context.evaluateInJSThread(

internal fun <Context : V8Value, T> Context.evaluateInJSThreadBlocking(
runtime: Runtime<V8Value>,
timeout: Long = 5000,
block: Context.() -> T,
): T {
if (runtime.isReleased()) throw PlayerRuntimeException(runtime, "Runtime object has been released!")
Expand All @@ -32,19 +30,17 @@ internal fun <Context : V8Value, T> Context.evaluateInJSThreadBlocking(
} else {
runtime.checkBlockingThread(Thread.currentThread())
runBlocking {
evaluateInJSThread(runtime, timeout, block)
evaluateInJSThread(runtime, block)
}
}
}

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

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

0 comments on commit 5eab9a8

Please sign in to comment.