簡體   English   中英

RXJS 立即發出並重試

[英]RXJS emit immediately and retry

我們正在 Angular 中發出一個 http 請求,該請求返回一些包含指示數據是否已完全處理的布爾值的數據。 我們想從第一個 http 響應中發出不完整的數據並繼續發出 http 請求,直到處理為真。 如果第一個響應返回為 true,我們還希望避免發出重復的 http 請求。 我們正在使用 retryBackoff 庫,如果我理解正確的話,它只是 retryWhen 引擎蓋下的延遲。

這是我們正在處理的代碼中的一個示例:

const crashDetails$ = combineLatest(crashId$, database$)
   .pipe(switchMap(([crashId, database]) => { 
        return this._crashDetailsService.getCrashDetails({crashId, database});
    });

const crashDetailsWithRetry$ = crashDetails$
    .pipe(
        tap(crashDetails => {
            if (!crashDetails.processed) {
               throw new Error('Still processing, retry if able');
            }
        }),
        retryBackoff(retryOptions)
    );

return merge($crashDetails, crashDetailsWithRetry$);

問題是在處理為 true 的情況下會發出 2 個請求。

我嘗試將 share() 和 shareReplay(1) 添加到 crashDetails$,但這會導致 crashDetailsWithRetry$ 停止工作。 有沒有一種簡單的方法來實現我們想要的,或者我應該創建一個新的主題和自定義重試邏輯來處理這種情況?

謝謝!

這只是一個條件輪詢方案:

 const crashDetails$ = combineLatest(crashId$, database$)
   .pipe(switchMap(([crashId, database]) => { 
        // switch into a timer that switches into your observable, take while processing
        return timer(0, 3000).pipe( // set 3000 to whatever ms interval you want to poll on
          switchMapTo(this._crashDetailsService.getCrashDetails({crashId, database}),
          takeWhile(details => !details.processed, true) //take while not processed
        );
    });

 return crashDetails$;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM