Skip to content

Instantly share code, notes, and snippets.

@orahul1
Last active November 18, 2020 07:56
Show Gist options
  • Select an option

  • Save orahul1/afa7c1532dc3797e72365ea0b3f5aaf6 to your computer and use it in GitHub Desktop.

Select an option

Save orahul1/afa7c1532dc3797e72365ea0b3f5aaf6 to your computer and use it in GitHub Desktop.
Angular loader interceptor
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