NgRx - Reducers priority and execution order against store

Multi tool use
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. A first classic and logical reducer will use it to update the store:
Update
Update_Success
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? 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?
oldItem
Good idea. I didn't thought about it. I ended up using
withLatestFrom
into my effect to get a copy of the entity from store before any change, moved the code logic there and changed the action class constructor to constructor(public payload: Item, public affectedFields?: string) {}
. Now when that effect dispatches UPDATE_ITEM_SUCCESS
my 2d reducer won't need any store parsing as it already receives the fields list as calculated within effect. Thank you @cartant for the suggestion.– Salem Ouerdani
Jul 2 at 6:03
withLatestFrom
constructor(public payload: Item, public affectedFields?: string) {}
UPDATE_ITEM_SUCCESS
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
If the order is important, you could call both reducers from within another reducer so that they are invoked in the order in which you require them to be.
– cartant
Jul 2 at 5:35