CounterSideEffect for blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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