RED-5224: get stat if doesn't exist

This commit is contained in:
Dan Percic 2022-09-20 15:15:23 +03:00
parent 9589808c6d
commit 440b8e6b82

View File

@ -1,5 +1,5 @@
import { inject, Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { BehaviorSubject, Observable, switchMap } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { tap } from 'rxjs/operators';
import { HeadersConfiguration, mapEach, RequiredParam, Validate } from '../utils';
@ -43,7 +43,12 @@ export abstract class StatsService<E, I = E> {
}
watch$(key: string): Observable<E> {
return this._getBehaviourSubject(key).asObservable();
const subject = this.#map.get(key);
if (!subject) {
return this.getFor([key]).pipe(switchMap(() => this._getBehaviourSubject(key).asObservable()));
}
return subject.asObservable();
}
private _pluckPrimaryKey(stats: E): string {
@ -53,7 +58,11 @@ export abstract class StatsService<E, I = E> {
}
private _getBehaviourSubject(key: string): BehaviorSubject<E> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.#map.get(key)!;
const subject = this.#map.get(key);
if (!subject) {
throw new Error(`No stats for key ${key}`);
}
return subject;
}
}