Yokan / rebuild
Course 1 8624561

BackgroundTaskCompletion.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/scheduling/BackgroundTaskCompletion.kt

implementation23 lines
1package eu.friedcutlet.yokan.services.scheduling23import eu.friedcutlet.yokan.services.catchingNonCancellation4import kotlinx.coroutines.CoroutineScope5import kotlinx.coroutines.Job6import kotlinx.coroutines.flow.MutableStateFlow7import kotlinx.coroutines.flow.update8import kotlinx.coroutines.launch910/** Completes even when expiry cancels a callback before dispatch, and only after its cleanup joins. */11internal fun launchCompletingBackgroundTask(12    scope: CoroutineScope,13    run: suspend () -> ProfileWorkResult<PeriodicTaskOutcome>,14    complete: (Boolean) -> Unit,15): Job {16    val succeeded = MutableStateFlow(false)17    val job = scope.launch {18        val outcome = catchingNonCancellation { run() }.getOrNull()19        succeeded.update { outcome?.shouldRetry == false }20    }21    job.invokeOnCompletion { cause -> complete(cause == null && succeeded.value) }22    return job23}24

Search chapters, reference and source filenames.