Skip to content

Instantly share code, notes, and snippets.

@yshrsmz
Created December 5, 2019 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yshrsmz/3599d0014993af604bf0378f6843c59f to your computer and use it in GitHub Desktop.
Save yshrsmz/3599d0014993af604bf0378f6843c59f to your computer and use it in GitHub Desktop.
AbstractStateNotifier
abstract class AbstractStateNotifier<ACTION : Action, STATE : State, EFFECT : Effect, VM : MviViewModel<*, ACTION, STATE, EFFECT>>(
uiContext: CoroutineContext
) : CoroutineScope {
private val job = SupervisorJob()
override val coroutineContext: CoroutineContext = job + uiContext
fun stateChanged(vm: VM, state: (state: STATE) -> Unit): Job {
return launch { vm.states.collect { state(it) } }
}
fun effectReceived(vm: VM, effect: (effect: EFFECT) -> Unit): Job {
return launch { vm.effects.collectUnhandled { effect(it) } }
}
fun dispose() {
job.cancel()
}
}
class DepositProductDetailVMStateNotifier(uiContext: CoroutineContext) :
AbstractStateNotifier<
DepositProductDetailAction,
DepositProductDetailState,
DepositProductDetailEffect,
DepositProductDetailViewModel>(uiContext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment