Created
January 15, 2019 12:45
-
-
Save trinhvanhuy/f27381413ea4b66aec87b1732c0fbf57 to your computer and use it in GitHub Desktop.
Spinner - Interceptor Service
This file contains 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 { | |
HttpClient, | |
HttpHeaders, | |
HttpParams, | |
HttpParameterCodec | |
} from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { LoaderService } from '@services/loader.service'; | |
import { | |
HttpEvent, | |
HttpHandler, | |
HttpInterceptor | |
} from '@angular/common/http'; | |
import { finalize } from 'rxjs/operators'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class LoaderInterceptorService implements HttpInterceptor { | |
constructor(private loaderService: LoaderService) {} | |
intercept( | |
req: HttpRequest<any>, | |
next: HttpHandler | |
): Observable<HttpEvent<any>> { | |
this.showLoader(); | |
// we use finalize for covering all the cas | |
return next.handle(req).pipe(finalize(() => this.onEnd())); | |
} | |
private onEnd(): void { | |
this.hideLoader(); | |
} | |
private showLoader(): void { | |
this.loaderService.show(); | |
} | |
private hideLoader(): void { | |
this.loaderService.hide(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment