rxjs takeUntil不执行终结

时间:2019-02-28 09:57:38

标签: angular typescript rxjs pipe finalize

我的倒计时如下:

userClick=new Subject()

resetCountdown(){this.userClick.next()}

setCountDown() {
    let counter = 5;
    let tick = 1000;
    this.countDown = timer(0, tick)
    .pipe(
      take(counter),
      map(() => --counter),
      takeUntil(this.userClick),
      finalize(() => {
        if (this.currentQuestionNumber < this.questionsToAsk)
          this.showNextQuestion();
        else {
          this.endQuiz();
        }

      })
    );
}

发生takeUntil(this.userClick)时,我不希望执行finalize。是否有可能实现这一目标?我只希望在倒计时达到finalize且之前没有被0

打断时执行takeUntil

1 个答案:

答案 0 :(得分:1)

您可以使用tap来检查值是否达到目标并执行副作用。

function setCountDown() {
    let counter = 5;
    let tick = 1000;
    this.countDown = timer(0, tick)
      .pipe(
        take(counter + 1),
        map(value => counter - value),
        tap(value => {
          if (value === 0) {
            console.log('OUT OF TIME');
          }
        }),
        takeUntil(userClick)
      )
}

查看此example