Check for changes supports mapping
This commit is contained in:
parent
b338dd9baa
commit
cdd20751dc
@ -4,6 +4,8 @@ import { Observable } from 'rxjs';
|
||||
import { CustomHttpUrlEncodingCodec, HeadersConfiguration, List, RequiredParam, Validate } from '../utils';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
|
||||
const ROOT_CHANGES_KEY = 'root';
|
||||
|
||||
export interface HeaderOptions {
|
||||
readonly authorization?: boolean;
|
||||
readonly accept?: boolean;
|
||||
@ -21,16 +23,10 @@ export interface QueryParam {
|
||||
*/
|
||||
export abstract class GenericService<I> {
|
||||
protected readonly _http = this._injector.get(HttpClient);
|
||||
private _lastCheckedForChanges = '0';
|
||||
private _lastCheckedForChanges = new Map<string, string>([[ROOT_CHANGES_KEY, '0']]);
|
||||
|
||||
protected constructor(protected readonly _injector: Injector, protected readonly _defaultModelPath: string) {}
|
||||
|
||||
protected get _hasChanges$(): Observable<boolean> {
|
||||
return this._post<{ value: boolean }>({ value: this._lastCheckedForChanges }, `${this._defaultModelPath}/changes`).pipe(
|
||||
map((res: { value: boolean }) => res.value),
|
||||
);
|
||||
}
|
||||
|
||||
get<T = I[]>(): Observable<T>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/unified-signatures
|
||||
@ -51,6 +47,10 @@ export abstract class GenericService<I> {
|
||||
.pipe(tap(() => this._updateLastChanged()));
|
||||
}
|
||||
|
||||
getFor<R = I[]>(entityId: string, queryParams?: List<QueryParam>): Observable<R> {
|
||||
return this.getAll<R>(`${this._defaultModelPath}/${entityId}`, queryParams).pipe(tap(() => this._updateLastChanged(entityId)));
|
||||
}
|
||||
|
||||
@Validate()
|
||||
delete(@RequiredParam() body: unknown, modelPath = this._defaultModelPath, queryParams?: List<QueryParam>): Observable<unknown> {
|
||||
let path = `/${encodeURI(modelPath)}`;
|
||||
@ -84,6 +84,14 @@ export abstract class GenericService<I> {
|
||||
});
|
||||
}
|
||||
|
||||
hasChanges$(key = ROOT_CHANGES_KEY): Observable<boolean> {
|
||||
const modelPath = key === ROOT_CHANGES_KEY ? '' : `${key}/`;
|
||||
return this._post<{ value: boolean }>(
|
||||
{ value: this._lastCheckedForChanges.get(key) },
|
||||
`${this._defaultModelPath}/${modelPath}changes`,
|
||||
).pipe(map((res: { value: boolean }) => res.value));
|
||||
}
|
||||
|
||||
@Validate()
|
||||
protected _post<R = I>(
|
||||
@RequiredParam() body: unknown,
|
||||
@ -136,7 +144,7 @@ export abstract class GenericService<I> {
|
||||
return queryParameters;
|
||||
}
|
||||
|
||||
protected _updateLastChanged(): void {
|
||||
this._lastCheckedForChanges = new Date().toISOString();
|
||||
protected _updateLastChanged(key = ROOT_CHANGES_KEY): void {
|
||||
this._lastCheckedForChanges.set(key, new Date().toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user