Dimillian / SwiftUIFlux

A very naive implementation of Redux using Combine BindableObject to serve as an example
Apache License 2.0
654 stars 62 forks source link

Add id for Action protocol #12

Open skywalkerlw opened 4 years ago

skywalkerlw commented 4 years ago

Description

This MR is to add an optional id property to Action protocol

Why

With id added, we don't need to run all cases in reducer, here is an example

// old

func appStateReducer(state: AppState, action: Action) -> AppState {
    var state = state
    state.nextRacesState = nextRacesStateReducer(state: state.nextRacesState, action: action)
    state.timestampSyncState = timestampSyncStateReducer(state: state.timestampSyncState, action: action)
    return state
}

// new

func appStateReducer(state: AppState, action: Action) -> AppState {
    var state = state
    switch action.id {
    case NextRacesActions.actionId:
        state.nextRacesState = nextRacesStateReducer(state: state.nextRacesState, action: action)
    case TimestampSyncActions.actionId:
        state.timestampSyncState = timestampSyncStateReducer(state: state.timestampSyncState, action: action)
    default:
        print("do nothing")
    }
    return state
}
haifengkao commented 4 years ago

why don't you just check the type of action?