freeletics/CoRedux

Provide a method to create one state - one action reducer

Closed this issue · 0 comments

If #17 would be implemented, following function can be useful to create a reducer that acts on concrete state and action combination:

inline fun <State : Any, Action : Any, reified S : State, reified A : Action> singleActionReducer(
    crossinline reducer: (currentState: S, newAction: A) -> State
): Reducer<State, Action> = { state, action ->
    if (state is S && action is A) reducer(state, action) else state
}

Sample usage:

sealed class State {
    data class Current(val number: Int) : State()
    data class Print(val number: String) : State()
}

sealed class Action {
    object Print : Action()
    object Increase : Action()
    object Decrease : Action()
}

val test = singleActionReducer<State, Action, State.Current, Action.Print> { currentState, _ ->
    State.Print(currentState.number.toString())
}

We should investigate if such inline function is actually useful.

@fraherm Would be interesting to hear your thoughts about this.