Last active
January 7, 2022 12:52
-
-
Save tunjid/627a0863fee446857238e217ceaf43d2 to your computer and use it in GitHub Desktop.
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
data class State( | |
val count: Int = 0 | |
) | |
sealed class Action { | |
abstract val value: Int | |
data class Add(override val value: Int) : Action() | |
data class Subtract(override val value: Int) : Action() | |
} | |
val mutator : Mutator<Action, StateFlow<State>> = stateFlowMutator<Action, State>( | |
scope = scope, | |
initialState = State(), | |
started = SharingStarted.WhileSubscribed(), | |
transform = { actions -> | |
actions.toMutationStream { | |
when (val action = type()) { | |
is Action.Add -> action.flow | |
.map { | |
Mutation { copy(count = count + value) } | |
} | |
is Action.Subtract -> action.flow | |
.map { | |
Mutation { copy(count = count - value) } | |
} | |
} | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment