简体   繁体   中英

Implement pooling with rxjs - wait for correct response and do it with timeout and delay

I have to implement pooling in my case, but I have some problem with correct implement all my condition.

So first, I have to call to one endpoint, and after it return success, call another endpoint until it returns correct response (It always return success /200/, but for me most important is response, so if the response will be {state: 'ready'} or if time will pass (20 sec), I should stop call api.

  executeTest$(testCode: string, name: string): Observable<Test> {
    let requestDelay = 500;
    return this.testService.start({
      body: {
        code: {value: testCode},
        name
      }
    }).pipe(
      switchMap(body => {
        return this.testStatus(body.name).pipe(
          delay(500),
          // and here I have problem to implement logic: 
    repeat this http until condition is met, so until response will be {state: 'ready'}
    I see that repeat when is deprecated and retry when is suitable for errors.
   

          timeout(20000)

        );
      })
    );
  }
  private testStatus(testId: string): Observable<Test> {
    return this.http.get(apiUrl)
  }

You have to change the inner switchMap observable to below. takeUntil and repeat should help here.

switchMap(body => {
  const subject: Subject<boolean> = new Subject();
  const completeSubject = () => {
    subject.next()
    subject.complete();
  }
  return this.testStatus(body.name).pipe(
    delay(500),
    timeout(20000),
    tap(res => {
      if (res.status === ready) {
        completeSubject();
      }
    }),
    takeUntil(subject)
    repeat(),
    finalize(() => completeSubject())
  );
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM