仅在满足条件的情况下如何执行rxjs转换运算符

时间:2019-05-01 13:46:56

标签: angular rxjs observable

我想通过对服务器的API调用来验证优惠券代码,但首先我必须检查用户会话,因为我需要用户使用有效的会话令牌登录。如果会话已过期,我想显示一个登录对话框并完成流,而无需输入subscribe函数的下一个回调。如果会话仍然有效,我想调用API来验证优惠券并执行代码订阅函数的下一个回调。

applyCouponCode() {
  const user = this.auth.getUser();
  if (user) { //verify if user is logged in
    if (this.couponCode.length !== 0) {
      this.auth.checkSession() //API call to verify if user session is still valid
        .pipe(
          tap((sessionValid) => {
            if (!sessionValid) {
              //session expired
              this.auth.clientLogout();
              this.auth.showLoginDialog();
            } else {
              //session valid, call the API to validate the coupon code
              mergeMap(() => this.reservation.validateCouponCode(this.couponCode, user.token));
            }
          })
        )
        .subscribe(
          discountCode => {
            if (discountCode.valid) {
              //create the discount code obj and apply to user cart
              ....
            } else {
              //notify discount code is invalid to the user
              ....
            }
          },
          error => {
            console.log('error', error);
          }
        );
    } else {
      this.emptyCouponSubmitted = true;
    }
  }
}

另一种可能的解决方案是在管道函数中重构运算符

.pipe(
  tap((sessionValid) => {
    if (!sessionValid) {
      this.auth.clientLogout();
      this.auth.showLoginDialog();
    }
  }),
  mergeMap((sessionValid) => {
    if (sessionValid) {
      return this.reservation.validateCouponCode(this.couponCode, user.token));
    } else {
      // I would like to complete the stream without enter in the next callback
      return of(null);
    }
  })

仅当调用用于验证优惠券代码的API且如果用户会话已过期时我想完成流时,我才想输入subscribe next回调。这是对的吗?有没有更好的方法可以根据先前的条件执行mergeMap运算符,并在不满足该条件的情况下完成流?处理此类情况的最佳实践是什么?

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法。我在tap和mergeMap运算符之间添加了一个过滤器运算符

.pipe(
   tap((sessionValid) => {
     if (!sessionValid) {
       this.auth.clientLogout();
       this.auth.showLoginDialog();
     }),
     filter(sessionValid => sessionValid),
     mergeMap(() => this.reservation.validateCouponCode(this.couponCode, user.token))
)

执行此操作,如果会话已过期(sessionValid为false),则无需调用mergeMap运算符即可显示对话框,因为发出的值已由filter运算符过滤,并且流完成而无需在下一个回调中输入订阅函数,否则将调用mergeMap,我可以在订阅函数中管理新流的结果。