Skip to content

4.1. Reducer

Gabriel Brasileiro edited this page Jun 13, 2020 · 5 revisions

Reducer is the contract containing the base methods.

If you would like, you can create your customized ReducerScope.

Default ScopeReducer implementation:

abstract class ReducerScope<S : State, SE : StateEvent>(initialState: S) : Reducer<S, SE> {

    private val state = MutableLiveData<S>()

    init {
        state.value = initialState
    }
    protected fun updateState(update: S.() -> S) {
        val currentState = requireNotNull(state.value)
        state.value = update(currentState)
    }

    override fun getState(): LiveData<S> = state
}
Clone this wiki locally