Skip to content

Instantly share code, notes, and snippets.

@tunjid
Last active January 7, 2022 12:52
Show Gist options
  • Save tunjid/627a0863fee446857238e217ceaf43d2 to your computer and use it in GitHub Desktop.
Save tunjid/627a0863fee446857238e217ceaf43d2 to your computer and use it in GitHub Desktop.
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