API: Reducer Composition Helpers

  • Combines multiple reducer functions into a single reducer function. Each reducer function must reduce the same type.

    Usually, you will want to to convert multiple key-specific reducers into a single reducer to represent an entire type using forKey(_:use:).

    struct Scoreboard {
        var home = 0
        var away = 0
    }
    
    let scoreboardReducer: ReducerFn<Scoreboard> = combineReducers(
       forKey(\.home, use: homeScoreReducer),
       forKey(\.away, use: awayScoreReducer)
    )
    

    Declaration

    Swift

    public func combineReducers<S>(_ reducers: ReducerFn<S>...) -> ReducerFn<S>

    Parameters

    reducers

    reducers to be combined into a single reducer.

  • Convert a reducer function for a specific instance property into a reducer function for the entire instance.

    Usually paired with combineReducers(_:).

    The following example converts a reducer that only reduces the home score into a reducer that reduces the entire Scoreboard:

    struct Scoreboard {
        var home = 0
        var away = 0
    }
    
    let homeScoreReducer: ReducerFn<Int> = { (state: Int, action) in
        var state = state
        switch action {
            case let action as HomeScore:
                state += 1
                return state
            default:
                return state
        }
    }
    
    let homeKeyReducer: ReducerFn<Scoreboard> = forKey(\.home, use: homeScoreReducer)
    

    Declaration

    Swift

    public func forKey<S, K>(_ keyPath: WritableKeyPath<S, K>, use reducer: @escaping ReducerFn<K>) -> ReducerFn<S>

    Parameters

    keyPath

    The key of the instance property to be reduced.

    reducer

    The reducer function for the given instance property.