2

How can i configuration ngrx/data that when i call getAll (example) 10 seconds intervall from entityService what use http. I would like change all (remove all not exist data). I experience it will merge and not remove what already dont exist.

If possible I would avoid the clearCache function.

export interface IItem {
    id: number;
    data: string;
}
export class MyEntityService extends EntityCollectionServiceBase<IItem> {
    constructor(serviceElementsFactory: EntityCollectionServiceElementsFactory) {
        super("entityKey", serviceElementsFactory);
    }

    // I tried all MergeStrategy but it is not solution for me.
    //
    // getAll(options: EntityActionOptions = { mergeStrategy: MergeStrategy.IgnoreChanges }): Observable<IItem[]> {
    //     return super.getAll(options);
    // }
}
export class MyDataService extends DefaultDataService<IItem> {
    constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
        super("entityKey", http, httpUrlGenerator);
    }

    getAll(): Observable<IIncidentItem[]> {
        return this.http.get<IItem[]>('/api/items');
    }
}
export class AnyComponent implements OnDestroy {
    readonly count: Observable<number>;
    private readonly sequenceUpdate;

    constructor(private myEntityService: MyEntityService) {
        this.count = this.myEntityService.entities$.pipe(
            map((data: IItem[]) => data.length)
        );

        // all refresh 
        this.sequenceUpdate = setInterval(() => {
            this.myEntityService.getAll();
        }, 10000);
    }

    ngOnDestroy(): void {
        clearInterval(this.sequenceUpdate);
    }
}

1. run I get below (example) data from http.

[
  { id: 1, data: '1' },
  { id: 2, data: '2' },
  { id: 3, data: '3' },
  { id: 4, data: '4' },
]

count is 4. It is fine.

2. run I get below (example) data from http.

[
  { id: 1, data: '1' },
  { id: 2, data: '2' },
  { id: 3, data: '3' },
  { id: 4, data: '4' },
  { id: 5, data: '5' },
]

count is 5. It is fine.

3. run I get below (example) data from http.

[
  { id: 1, data: '1' },
  { id: 2, data: '2' },
  { id: 3, data: '3' },
  { id: 4, data: '4' },
]

count is 5. It is not fine. Not remove id 5 and it is exist in entityCache.

Numichi
  • 926
  • 6
  • 14

1 Answers1

1

There is a ADD_ALL entity cache action available. See source, shown below.

  /**
   * Replaces all entities in the collection
   * Sets loaded flag to true.
   * Merges query results, preserving unsaved changes
   */
  protected addAll(
    collection: EntityCollection<T>,
    action: EntityAction<T[]>
  ): EntityCollection<T> {
    const entities = this.guard.mustBeEntities(action);
    return {
      ...this.adapter.addAll(entities, collection),
      loading: false,
      loaded: true,
      changeState: {},
    };
  }

Untested but something like the following should work (if you accept a very small overlap where data merges first?)

this.myEntityService.getAll().pipe(
    tap(entities => this.myEntityService.addAllToCache(entities)
)
Andrew Allen
  • 6,512
  • 5
  • 30
  • 73