diff --git a/src/lib/services/entities-map.service.ts b/src/lib/services/entities-map.service.ts index bec6c00..ffd7fc5 100644 --- a/src/lib/services/entities-map.service.ts +++ b/src/lib/services/entities-map.service.ts @@ -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, PrimaryKey extends Id = Class['id']> { @@ -18,20 +19,32 @@ export abstract class EntitiesMapService this._map.delete(key)); + delete(keys: List): void; + delete(key: Id, entity: PrimaryKey | Class): void; + delete(keys: List | 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([])); } 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 { - 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 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 !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 { + // 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 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 { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this._map.get(key)!;