All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsMain.com.zegreatrob.react.dataloader.DataLoaderTools.kt Maven / Gradle / Ivy

There is a newer version: 5.6.6
Show newest version
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