【问题标题】:Implement pooling with rxjs - wait for correct response and do it with timeout and delay使用 rxjs 实现池化 - 等待正确的响应并使用超时和延迟来完成
【发布时间】:2022-08-02 06:54:10
【问题描述】:

我必须在我的情况下实施池化,但我在正确实施我的所有条件时遇到了一些问题。

所以首先,我必须调用一个端点,在它返回成功后,调用另一个端点,直到它返回正确的响应(它总是返回成功/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)
  }

    标签: angular rxjs pooling


    【解决方案1】:

    您必须将内部的 switchMap observable 更改为以下。 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())
      );
    })
    

    【讨论】:

    • @emka26 您有机会尝试建议的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多