Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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