Created
January 31, 2018 12:31
-
-
Save YonathanMeguira/2fbc3f4e556aaa78d187784beb5c930d to your computer and use it in GitHub Desktop.
http 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 { Observable } from 'rxjs/Observable'; | |
import { | |
HttpEvent, | |
HttpHandler, | |
HttpInterceptor, | |
HttpRequest | |
} from '@angular/common/http'; | |
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
import { catchError, finalize, map } from 'rxjs/operators'; | |
import 'rxjs/add/observable/throw'; | |
@Injectable() | |
export class HTTPStatus { | |
private requestInFlight$: BehaviorSubject<boolean>; | |
constructor() { | |
this.requestInFlight$ = new BehaviorSubject(false); | |
} | |
setHttpStatus(inFlight: boolean) { | |
this.requestInFlight$.next(inFlight); | |
} | |
getHttpStatus(): Observable<boolean> { | |
return this.requestInFlight$.asObservable(); | |
} | |
} | |
@Injectable() | |
export class HTTPListener implements HttpInterceptor { | |
constructor(private status: HTTPStatus) {} | |
intercept( | |
req: HttpRequest<any>, | |
next: HttpHandler | |
): Observable<HttpEvent<any>> { | |
return next.handle(req).pipe( | |
map(event => { | |
return event; | |
}), | |
catchError(error => { | |
return Observable.throw(error); | |
}), | |
finalize(() => { | |
this.status.setHttpStatus(false); | |
}) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To Track Multiple requests inflight