Last active
August 6, 2021 06:17
-
-
Save abhimuktheeswarar/f7ee4244deda35e467fe0f0b91238383 to your computer and use it in GitHub Desktop.
CounterSideEffect for blog
This file contains hidden or 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