Rxjs observable wait until some condition is met

2019-06-18 03:44发布

I have the following retry logic to retry an operation. It works fine for single request. For multiple on going requests, I would like to wait for existing retry logic to complete before retrying.

handleError(errors: Observable<any>) {

    const retryCountStart: number = 1;

    // wait if there is any existing operation retrying
    // once it is complete, continue here

    return errors
        .mergeScan<any, any>(
        (retryCount: any, err: any) => {

            if (retryCount <= 5) {
                return Observable.of(retryCount + 1);
            } 

        },retryCountStart)
        .delay(1000);
}

How can I add delay until some condition is met in the above method?

2条回答
别忘想泡老子
2楼-- · 2019-06-18 04:30

You can use async / await for that purpose with the Promise resolve:

async handleError(errors: Observable<any>) {

    const retryCountStart: number = 1;

    // wait if there is any existing operation retrying
    // ----------------------------------------------------------
    await new Promise(resolve => {
        // declare some global variable to check in while loop
        while(this.retrying){
            setTimeout(()=> {
                // Just adding some delay 
                // (you can remove this setTimeout block if you want)
            },50);
        }

        // when while-loop breaks, resolve the promise to continue
        resolve();
    });
    // ----------------------------------------------------------

    // once it is complete, continue here

    return errors
        .mergeScan<any, any>(
        (retryCount: any, err: any) => {

            if (retryCount <= 5) {
                return Observable.of(retryCount + 1);
            } 

        },retryCountStart)
        .delay(1000);
}
查看更多
冷血范
3楼-- · 2019-06-18 04:30

As i understood you want to start next stream only after previous has completed (i.e. add stream to queue)

import { Observable, of, BehaviorSubject, from } from 'rxjs';
import { tap, finalize, filter, take, switchMap, delay } from 'rxjs/operators';

class StreamQueue {
  lastStreamCompleted$: Observable<boolean> = new BehaviorSubject(true);

  private runAfter<T>(lastStreamCompleted$: Observable<boolean>, stream$: Observable<T>): [Observable<boolean>, Observable<T>] {
    const newLastStreamCompleted$ = new BehaviorSubject(false);
    const newStream$ = lastStreamCompleted$
      .pipe(
        filter(lastStreamCompleted => lastStreamCompleted),
        take(1),
        switchMap(() => stream$),
        finalize(() => newLastStreamCompleted$.next(true)),
    );
    return [newLastStreamCompleted$, newStream$];
  }

  add(stream$: Observable<any>) {
    const [newLastStreamCompleted$, newStream$] = this.runAfter(this.lastStreamCompleted$, stream$);
    this.lastStreamCompleted$ = newLastStreamCompleted$;
    return newStream$;
  }
}

const streamQueue = new StreamQueue();

streamQueue.add(from([1, 2]).pipe(delay(100))).subscribe(console.log);
setTimeout(()=>streamQueue.add(from([21, 22]).pipe(delay(100))).subscribe(console.log), 100);
streamQueue.add(from([11, 12]).pipe(delay(100))).subscribe(console.log);

// Output:
// 1
// 2
// 11
// 12
// 21
// 22
查看更多
登录 后发表回答