import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http"; import { Injectable } from "@angular/core"; import { Observable, finalize } from "rxjs"; import { LoaderService } from "../services/loader.service"; @Injectable() export class LoadingInterceptor implements HttpInterceptor { private totalRequests: number = 0; constructor(private loadingSvc: LoaderService) { } intercept(req: HttpRequest, next: HttpHandler): Observable> { this.totalRequests++; this.loadingSvc.setLoading(true); return next.handle(req).pipe( finalize( () => { this.totalRequests--; if (this.totalRequests == 0) { this.loadingSvc.setLoading(false); } } ) ); } }