import { Injectable } from '@angular/core'; import { HttpRequest, HttpHandler, HttpInterceptor } from '@angular/common/http'; import { finalize } from 'rxjs/operators'; import { SpinnerService } from "@accubits/spinner"; import { of } from 'rxjs'; @Injectable() export class LoaderInterceptor implements HttpInterceptor { private totalRequests = 0; constructor(private loadingService: SpinnerService) { } intercept(request: HttpRequest, next: HttpHandler) { this.totalRequests++; this.loadingService.show(); return next.handle(request).pipe( finalize(() => { this.totalRequests--; if (this.totalRequests === 0) { this.loadingService.hide(); } }) ); } }