Created
September 22, 2019 08:49
-
-
Save takuya-nakayasu/1d179d16d33fb079877588bec53d4edc to your computer and use it in GitHub Desktop.
スピナーの表示・非表示を制御するインターセプター
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 { | |
HttpEvent, | |
HttpInterceptor, | |
HttpHandler, | |
HttpRequest | |
} from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { finalize } from 'rxjs/operators'; | |
import { SpinnerService } from './spinner.service'; | |
/** | |
* HttpClientModuleのリクエスト、レスポンス処理のタイミングで、 | |
* スピナーを表示する。 | |
* | |
* @export | |
* @class SpinnerInterceptor | |
* @implements {HttpInterceptor} | |
*/ | |
@Injectable() | |
export class SpinnerInterceptor implements HttpInterceptor { | |
constructor(public spinnerService: SpinnerService) {} | |
intercept( | |
req: HttpRequest<any>, | |
next: HttpHandler | |
): Observable<HttpEvent<any>> { | |
// HTTPリクエストのタイミングで呼び出される | |
// スピナーを表示する | |
this.spinnerService.show(); | |
return next.handle(req).pipe(finalize(() => this.spinnerService.hide())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment