Created
April 13, 2022 17:17
-
-
Save linean/3a0ca4746754357895097230d8eb5231 to your computer and use it in GitHub Desktop.
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
private class StateReducerFlowImpl<STATE, EVENT>( | |
initialState: STATE, | |
reduceState: (STATE, EVENT) -> STATE, | |
scope: CoroutineScope | |
) : StateReducerFlow<STATE, EVENT> { | |
private val events = Channel<EVENT>() | |
private val stateFlow = events | |
.receiveAsFlow() | |
.runningFold(initialState, reduceState) | |
.stateIn(scope, Eagerly, initialState) | |
override val replayCache get() = stateFlow.replayCache | |
override val value get() = stateFlow.value | |
override suspend fun collect(collector: FlowCollector<STATE>): Nothing { | |
stateFlow.collect(collector) | |
} | |
override fun handleEvent(event: EVENT) { | |
events.trySend(event) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Article: https://maciej-sady.medium.com/all-you-need-for-mvi-is-kotlin-how-to-reduce-without-reducer-5e986856610f
Project: https://github.com/linean/StateReducerFlow