简体   繁体   中英

RXJS emit immediately and retry

We are making an http request in Angular that returns some data containing a boolean that indicates whether or not the data has been completely processed. We would like to emit the incomplete data from the first http response and continue making http requests until processed is true. We also would like to avoid making duplicate http requests if the first response returns with processed true. We are using the retryBackoff library, which if I understand correctly is just retryWhen with a delay under the hood.

Here's a sample from the code we're working on:

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$);

The problem is 2 requests get made in the case processed is true.

I tried adding share() and shareReplay(1) to crashDetails$, but this causes crashDetailsWithRetry$ to stop working. Is there an easy way to achieve what we want, or should I create a new subject and custom retry logic to handle this case?

Thanks!

this is just a conditional polling scheme:

 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$;

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