implement delete and add methods for entities map service

This commit is contained in:
Dan Percic 2023-01-09 11:15:22 +02:00
parent b20d4e938c
commit 64d1cb8be6

View File

@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, Subject, switchMap } from 'rxjs';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { filter, map, startWith } from 'rxjs/operators';
import { Entity } from '../listing';
import { RequiredParam, shareLast, Validate } from '../utils';
import { List, RequiredParam, shareLast, Validate } from '../utils';
import { Id } from '../listing/models/trackable';
import { isArray } from '../permissions';
@Injectable({ providedIn: 'root' })
export abstract class EntitiesMapService<Interface, Class extends Entity<Interface, PrimaryKey>, PrimaryKey extends Id = Class['id']> {
@ -18,20 +19,32 @@ export abstract class EntitiesMapService<Interface, Class extends Entity<Interfa
return this._map.size === 0;
}
delete(keys: Id[]): void {
keys.forEach(key => this._map.delete(key));
delete(keys: List<Id>): void;
delete(key: Id, entity: PrimaryKey | Class): void;
delete(keys: List<Id> | Id, entity?: PrimaryKey | Class): void {
if (isArray(keys)) {
return keys.forEach(key => this._map.delete(key));
}
if (entity) {
const entityId = typeof entity === 'string' || typeof entity === 'number' ? entity : entity.id;
const entities = this.get(keys).filter(entity => entity.id !== entityId);
this.set(keys, entities);
}
console.error('entityId is null when deleting from EntitiesMapService');
}
get$(key: Id) {
if (!this._map.has(key)) {
if (!this.has(key)) {
this._map.set(key, new BehaviorSubject<Class[]>([]));
}
return this._getBehaviourSubject(key).asObservable();
}
has(parentId: Id) {
return this._map.has(parentId);
has(key: Id) {
return this._map.has(key);
}
get(key: Id): Class[];
@ -55,7 +68,7 @@ export abstract class EntitiesMapService<Interface, Class extends Entity<Interfa
// Keep old object references for unchanged entities
const newEntities: Class[] = entities.map(newEntity => {
const oldEntity: Class | undefined = this.get(key, newEntity.id);
const oldEntity = this.get(key, newEntity.id);
if (oldEntity && newEntity.isEqual(oldEntity)) {
return oldEntity;
@ -82,9 +95,13 @@ export abstract class EntitiesMapService<Interface, Class extends Entity<Interfa
}
}
replace(entities: Class[]) {
/** Return true if entities were replaced or false if not **/
const key = this._pluckPrimaryKey(entities[0]);
add(key: string, entity: Class) {
const entities = this.get(key);
this.set(key, [...entities, entity]);
}
/** Return true if entities were replaced or false if not */
replace(key: string, entities: Class[]) {
const entityIds = entities.map(entity => entity.id);
const existingEntities = this.get(key).filter(entity => entityIds.includes(entity.id));
const newEntities = entities.filter(entity => {
@ -93,7 +110,8 @@ export abstract class EntitiesMapService<Interface, Class extends Entity<Interfa
});
if (newEntities.length) {
const all = this.get(key).filter(e => !newEntities.map(entity => entity.id).includes(e.id));
const newEntitiesIds = newEntities.map(entity => entity.id);
const all = this.get(key).filter(e => !newEntitiesIds.includes(e.id));
this.set(key, [...all, ...newEntities]);
return true;
}
@ -112,6 +130,7 @@ export abstract class EntitiesMapService<Interface, Class extends Entity<Interfa
@Validate()
watchChanged$(@RequiredParam() key: Id): Observable<boolean> {
// TODO: This is wrong, entityChanged emits only one entity at a time
return this.#entityChanged$.pipe(
startWith(this.get(key)),
map(entities => entities as Class[]),
@ -123,12 +142,6 @@ export abstract class EntitiesMapService<Interface, Class extends Entity<Interfa
return this.#entityDeleted$.pipe(filter(entity => entity.id === entityId));
}
private _pluckPrimaryKey(entity: Class): Id {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return entity[this._primaryKey] as Id;
}
private _getBehaviourSubject(key: Id): BehaviorSubject<Class[]> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this._map.get(key)!;