У меня есть сценарий для обработки сессии в приложении Angular.Вот что я пытаюсь добиться.
1) Пусть класс реализует HttpInterceptor, чтобы я мог запускать таймер (начиная с 0Sec) при любом успешном ответе и увеличивать таймер до 30 минут.Если между таймерами не происходит никакого серверного вызова, я хочу предупредить пользователя, что сессия истекает.3) Я пытаюсь использовать концепцию интервалов Rxjs здесь - не уверен, что это то, что мне нужно использовать.Пожалуйста, смотрите код ниже
import { Injectable, OnDestroy } from '@angular/core';
import { HttpInterceptor, HttpHeaderResponse, HttpRequest, HttpSentEvent, HttpHandler, HttpProgressEvent, HttpResponse, HttpUserEvent, HttpEvent } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import { timer } from 'rxjs/observable/timer'
import { tap } from 'rxjs/Operators';
import { pipe, observable, interval, Subscription } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class NoopInterceptorService implements HttpInterceptor {
constructor() {
//assuming to reset the timer here again to 0 sec
this.stopInterval();
}
event : Observable<any>;
subscription: Subscription;
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
console.log("Inside the interceptor");
return next.handle(req).pipe(tap(event => {
if(event instanceof Response){
if(event.status == 200){
this.timer();
}
}
}));
}
timer() {
this.subscription = interval(1000).subscribe(
//wait for 30mins here and give warning to user
val=>console.log("----------"+val));
}
stopInterval() {
console.log("--unscubscribing ---")
this.subscription.unsubscribe();
}
}