Concurrency.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/Concurrency.kt
1package eu.friedcutlet.yokan.services23import kotlinx.coroutines.CoroutineDispatcher4import kotlinx.coroutines.CoroutineScope5import kotlinx.coroutines.Job6import kotlinx.coroutines.NonCancellable7import kotlinx.coroutines.async8import kotlinx.coroutines.cancelAndJoin9import kotlinx.coroutines.currentCoroutineContext10import kotlinx.coroutines.ensureActive11import kotlinx.coroutines.job12import kotlinx.coroutines.withContext13import kotlin.coroutines.CoroutineContext14import kotlin.coroutines.EmptyCoroutineContext1516/**17 * `runBlocking` for call sites that must bridge into suspending code from a18 * synchronous context (extension bridge callbacks, startup checks). JVM and19 * native targets delegate to `kotlinx.coroutines.runBlocking`; the browser20 * cannot block its single thread, so the wasm actual throws21 * [UnsupportedOperationException] — callers already wrap these paths in22 * `runCatching` and degrade gracefully. A [kotlinx.coroutines.Job] in [context]23 * makes the blocked work its child, so cancelling that job cancels the work.24 */25expect fun <T> runBlockingCompat(26 context: CoroutineContext = EmptyCoroutineContext,27 block: suspend CoroutineScope.() -> T,28): T2930/**31 * Dispatcher for work that blocks its thread (extension engines and their synchronous host32 * bridges): `Dispatchers.IO` where the platform has one, `Dispatchers.Default` in the browser.33 */34expect val blockingDispatcher: CoroutineDispatcher3536/**37 * [runCatching] for suspending work: failures become [Result.failure], but38 * [kotlinx.coroutines.CancellationException] is rethrown so cancellation still39 * propagates to the caller's scope.40 */41inline fun <T> catchingNonCancellation(block: () -> T): Result<T> =42 try {43 Result.success(block())44 } catch (cancelled: kotlinx.coroutines.CancellationException) {45 throw cancelled46 } catch (failure: Throwable) {47 Result.failure(failure)48 }4950/**51 * Runs [action] as a child of this owner scope and joins it before returning, so the work never52 * outlives the owner: cancelling the caller or the owner cancels the work, and the caller resumes53 * only after the work's finalizers have finished. [admit] runs before dispatch and throws to54 * reject the call. A result produced after the call or the owner was cancelled is discarded,55 * because a delegate may swallow cancellation or return from a `NonCancellable` finalizer.56 */57internal suspend fun <T> CoroutineScope.runJoined(58 context: CoroutineContext = EmptyCoroutineContext,59 admit: () -> Unit = {},60 action: suspend (callJob: Job) -> T,61): T {62 // An expired caller must not dispatch even when the owner's dispatcher is immediate.63 currentCoroutineContext().ensureActive()64 admit()65 ensureActive()66 val work = async(context) {67 val callJob = currentCoroutineContext().job68 callJob.ensureActive()69 action(callJob).also {70 callJob.ensureActive()71 this@runJoined.ensureActive()72 }73 }74 return try {75 work.await()76 } finally {77 withContext(NonCancellable) { work.cancelAndJoin() }78 }79}80