Files map service extends generic service

This commit is contained in:
Adina Țeudan 2022-02-10 18:59:19 +02:00
parent 9b330a1f7d
commit 638cb7060e
2 changed files with 5 additions and 90 deletions

View File

@ -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<File>();
private readonly _entityDeleted$ = new Subject<File>();
private readonly _map = new Map<string, BehaviorSubject<File[]>>();
get$(dossierId: string) {
if (!this._map.has(dossierId)) {
this._map.set(dossierId, new BehaviorSubject<File[]>([]));
}
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<File[]>(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<File, IFile> {
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<File> {
return this._entityChanged$.pipe(
filter(entity => entity.id === entityId),
startWith(this.get(key, entityId)),
shareLast(),
);
}
watchDeleted$(entityId: string): Observable<File> {
return this._entityDeleted$.pipe(filter(entity => entity.id === entityId));
}
}

@ -1 +1 @@
Subproject commit 0bab4584767b868f9d72a4de7ae23d0dd19daad3
Subproject commit 492ac8d7c046d6a22a6d3ac07d569721fb14082e