Skip to content

Instantly share code, notes, and snippets.

@takuya-nakayasu
Created September 22, 2019 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuya-nakayasu/1d179d16d33fb079877588bec53d4edc to your computer and use it in GitHub Desktop.
Save takuya-nakayasu/1d179d16d33fb079877588bec53d4edc to your computer and use it in GitHub Desktop.
スピナーの表示・非表示を制御するインターセプター
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