jsMain.com.zegreatrob.react.dataloader.DataLoaderTools.kt Maven / Gradle / Ivy
package com.zegreatrob.react.dataloader
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
data class DataLoaderTools(val scope: CoroutineScope, val reloadData: ReloadFunc) {
@ExperimentalCoroutinesApi
fun performAsyncWork(
work: suspend () -> R,
errorResult: (Throwable) -> R,
onWorkComplete: (R) -> Unit
) = scope.async { work() }
.handleOnCompletion(onWorkComplete, errorResult)
@ExperimentalCoroutinesApi
private fun Deferred.handleOnCompletion(
onWorkComplete: (R) -> Unit,
errorResult: (Throwable) -> R
) = invokeOnCompletion { throwable ->
if (throwable == null)
getCompleted().let(onWorkComplete)
else
errorResult(throwable).let(onWorkComplete)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy