-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
`
import { timer as observableTimer, Subscription, throwError } from 'rxjs';
import { takeWhile, tap, catchError, exhaustMap } from 'rxjs/operators';
import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { REPORT_CONFIG_TOKEN } from '../configs/report.config';
@Injectable()
export class PollingService {
private pollSubscriptions: Subscription;
private defaultPollTime: number = 2000;
constructor(
private http: HttpClient,
@Inject(REPORT_CONFIG_TOKEN) private config: any
) {
this.pollSubscriptions = new Subscription();
}
pollRequest<T>(
url: string,
updateStatus: any,
pollWhileCondition: Function,
pollErrorCallback?: Function,
onPollingSuccessCallback?: Function,
timer = this.defaultPollTime
) {
if (this.pollSubscriptions.closed) {
this.pollSubscriptions = new Subscription();
}
this.pollSubscriptions.add(observableTimer(0, timer).pipe(
exhaustMap(() => this.http.get<T>(url, { headers: this.config.requestConfig.headers }).pipe(
catchError((err: HttpErrorResponse) => {
pollErrorCallback(err);
return throwError(err);
}))),
tap(updateStatus),
takeWhile(data => pollWhileCondition(data)))
.subscribe());
}
stopPolling(): void {
this.pollSubscriptions.unsubscribe();
}
}
`
I am able to poll for multiple urls simultaneously. But can I use this library so that I can meet the following requirements:
- If a url which is polled gets failed then how can we retry that poll url with a delay of 3(n) secs for 3 times(I am okay with any strategy but should have some delay)?
- Can we make sure that same urls are not polled at any point of time?
- If we pass retryAttempts can we throw that error? But stream should not break!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels