commonMain.pro.respawn.flowmvi.modules.Recoverable.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-jvm Show documentation
Show all versions of core-jvm Show documentation
A Kotlin Multiplatform MVI library based on plugins that is simple, fast, powerful & flexible
package pro.respawn.flowmvi.modules
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch
import pro.respawn.flowmvi.api.DelicateStoreApi
import pro.respawn.flowmvi.api.MVIAction
import pro.respawn.flowmvi.api.MVIIntent
import pro.respawn.flowmvi.api.MVIState
import pro.respawn.flowmvi.api.PipelineContext
import pro.respawn.flowmvi.api.Recoverable
@OptIn(DelicateStoreApi::class)
@Suppress("FunctionName")
internal fun Recoverable.PipelineExceptionHandler(
pipeline: PipelineContext,
) = CoroutineExceptionHandler { ctx, e ->
when {
e !is Exception -> throw e
ctx[Recoverable] != null -> throw e
// add Recoverable to the coroutine context
// and handle the exception asynchronously to allow suspending inside recover
else -> with(pipeline) { launch(this@PipelineExceptionHandler) { recover(e) } }
}
}
@DelicateStoreApi
internal suspend inline fun Recoverable.catch(
ctx: PipelineContext,
block: () -> Unit
) = try {
block()
} catch (e: CancellationException) {
throw e
} catch (expected: Exception) {
with(ctx) { recover(expected) }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy