Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Last active August 6, 2021 06:17
Show Gist options
  • Save abhimuktheeswarar/f7ee4244deda35e467fe0f0b91238383 to your computer and use it in GitHub Desktop.
Save abhimuktheeswarar/f7ee4244deda35e467fe0f0b91238383 to your computer and use it in GitHub Desktop.
CounterSideEffect for blog
class Repository {
suspend fun update(count: Int): Boolean {
TODO("Save to DB")
}
}
class CounterSideEffect(
private val repository: Repository,
private val counterStateStore: CounterStateStore,
) {
init {
counterStateStore.messagesFlow
.onEach(::handle)
.launchIn(counterStateStore.scope)
}
private fun handle(message: CounterMessage) {
when (message) {
IncrementCounter -> {
counterStateStore.scope.launch {
val currentState = counterStateStore.getState()
repository.update(currentState.count)
}
}
DecrementCounter -> TODO()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment