add min loading time to loading service

This commit is contained in:
Dan Percic 2021-06-25 13:55:59 +03:00
parent e3f5beb509
commit 8e995e2afe

View File

@ -1,11 +1,14 @@
import { EventEmitter, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
const MIN_LOADING_TIME = 300;
@Injectable({
providedIn: 'root'
})
export class LoadingService {
private readonly _loadingEvent = new EventEmitter();
private _loadingStarted: number;
get isLoading(): Observable<boolean> {
return this._loadingEvent;
@ -13,9 +16,18 @@ export class LoadingService {
start(): void {
this._loadingEvent.next(true);
this._loadingStarted = new Date().getTime();
}
stop(): void {
const timeDelta = new Date().getTime() - this._loadingStarted;
if (timeDelta < MIN_LOADING_TIME) {
setTimeout(() => {
this._loadingEvent.next(false);
}, MIN_LOADING_TIME - timeDelta);
return;
}
this._loadingEvent.next(false);
}
}