Spaces:
Paused
Paused
Create loading.service.ts
Browse files
flare-ui/src/app/services/loading.service.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Injectable } from '@angular/core';
|
| 2 |
+
import { BehaviorSubject } from 'rxjs';
|
| 3 |
+
|
| 4 |
+
@Injectable({
|
| 5 |
+
providedIn: 'root'
|
| 6 |
+
})
|
| 7 |
+
export class LoadingService {
|
| 8 |
+
private loadingSubject = new BehaviorSubject<boolean>(false);
|
| 9 |
+
public loading$ = this.loadingSubject.asObservable();
|
| 10 |
+
|
| 11 |
+
private activeRequests = 0;
|
| 12 |
+
|
| 13 |
+
show() {
|
| 14 |
+
this.activeRequests++;
|
| 15 |
+
this.loadingSubject.next(true);
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
hide() {
|
| 19 |
+
this.activeRequests--;
|
| 20 |
+
if (this.activeRequests <= 0) {
|
| 21 |
+
this.activeRequests = 0;
|
| 22 |
+
// Small delay to prevent flicker
|
| 23 |
+
setTimeout(() => {
|
| 24 |
+
if (this.activeRequests === 0) {
|
| 25 |
+
this.loadingSubject.next(false);
|
| 26 |
+
}
|
| 27 |
+
}, 300);
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
forceHide() {
|
| 32 |
+
this.activeRequests = 0;
|
| 33 |
+
this.loadingSubject.next(false);
|
| 34 |
+
}
|
| 35 |
+
}
|