Skip to content

Commit

Permalink
revert set timeout plugin strategy for hermes runtime, and give optio…
Browse files Browse the repository at this point in the history
…n to override timeout defintions with another plugin
  • Loading branch information
sugarmanz committed Jul 10, 2024
1 parent 6e547e7 commit e1bb8cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ public class HermesRuntime private constructor(mHybridData: HybridData) : Runtim
}

public object Hermes : PlayerRuntimeFactory<Config> {
override fun create(block: Config.() -> Unit): HermesRuntime =
HermesRuntime.create(Config().apply(block))
override fun create(block: Config.() -> Unit): HermesRuntime {
loadHermesJni()
val config = Config().apply(block)
// TODO: Move SetTimeoutPlugin to HeadlessPlayer init once cyclical dep is handled (split out headless impl)
return HermesRuntime.create(config).also(SetTimeoutPlugin(config.coroutineExceptionHandler)::apply)
}

override fun toString(): String = "Hermes"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/** [RuntimePlugin] that adds a `setTimeout` implementation into a the [Runtime] if it doesn't exist */
public class SetTimeoutPlugin(private val exceptionHandler: CoroutineExceptionHandler? = null) :
public class SetTimeoutPlugin(private val exceptionHandler: CoroutineExceptionHandler? = null, private val override: Boolean = false) :
RuntimePlugin, PlayerPlugin {

private var player: Player? = null

@OptIn(ExperimentalPlayerApi::class)
override fun apply(runtime: Runtime<*>) {
if (!runtime.contains("setTimeout")) {
if (override || !runtime.contains("setTimeout")) {
runtime.add("setTimeout") { callback: Invokable<Any?>, timeout: Long ->
if (timeout == 0L) {
callback()
} else runtime.scope.launch(
exceptionHandler ?: runtime.config.coroutineExceptionHandler ?: CoroutineExceptionHandler { _, exception ->
// TODO: Potentially just forward to runtime coroutine exception handler
exceptionHandler ?: CoroutineExceptionHandler { _, exception ->
PlayerPluginException(
"SetTimeoutPlugin",
"Exception throw during setTimeout invocation",
Expand All @@ -43,7 +44,7 @@ public class SetTimeoutPlugin(private val exceptionHandler: CoroutineExceptionHa
}
}

if (!runtime.contains("setImmediate")) {
if (override || !runtime.contains("setImmediate")) {
runtime.add("setImmediate", runtime.executeRaw("(callback => setTimeout(callback, 0))"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ internal class SetTimeoutPluginTest : RuntimeTest() {

@TestTemplate fun `works as intended`() = runBlockingTest {
val exceptions = mutableListOf<Throwable>()
Assertions.assertNull(runtime["setTimeout"])
SetTimeoutPlugin(
CoroutineExceptionHandler { _, exception ->
exception.printStackTrace()
exceptions.add(exception)
},
true,
).apply(runtime)
Assertions.assertNotNull(runtime["setTimeout"])
val promise = (
Expand All @@ -54,12 +54,12 @@ internal class SetTimeoutPluginTest : RuntimeTest() {

@TestTemplate fun `JS exceptions bubble up to the exception handler`() = runBlockingTest {
val exceptions = mutableListOf<Throwable>()
Assertions.assertNull(runtime["setTimeout"])
SetTimeoutPlugin(
CoroutineExceptionHandler { _, exception ->
exception.printStackTrace()
exceptions.add(exception)
},
true,
).apply(runtime)
Assertions.assertNotNull(runtime["setTimeout"])
runtime.execute(
Expand All @@ -78,12 +78,12 @@ internal class SetTimeoutPluginTest : RuntimeTest() {
@TestTemplate fun `releasing runtime doesn't cause a failure`() = runBlockingTest {
val exceptions = mutableListOf<Throwable>()
Assertions.assertTrue(runtime.scope.isActive)
Assertions.assertNull(runtime["setTimeout"])
SetTimeoutPlugin(
CoroutineExceptionHandler { _, exception ->
exception.printStackTrace()
exceptions.add(exception)
},
true,
).apply(runtime)
Assertions.assertNotNull(runtime["setTimeout"])
runtime.execute(
Expand Down

0 comments on commit e1bb8cd

Please sign in to comment.