Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we achieve parallel polling with this library #9

Open
siddharthpal opened this issue Sep 5, 2018 · 0 comments
Open

Can we achieve parallel polling with this library #9

siddharthpal opened this issue Sep 5, 2018 · 0 comments

Comments

@siddharthpal
Copy link

`
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant