我如何组合代表两种不同逻辑的 RXJS 表达式?

How can i combine my RXJS expressions that represents two different logic?

嗨,我有一个 angular2 项目。 我有两个 rxjs 表达式。我怎样才能将它们结合起来。以下是我的 rxjs 表达式。

this.trafficTypeControl.valueChanges
      .pipe(
        filter(x => x !== null),
        switchMap(x => {
          if (x === TrafficType.TRANSACTIONAL || x === TrafficType.BULK_SEND) {
            this.campaignControl.setValue(null);
            this.flightControl.setValue(null);
            this.campaigns = [];
            this.flights = [];
          }
          return x;
        })
      ).subscribe();



this.trafficTypeControl.valueChanges
  .pipe(
    filter(x => x !== null),
    switchMap(x => {
      if (x === TrafficType.CAMPAIGN) {
        return this.campaignsService.getUnpaginatedCampaignsWithinRange({
          accountIds: this.currentUser.account
            ? this.currentUser.account.id
            : null,
          campaignStatuses: this.campaignStatusIds,
          rangeStartDate: this.rangeControl.value[0].toISOString(),
          rangeEndDate: this.rangeControl.value[1].toISOString()
        });
      }
    }),
    tap(campaigns => {
      console.log("TrafficType.CAMPAIGN :" + JSON.stringify(campaigns));
      this.campaigns = campaigns;
    })
  ).subscribe();

我想要一个包含两种逻辑的表达式。

如果您能提供帮助,我们将不胜感激 谢谢

你好我改写如下

 this.trafficTypeControl.valueChanges
      .pipe(
        filter(x => x !== null),
        switchMap(x => {

          if (x === TrafficType.TRANSACTIONAL || x === TrafficType.BULK_SEND) {

            this.campaignControl.setValue(null);
            this.flightControl.setValue(null);
            this.campaigns = [];
            this.flights = [];
            return [];
          } else if (x === TrafficType.CAMPAIGN) {

            return this.campaignsService.getUnpaginatedCampaignsWithinRange({
              accountIds: this.currentUser.account
                ? this.currentUser.account.id
                : null,
              campaignStatuses: this.campaignStatusIds,
              rangeStartDate: this.rangeControl.value[0].toISOString(),
              rangeEndDate: this.rangeControl.value[1].toISOString()
            });

          }
        }),
        tap(campaigns => {
          this.campaigns = campaigns;
        })
      ).subscribe();

不确定这是更清洁的解决方案。

不确定,但您可能更愿意摆脱 switchMap,而是在订阅方法中调用 this.campaignsService.getUnpaginatedCampaignsWithinRange

this.trafficTypeControl.valueChanges
  .pipe(
    filter(x => x !== null),
    tap(x => {
      if (x === TrafficType.TRANSACTIONAL || x === TrafficType.BULK_SEND) {
        this.campaignControl.setValue(null);
        this.flightControl.setValue(null);
        this.campaigns = [];
        this.flights = [];
      }
    }),
    switchMap(x => {
      if (x === TrafficType.CAMPAIGN) {
        return this.campaignsService.getUnpaginatedCampaignsWithinRange({
          accountIds: this.currentUser.account
            ? this.currentUser.account.id
            : null,
          campaignStatuses: this.campaignStatusIds,
          rangeStartDate: this.rangeControl.value[0].toISOString(),
          rangeEndDate: this.rangeControl.value[1].toISOString()
        }).pipe(
          tap(campaigns => {
            console.log("TrafficType.CAMPAIGN :" + JSON.stringify(campaigns));
            this.campaigns = campaigns;
          })
        );
      }
      return x;
    })
  ).subscribe();