这两个 reducer 是否在每次动作调度时都被调用?

Do both of these reducers get called on every action dispatch?

func appStateReducer(state: AppState, action: Action) -> AppState {
  var state = state
  state.moviesState = moviesStateReducer(state: state.moviesState, action: 
    action)
  state.peoplesState = peoplesStateReducer(state: state.peoplesState, 
  action: action)
  return state
}

例如会:

store.dispatch(action: MoviesActions.RemoveFromWishlist(movie: movie))

最终调用了 moviesStatepeoplesState reducer? 如果是这样,为什么不在每个操作中只调用一个 reducer 加入条件?

来源:

https://github.com/Dimillian/MovieSwiftUI/blob/master/MovieSwift/Shared/flux/reducers/AppReducer.swift

上级目录来源:

https://github.com/Dimillian/MovieSwiftUI/tree/52e852fc3da2524fd784374727e747eaba8497b6/MovieSwift/Shared/flux

默认情况下,是的,每个动作都会调用所有的 reducer 函数。根据 docs,这不太可能成为性能障碍。通常,只有一个 reducer 进行状态更新,而所有其他 reducer 只是 运行 通过它们的 switch 语句,所以时间复杂度与你拥有的 actions 的数量成比例,这是不太可能的达到足够高的数字需要花费大量时间(10^7 左右)。

但是,您可以使用像 redux-ignore 这样的库来让 reducer 忽略特定的操作 and/or reduxr-scoped-reducer 让 reducer 只响应特定的操作。