Skip to content

Can we achieve parallel polling with this library #9

@siddharthpal

Description

@siddharthpal

`
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:

  1. 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)?
  2. Can we make sure that same urls are not polled at any point of time?
  3. If we pass retryAttempts can we throw that error? But stream should not break!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions