Closed AdditionAddict closed 4 years ago
Full chain following the source code:
mergeQuerySet → this.dispatch(new MergeQuerySet(querySet, mergeStrategy, tag)); (entity-cache-dispatcher.ts) → this.mergeQuerySetReducer(entityCache, action as MergeQuerySet); (entity-cache-reducer.ts) → this.applyCollectionReducer(entityCache, act); (entity-cache-reducer.ts)
Where applyCollectionReducer is run for each entityName with entityOp UPSERT_MANY + PreserveChanges is passed in action
The problem with this is that in upsertMany
shown below, this.adapter.upsertMany(entities, collection);
isn't filtering out changed entities and trackUpsertMany
(and others like trackAddMany, trackAddOne) isn't making any use of mergeStrategy like mergeServerUpserts
(which is why GET_ALL works with mergeStrategy demo) but this doesn't.
protected upsertMany(
collection: EntityCollection<T>,
action: EntityAction<T[]>
): EntityCollection<T> {
// <v6: payload must be an array of `Updates<T>`, not entities
// v6+: payload must be an array of T
const entities = this.guard.mustBeEntities(action);
const mergeStrategy = this.extractMergeStrategy(action);
collection = this.entityChangeTracker.trackUpsertMany(
entities,
collection,
mergeStrategy
);
return this.adapter.upsertMany(entities, collection);
}
I'm generally curious if a refactor where we keep entityOriginalCache
and entityChangedCache
as separate normalised states and make heavier use of @ngrx/entity
if this would prevent some these bugs. Obviously this would be breaking for anyone using changeState directly but the overarching API could be kept?
within mergeQuerySetReducer
all current tests pass changing from const entityOp = EntityOp.UPSERT_MANY;
to const entityOp = EntityOp.QUERY_ALL_SUCCESS;
and this uses the happier path mergeQueryResults
→ mergeServerUpserts
Minimal reproduction of the bug/regression with instructions:
see
mergePreserveChanges()
inapp.component.ts
in this stackblitzafter some setup the state (1 x added, 1 x updated, 1 x deleted) is:
I'm dispatching a
mergeQuerySet
withMergeStrategy.PreserveChanges
strategyHere I've updated the power to -1:
Expected behavior:
Reference https://ngrx.io/guide/data/entity-change-tracker#merge-strategies
Expected state:
Actual state:
Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):
ngrx 8.6.0 angular 8
Other information:
I'll look through source and see if I can figure out the cause.
I would be willing to submit a PR to fix this issue
[x] Yes (Assistance is provided if you need help submitting a pull request) [ ] No