package com.atmob.keyboard_android.mvvm.base

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
 * 协程回调
 */
typealias block = suspend () -> Unit

/**
 * ViewModel基类
 */
open class BaseViewModel : ViewModel(), DefaultLifecycleObserver {
    /**
     * 开启一个协程
     */
    protected fun launch(
        block: block
    ): Job {
        return viewModelScope.launch {
            try {
                block.invoke()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}