import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; import { Observable } from 'rxjs'; import { LoadingService } from '@karmed/core'; import { finalize } from 'rxjs/operators'; import { Injectable } from '@angular/core'; @Injectable() export class LoadingInterceptor implements HttpInterceptor { constructor(private loadingService: LoadingService) { } private requests: HttpRequest[] = []; intercept(request: HttpRequest, next: HttpHandler): Observable> { this.requests.push(request); this.loadingService.isLoading.next(true); return next.handle(request).pipe(finalize(()=> this.removeRequest(request))); } removeRequest(req: HttpRequest) { const i = this.requests.indexOf(req); if (i >= 0) this.requests.splice(i, 1); this.loadingService.isLoading.next(this.requests.length > 0); } }