Last active
November 18, 2020 07:56
-
-
Save orahul1/afa7c1532dc3797e72365ea0b3f5aaf6 to your computer and use it in GitHub Desktop.
Angular loader interceptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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<any>, next: HttpHandler) { | |
| this.totalRequests++; | |
| this.loadingService.show(); | |
| return next.handle(request).pipe( | |
| finalize(() => { | |
| this.totalRequests--; | |
| if (this.totalRequests === 0) { | |
| this.loadingService.hide(); | |
| } | |
| }) | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment