package com.xty.common.work

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hjq.toast.ToastUtils
import com.xty.common.LogUtils
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope

class WorkHttp  : ViewModel(){

    val handlerExcept by lazy {
        CoroutineExceptionHandler { coroutineContext, throwable ->
            LogUtils.d("startHttp", "异常处理")
            try {

                // nowData.postValue()
                LogUtils.e("http", throwable.message!!)
                ToastUtils.show(throwable.message)
            } catch (e: Exception) {
                ToastUtils.show("报错拉")
            }
        }
    }

    fun startHttp( http: suspend () -> Unit) {
        viewModelScope.launch(handlerExcept) {
            supervisorScope {
                launch(handlerExcept) {
                    http.invoke()

                }
            }
        }
    }
}