diff --git a/src/lib/listing/models/entity.model.ts b/src/lib/listing/models/entity.model.ts index b8fc65f..fb4d63a 100644 --- a/src/lib/listing/models/entity.model.ts +++ b/src/lib/listing/models/entity.model.ts @@ -1,7 +1,8 @@ import { IListable } from './listable'; +import { Id } from './trackable'; export abstract class Entity implements IListable { - abstract readonly id: string; + abstract readonly id: Id; abstract readonly routerLink?: string; abstract readonly searchKey: string; diff --git a/src/lib/listing/models/trackable.ts b/src/lib/listing/models/trackable.ts index b752b41..66c3fb4 100644 --- a/src/lib/listing/models/trackable.ts +++ b/src/lib/listing/models/trackable.ts @@ -1,3 +1,5 @@ +export type Id = string | number; + export interface ITrackable { - readonly id: string; + readonly id: Id; } diff --git a/src/lib/listing/services/entities.service.ts b/src/lib/listing/services/entities.service.ts index 218d49d..ab843a6 100644 --- a/src/lib/listing/services/entities.service.ts +++ b/src/lib/listing/services/entities.service.ts @@ -4,6 +4,7 @@ import { filter, map, startWith, tap } from 'rxjs/operators'; import { IListable } from '../models'; import { GenericService, QueryParam } from '../../services'; import { getLength, List, mapEach, shareDistinctLast, shareLast } from '../../utils'; +import { Id } from '../models/trackable'; @Injectable() /** @@ -47,7 +48,7 @@ export class EntitiesService extends ); } - getEntityChanged$(entityId: string): Observable { + getEntityChanged$(entityId: Id): Observable { return this._entityChanged$.pipe( filter(entity => entity.id === entityId), startWith(this.find(entityId)), @@ -55,7 +56,7 @@ export class EntitiesService extends ); } - getEntityDeleted$(entityId: string): Observable { + getEntityDeleted$(entityId: Id): Observable { return this._entityDeleted$.pipe(filter(entity => entity.id === entityId)); } @@ -88,7 +89,7 @@ export class EntitiesService extends } } - remove(id: string) { + remove(id: Id) { const entity = this.all.find(item => item.id === id); if (entity) { this.#all$.next(this.all.filter(item => item.id !== id)); @@ -96,11 +97,11 @@ export class EntitiesService extends } } - find(id: string): E | undefined { + find(id: Id): E | undefined { return this.all.find(entity => entity.id === id); } - has(id: string): boolean { + has(id: Id): boolean { return this.all.some(entity => entity.id === id); } diff --git a/src/lib/services/entities-map.service.ts b/src/lib/services/entities-map.service.ts index 220d47f..4642c15 100644 --- a/src/lib/services/entities-map.service.ts +++ b/src/lib/services/entities-map.service.ts @@ -3,12 +3,13 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs'; import { filter, startWith } from 'rxjs/operators'; import { Entity } from '../listing'; import { RequiredParam, shareLast, Validate } from '../utils'; +import { Id } from '../listing/models/trackable'; @Injectable({ providedIn: 'root' }) export abstract class EntitiesMapService, I> { protected abstract readonly _primaryKey: string; - protected readonly _map = new Map>(); + protected readonly _map = new Map>(); readonly #entityChanged$ = new Subject(); readonly #entityDeleted$ = new Subject(); @@ -16,11 +17,11 @@ export abstract class EntitiesMapService, I> { return this._map.size === 0; } - delete(keys: string[]): void { + delete(keys: Id[]): void { keys.forEach(key => this._map.delete(key)); } - get$(key: string) { + get$(key: Id) { if (!this._map.has(key)) { this._map.set(key, new BehaviorSubject([])); } @@ -28,13 +29,13 @@ export abstract class EntitiesMapService, I> { return this._getBehaviourSubject(key).asObservable(); } - has(parentId: string) { + has(parentId: Id) { return this._map.has(parentId); } - get(key: string): E[]; - get(key: string, id: string): E | undefined; - get(key: string, id?: string): E | E[] | undefined { + get(key: Id): E[]; + get(key: Id, id: Id): E | undefined; + get(key: Id, id?: Id): E | E[] | undefined { const value = this._getBehaviourSubject(key)?.value; if (!id) { return value ?? []; @@ -42,7 +43,7 @@ export abstract class EntitiesMapService, I> { return value?.find(item => item.id === id); } - set(key: string, entities: E[]): void { + set(key: Id, entities: E[]): void { if (!this._map.has(key)) { this._map.set(key, new BehaviorSubject(entities)); return entities.forEach(entity => this.#entityChanged$.next(entity)); @@ -96,7 +97,7 @@ export abstract class EntitiesMapService, I> { } @Validate() - watch$(@RequiredParam() key: string, @RequiredParam() entityId: string): Observable { + watch$(@RequiredParam() key: Id, @RequiredParam() entityId: Id): Observable { return this.#entityChanged$.pipe( filter(entity => entity.id === entityId), startWith(this.get(key, entityId) as E), @@ -104,17 +105,17 @@ export abstract class EntitiesMapService, I> { ); } - watchDeleted$(entityId: string): Observable { + watchDeleted$(entityId: Id): Observable { return this.#entityDeleted$.pipe(filter(entity => entity.id === entityId)); } - private _pluckPrimaryKey(entity: E): string { + private _pluckPrimaryKey(entity: E): Id { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - return entity[this._primaryKey] as string; + return entity[this._primaryKey] as Id; } - private _getBehaviourSubject(key: string): BehaviorSubject { + private _getBehaviourSubject(key: Id): BehaviorSubject { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this._map.get(key)!; } diff --git a/src/lib/utils/functions.ts b/src/lib/utils/functions.ts index f7c5408..510f3ad 100644 --- a/src/lib/utils/functions.ts +++ b/src/lib/utils/functions.ts @@ -1,4 +1,4 @@ -import { ITrackable } from '../listing/models/trackable'; +import { Id, ITrackable } from '../listing/models/trackable'; import { UntypedFormGroup } from '@angular/forms'; import { forOwn, has, isEqual, isPlainObject, transform } from 'lodash-es'; import dayjs, { Dayjs } from 'dayjs'; @@ -34,7 +34,7 @@ export function toNumber(str: string): number { } export function trackByFactory() { - return (_index: number, item: T): string => item.id; + return (_index: number, item: T): Id => item.id; } export function hasFormChanged(form: UntypedFormGroup, initialFormValue: Record): boolean {