文件结构

Untitled

ChildReducer (VideoReducer)

import Foundation
import Redux

struct Video: Codable {
    var name: String
    var url: String
}
// 
struct VideoState: Codable, Persistent {
    var fetching: Bool = false
    var data: [Video] = []
    init() {}
    init(persistedState: VideoState) {
        self = persistedState
    }
    func persist() -> VideoState {
        self
    }
}

// Action
struct VideoRequestAction: Action {
}
struct VideoRequestSuceessAction: Action {
    let data: [Video]
}

// reducer
func videoReducer(_ state: VideoState?, _ action: Action) -> VideoState {
    let state = state ?? VideoState()
    if let action = action as? VideoRequestAction {
        return request(state: state, action: action)
    }else if let action = action as? VideoRequestSuceessAction {
        return requestSuccess(state: state, action: action)
    }
    return state
}
let persistentVideoReducer = persistReducer(reducer: videoReducer)

// reducer func
fileprivate func request(state: VideoState, action: VideoRequestAction) -> VideoState {
    var state = state
    state.fetching = true
    return state
}

fileprivate func requestSuccess(state: VideoState, action: VideoRequestSuceessAction) -> VideoState {
    var state = state
    state.fetching = false
    state.data = action.data
    return state
}