package com.orange.origin.origin

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

open class BaseViewModel : ViewModel(){

     fun <T> request(
        work:suspend ()-> T,
        respond:(T)->Unit,
        error:(Throwable)->Unit
    ){
        viewModelScope.launch {
            kotlin.runCatching {
                withContext(Dispatchers.IO){
                    work()
                }

            }.onSuccess {
                respond(it)
            }.onFailure {
                error(it)
            }

        }

    }
}