diff --git a/redwood-treehouse-guest/build.gradle b/redwood-treehouse-guest/build.gradle index 485b23b0f3..9ba84fc7bf 100644 --- a/redwood-treehouse-guest/build.gradle +++ b/redwood-treehouse-guest/build.gradle @@ -25,6 +25,18 @@ kotlin { api projects.redwoodTreehouseGuestCompose } } + nonJsMain { + dependsOn(commonMain) + } + androidMain { + dependsOn(nonJsMain) + } + jvmMain { + dependsOn(nonJsMain) + } + nativeMain { + dependsOn(nonJsMain) + } } } diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt new file mode 100644 index 0000000000..6f4b21c133 --- /dev/null +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import kotlinx.coroutines.CoroutineExceptionHandler + +/** + * Customize the runtime for this Treehouse application. + */ +internal expect fun prepareEnvironment( + coroutineExceptionHandler: CoroutineExceptionHandler, +) diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt index 2ca6f0264c..e08118f6ce 100644 --- a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt @@ -33,6 +33,7 @@ public class StandardAppLifecycle( internal val json: Json, internal val widgetVersion: UInt, ) : AppLifecycle { + private var started = false private lateinit var host: Host private lateinit var broadcastFrameClock: BroadcastFrameClock @@ -60,9 +61,14 @@ public class StandardAppLifecycle( internal val coroutineScope = CoroutineScope(coroutineExceptionHandler) override fun start(host: Host) { + check(!started) { "already started" } + this.started = true + this.host = host this.broadcastFrameClock = BroadcastFrameClock { host.requestFrame() } this.frameClock = broadcastFrameClock + + prepareEnvironment(coroutineExceptionHandler) } override fun sendFrame(timeNanos: Long) { diff --git a/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt b/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt new file mode 100644 index 0000000000..68bbb31203 --- /dev/null +++ b/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import kotlin.coroutines.CoroutineContext +import kotlinx.coroutines.CoroutineExceptionHandler + +/** + * This installs a global coroutine exception handler using an internal API. + * + * This is necessary for uncaught exceptions to be delivered to the host application. Otherwise + * they're logged to the console where they're easily missed. + * + * This assumes we're the [StandardAppLifecycle] owns the entire JS runtime. + */ +@Suppress("INVISIBLE_MEMBER") // https://github.com/Kotlin/kotlinx.coroutines/issues/3978 +internal actual fun prepareEnvironment( + coroutineExceptionHandler: CoroutineExceptionHandler, +) { + kotlinx.coroutines.internal.ensurePlatformExceptionHandlerLoaded( + object : CoroutineExceptionHandler by coroutineExceptionHandler { + override fun handleException(context: CoroutineContext, exception: Throwable) { + coroutineExceptionHandler.handleException(context, exception) + throw kotlinx.coroutines.internal.ExceptionSuccessfullyProcessed + } + }, + ) +} diff --git a/redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt b/redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt new file mode 100644 index 0000000000..77327aa69a --- /dev/null +++ b/redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import kotlinx.coroutines.CoroutineExceptionHandler + +internal actual fun prepareEnvironment( + coroutineExceptionHandler: CoroutineExceptionHandler, +) { + // Do nothing. +}