27 lines
881 B
TypeScript
27 lines
881 B
TypeScript
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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
this.totalRequests++;
|
|
this.loadingSvc.setLoading(true);
|
|
return next.handle(req).pipe(
|
|
finalize(
|
|
() => {
|
|
this.totalRequests--;
|
|
if (this.totalRequests == 0) {
|
|
this.loadingSvc.setLoading(false);
|
|
}
|
|
}
|
|
)
|
|
);
|
|
}
|
|
} |