From 638cb7060e6596f6bf00f4de5b0be2f2f9fbbb18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Thu, 10 Feb 2022 18:59:19 +0200 Subject: [PATCH] Files map service extends generic service --- .../entity-services/files-map.service.ts | 93 +------------------ libs/common-ui | 2 +- 2 files changed, 5 insertions(+), 90 deletions(-) diff --git a/apps/red-ui/src/app/services/entity-services/files-map.service.ts b/apps/red-ui/src/app/services/entity-services/files-map.service.ts index 372fadc42..82741b35f 100644 --- a/apps/red-ui/src/app/services/entity-services/files-map.service.ts +++ b/apps/red-ui/src/app/services/entity-services/files-map.service.ts @@ -1,83 +1,11 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject, Observable, Subject } from 'rxjs'; import { File, IFile } from '@red/domain'; -import { filter, startWith } from 'rxjs/operators'; -import { RequiredParam, shareLast, Validate } from '@iqser/common-ui'; +import { EntitiesMapService } from '@iqser/common-ui'; @Injectable({ providedIn: 'root' }) -export class FilesMapService { - private readonly _entityChanged$ = new Subject(); - private readonly _entityDeleted$ = new Subject(); - private readonly _map = new Map>(); - - get$(dossierId: string) { - if (!this._map.has(dossierId)) { - this._map.set(dossierId, new BehaviorSubject([])); - } - - return this._map.get(dossierId).asObservable(); - } - - has(dossierId: string) { - return this._map.has(dossierId); - } - - get(key: string): File[]; - get(key: string, id: string): File | undefined; - get(key: string, id?: string): File | File[] | undefined { - const value = this._map.get(key)?.value; - if (!id) { - return value ?? []; - } - return value?.find(item => item.id === id); - } - - set(key: string, entities: File[]): void { - if (!this._map.has(key)) { - this._map.set(key, new BehaviorSubject(entities)); - return entities.forEach(entity => this._entityChanged$.next(entity)); - } - - const changedEntities: File[] = []; - const deletedEntities = this.get(key).filter(oldEntity => !entities.find(newEntity => newEntity.id === oldEntity.id)); - - // Keep old object references for unchanged entities - const newEntities = entities.map(newEntity => { - const oldEntity = this.get(key, newEntity.id); - - if (newEntity.isEqual(oldEntity)) { - return oldEntity; - } - - changedEntities.push(newEntity); - return newEntity; - }); - - this._map.get(key).next(newEntities); - - // Emit observables only after entities have been updated - - for (const file of changedEntities) { - this._entityChanged$.next(file); - } - - for (const file of deletedEntities) { - this._entityDeleted$.next(file); - } - } - - replace(entities: File[]) { - const dossierId = entities[0].dossierId; - const entityIds = entities.map(entity => entity.id); - let existingFiles = this.get(dossierId).filter(file => entityIds.includes(file.fileId)); - entities = entities.filter(entity => { - const existingFile = existingFiles.find(existingFile => existingFile.id === entity.id); - return existingFile.lastUpdated !== entity.lastUpdated; - }); - if (entities.length) { - const all = this.get(dossierId).filter(file => !entities.map(entity => entity.id).includes(file.id)); - this.set(dossierId, [...all, ...entities]); - } +export class FilesMapService extends EntitiesMapService { + constructor() { + super('dossierId'); } replaceFiles(files: File[], property: keyof IFile, generateValue: Function) { @@ -87,17 +15,4 @@ export class FilesMapService { ); this.replace(newFiles); } - - @Validate() - watch$(@RequiredParam() key: string, @RequiredParam() entityId: string): Observable { - return this._entityChanged$.pipe( - filter(entity => entity.id === entityId), - startWith(this.get(key, entityId)), - shareLast(), - ); - } - - watchDeleted$(entityId: string): Observable { - return this._entityDeleted$.pipe(filter(entity => entity.id === entityId)); - } } diff --git a/libs/common-ui b/libs/common-ui index 0bab45847..492ac8d7c 160000 --- a/libs/common-ui +++ b/libs/common-ui @@ -1 +1 @@ -Subproject commit 0bab4584767b868f9d72a4de7ae23d0dd19daad3 +Subproject commit 492ac8d7c046d6a22a6d3ac07d569721fb14082e