import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of, tap } from 'rxjs';
@Injectable()
export class CachingInterceptor implements HttpInterceptor {
private cache = new Map<string, any>();
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (request.method !== 'GET') {
return next.handle(request);
}
const cachedResponse = this.cache.get(request.url);
if (cachedResponse) {
return of(cachedResponse);
}
return next.handle(request).pipe(
tap((response) => {
if (response instanceof HttpResponse) {
this.cache.set(request.url, response);
}
})
);
}
}
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({ providedIn: 'root' })
export class TimerService {
private isTimerStarted = false;
public dateNow = new Date();
public dDay = new Date();
milliSecondsInASecond = 1000;
hoursInADay = 24;
minutesInAnHour = 60;
SecondsInAMinute = 60;
public timeDifference: any;
constructor() {}
private getTimeDifference() {
this.timeDifference = this.dDay.getTime() - new Date().getTime();
}
startTimer() {
if (!this.isTimerStarted) {
this.dDay.setMinutes(
this.dDay.getMinutes() + 30 //+environment.cacheTimeInMinutes // you can put this time in environment file to make it configurable.
);
this.getTimeDifference();
this.isTimerStarted = true;
}
}
resetTimer() {
this.isTimerStarted = false;
this.getTimeDifference();
}
getRemainingTime(): number {
this.getTimeDifference();
return this.timeDifference;
}
}