// usage
loadGame(): void {
this.games = []
this.loadHandler.start()
this.http.get<Game[]>.get(this.url).subscribe(
games => {
this.loadHandler.finish();
this.games = games;
})
}
// service
export class LoadHandler {
private _isLoading$ = new BehaviorSubject(false);
isLoading$: Observable<boolean> = this._isLoading$.pipe(
switchMap( isLoading => {
if(!isLoading) {
return of(false)
}else{
return of(true).pipe(delay(1000));
}
})
)
start(){
this._isLoading.next(true);
}
finish(){
this._isLoading.next(false);
}
}