import { LoadingBarService } from "mirra-ui/loading-bar-core"; import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs'; import { finalize, tap } from 'rxjs/operators'; @Injectable() export class LoadingBarInterceptor implements HttpInterceptor { constructor(private loader: LoadingBarService) {} intercept(req: HttpRequest, next: HttpHandler): Observable> { // https://github.com/angular/angular/issues/18155 if (req.headers.has('ignoreLoadingBar')) { return next.handle(req.clone({ headers: req.headers.delete('ignoreLoadingBar') })); } let started = false; const ref = this.loader.useRef('http'); return next.handle(req).pipe( tap(() => { if (!started) { ref.start(); started = true; } }), finalize(() => started && ref.complete()), ); } }