abstract class StateViewModel( private val mainScope: MainScope = object : MainScope { override val coroutineContext: CoroutineContext = Dispatchers.Main }, private val workerScope: WorkerScope = object : WorkerScope { override val coroutineContext: CoroutineContext = Dispatchers.Default }, private val ioScope: IOScope = object : IOScope { override val coroutineContext: CoroutineContext = Dispatchers.IO }, private val commandToStatePipeline: Pipeline ) : ViewModel(), StateModel { protected val _state = MediatorLiveData() private val router = workerScope.selector { commandToStatePipeline.onReceive { _state.postValue(it) } } override val state: LiveData = _state override val commandChannel: SendChannel = commandToStatePipeline override fun onCleared() { mainScope.coroutineContext.cancel(CancellationException("Execution of ViewModel::onCleared")) workerScope.coroutineContext.cancel(CancellationException("Execution of ViewModel::onCleared")) ioScope.coroutineContext.cancel(CancellationException("Execution of ViewModel::onCleared")) super.onCleared() } }