简体   繁体   English

NgRx-针对商店的减速器优先级和执行顺序

[英]NgRx - Reducers priority and execution order against store

If different reducers are associated to the same action and are performing changes to the store, are they receiving the same version of it before any edit happens? 如果不同的化简器与同一动作相关联并且正在对商店进行更改,那么在进行任何编辑之前,它们是否收到了相同的版本? Is there a priority to consider? 有优先考虑的地方吗?

Example: 例:

Lets assume you dispatch an action Update to edit an entity on server side, once data is successfully updated, an effect will dispatch an Update_Success action with an instance of the newly received version of entity as payload. 假设您在服务器端调度了一个操作Update以编辑实体,一旦数据成功更新,一个效果将调度一个Update_Success动作,并以新接收到的实体版本的实例作为有效负载。 A first classic and logical reducer will use it to update the store: 第一个经典的逻辑化约简器将使用它来更新商店:

case ItemActionTypes.UPDATE_ITEM_SUCCESS: {
    const item = action.payload;

    return {
    ...adapter.upsertOne(item, state),
    loaded: true,
    loading: false
    };
}

Now lets assume that in a different file, you have a different reducer associated to the same action and needs to compare the newly received entity against the old one in store: 现在假设在另一个文件中,您具有与同一操作关联的另一个化简器,并且需要将新接收的实体与商店中的旧实体进行比较:

case ItemActionTypes.UPDATE_ITEM_SUCCESS: {
    const item = action.payload;
    const oldItem = state.entities[item.id];
    const changes = getAffectedProperties(item, oldItem);

    // ...
}

The question is: Is there any chance oldItem is actually holding the newly received item instead of the old one as the first reducer may have already updated it? 问题是:由于第一个reducer可能已经更新了旧项目,所以oldItem实际上会保存新收到的项目而不是旧项目吗? Is it a first come first serve case? 先到先得案吗? or is there anything on its implementation that guarantees all reducers are performing changes to the the same version of the store and maybe merging those changes at a further step? 还是在实现上有什么保证所有的reducer都对同一版本的商店进行更改,并可能将这些更改进一步合并?

If different reducers are associated to the same action and are performing changes to the store, are they receiving the same version of it before any edit happens? 如果不同的化简器与同一动作相关联并且正在对商店进行更改,那么在进行任何编辑之前,它们是否收到了相同的版本? Is there a priority to consider? 有优先考虑的地方吗?

Reducers only have access to their piece of state. 减速器只能访问其状态。 So you're not supposed to really care about that. 因此,您不应该真正在乎这一点。

Also, when one action is dispatched, all the reducers are called in a synchronous way. 同样,在分派一个动作时,所有减速器都以同步方式调用。 Which means that if for the ACTION_A you're modifying your store from different places (/ different reducers), your selectors (or even the view), will only be refreshed once. 这意味着,如果您要在ACTION_A从不同的地方(/不同的缩减器)修改商店,则选择器(甚至是视图)将仅刷新一次。 When all the updates for that action are applied on the store. 当该操作的所有更新都应用到商店时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM