代码之家  ›  专栏  ›  技术社区  ›  Noitidart

每次重试前后发出

  •  0
  • Noitidart  · 技术社区  · 5 年前
    1. 一旦它得到行动,它应该做一个 ajax.post
    2. 分支
      • YES
      • 如果状态不好,则发出 pre post

    我在与最后一颗子弹搏斗,这是我在操场上的密码- https://rxviz.com/v/WJxGMl4O

    这是我的管道部分:

    action$.pipe(
        flatMap(action =>
            defer(() => ajax.post('foo foo foo')).pipe(
                tap(res => console.log('succeeded:', res)),
                mapTo('YES'),
                retryWhen(error$ =>
                    error$.pipe(
                        tap(error => console.log('got error:', error)),
                        merge(of('pre')), // this isnt emiting
                        delay(1000),
                        merge(of('post')) // this isnt emitting
                    )
                )
            )
        )
    )
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   martin    5 年前

    我认为你可以通过使用 catchError 而不是 retryWhen 因为 复述时间 next 通知,但不会进一步传播。与 捕捉错误 concat 只有在前一个消息完成后,才能逐个订阅它的所有源,因此它将首先发送这两条消息 pre post

    action$.pipe(
        filter(action => action === 'hi'),
        mergeMap(action =>
            defer(() => resolveAfter(3)).pipe(
                tap(res => console.log('succeeded:', res)),
                mapTo('YES'),
                catchError((error, source$) => {
                    console.log('retrying, got error:', error);
                    return staticConcat(
                        of('pre'),
                        of('post').pipe(delay(1000)),
                        source$,
                    );
               }),
            )
        ),
        //take(4)
    )
    

    您的最新演示: https://rxviz.com/v/A8D7BzyJ

        2
  •  1
  •   Andrei Gătej    5 年前

    首先,我创建了两个自定义运算符,其中一个将处理“pre”和“post”( skipValidation )一个能处理逻辑的人( useValidation

    const skipValidation = src => of(src).pipe(
      concatMap(
        v => of('post').pipe(
         startWith('pre'),
         delay(1000),
        ),
      ),
    );
    

    action$.next({ skip: true }) . 有了这些,我们将释放出新的价值观,这些价值观将通过 iif操作员 这样我们就可以发出“pre”和“post”;

    const useValidation = src => of(src).pipe(
     filter(action => action === 'hi'),
        mergeMap(action =>
            defer(() => resolveAfter(3)).pipe(
                tap(res => console.log('succeeded:', res)),
                mapTo('YES'),
                delay(1000),
                retryWhen(error$ =>
                    error$.pipe(
                            tap(error => { console.log('retrying, got error:', error); action$.next({ skip: true })}),
                            delay(1000),
                    )
                )
            )
          )
    );
    
    action$.pipe(
        tap(v => console.log('v', v)), // Every emitted value will go through the `iif ` operator
        mergeMap(v => iif(() => typeof v === 'object' && v.skip, skipValidation(v), useValidation(v))),
    )
    

    Here is your updated demo