如何阻止可观察到的RxJ触发,直到其他呼叫完成?

时间:2020-06-02 13:11:38

标签: angular rxjs

我有以下代码:

    this.redactedService.selectedProfile.pipe(switchMap(selectedProfile => {
      this.isLoading = true;
      this.selectedProfile = selectedProfile;

      this.existingExpiredListToDisplay = [];
      this.existingFailedListToDisplay = [];
      this.existingAuthoriseListToDisplay = [];
      this.pendingExpiredList = [];
      this.pendingFailedList = [];

      return forkJoin(this.redactedService.getAuthorisationsExpired(selectedProfile.ProfileId), this.redactedService.getAuthorisationsFailed(selectedProfile.ProfileId));
    })).subscribe(([authExpired, authFailed]) => {
      this.authExpiredList$.next(authExpired);
      this.authFailedList$.next(authFailed);
    });

    zip(this.authExpiredList$, this.authFailedList$).pipe(switchMap(([authExpired, authFailed]) => {
      this.pendingExpiredList = authExpired.ExpiredPaymentsList;
      this.pendingFailedList = authFailed.FailedPaymentList;

      return this.redactedService.selectedAccount;
    }), takeUntil(this.unsubscribe$)).subscribe(selectedAccount => {
      this.selectedAccount = selectedAccount;
      this.accountNumberForRequests = this.selectedAccount.AccountNumberForRequests;

      // filter when changing accounts
      this.filterExpiredPaymentsList(this.accountNumberForRequests, 'Expired & Failed');
    });

我遇到的问题是由于在父组件中,如果selectedProfile更改了,它也会更改selectedAccount。

因此,如果SelectedProfile发生更改,则在执行此代码期间,selectedAccount订阅将在SelectedProfile被触发后立即触发,但是我不希望这种情况发生,直到与配置文件更改相关的所有操作都已触发(使getAuthorisationsExpired / getAuthorisationsFailed XHR电话等)。

在进行必要的呼叫之前,有什么方法可以阻止selectedAccount的内部可观察性触发吗?

作为预期功能的一个示例,这是我刚想出的“修复”,我认为它可以起作用...但是看起来很hacky。

this.redactedService.selectedProfile.pipe(switchMap(selectedProfile => {
      this.profileJustChanged = true;
      this.isLoading = true;
      this.selectedProfile = selectedProfile;

      this.existingExpiredListToDisplay = [];
      this.existingFailedListToDisplay = [];
      this.existingAuthoriseListToDisplay = [];
      this.pendingExpiredList = [];
      this.pendingFailedList = [];

      return forkJoin(this.redactedService.getAuthorisationsExpired(selectedProfile.ProfileId), this.redactedService.getAuthorisationsFailed(selectedProfile.ProfileId));
    })).subscribe(([authExpired, authFailed]) => {
      this.authExpiredList$.next(authExpired);
      this.authFailedList$.next(authFailed);
    });

    zip(this.authExpiredList$, this.authFailedList$).pipe(switchMap(([authExpired, authFailed]) => {
    this.profileJustChanged = false;
      this.pendingExpiredList = authExpired.ExpiredPaymentsList;
      this.pendingFailedList = authFailed.FailedPaymentList;

      return this.redactedService.selectedAccount;
    }), takeUntil(this.unsubscribe$)).subscribe(selectedAccount => {
      if(!this.profileJustChanged) {
          this.selectedAccount = selectedAccount;
          this.accountNumberForRequests = this.selectedAccount.AccountNumberForRequests;

          // filter when changing accounts
          this.filterExpiredPaymentsList(this.accountNumberForRequests, 'Expired & Failed');
      }
    });

0 个答案:

没有答案
相关问题