简体   繁体   English

使用 rxjs 实现池化 - 等待正确响应并使用超时和延迟进行操作

[英]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.所以首先,我必须调用一个端点,在它返回成功后,调用另一个端点,直到它返回正确的响应(它总是返回成功/200/,但对我来说最重要的是响应,所以如果响应将是 {state : 'ready'} 或者如果时间过去了(20 秒),我应该停止调用 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.您必须将内部可观察到的switchMap更改为下面。 takeUntil and repeat should help here. takeUntilrepeat应该在这里有所帮助。

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())
  );
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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