0

我对 RxJS 很陌生,我想知道我是否做得对……在下面的 ngOnInit() 函数中,我得到一个客户端对象,然后通过管道传输它……

有没有更好的方法来执行下面的重复 switchMap/map 操作?

我的代码有效...但我想知道是否应该采用更优雅的方法...

public client: Client;
public contract: Contract;      
public alreadyPendingContract: boolean;
public alreadyActiveContract: boolean;
public minimumStartDate: Date;
public minimumEndDate: Date;
public rolloverExclusionDate: Date;
public startDateFilter;

  ngOnInit() {
    this.clientService.getClient$().pipe(
      filter(client => client != null),
      map(client => this.client = client),
      pluck('client_id'),
      map((client_id: string) => {
        this.clientContractForm.get('client_id').setValue(client_id);
        return client_id;
      }),
      switchMap((client_id: string) => {
        return this.contractAddService.getAlreadyPendingContract$(client_id);
      }),
      map(alreadyPendingContract => {
        this.alreadyPendingContract = alreadyPendingContract;
        return this.client.client_id;
      }),
      switchMap((client_id: string) => {
        return this.contractAddService.getAlreadyActiveContract$(client_id);
      }),
      map(alreadyActiveContract => {
        this.alreadyActiveContract = alreadyActiveContract;
      }),
      switchMap(() => {
        return this.contractAddService.getMinimumStartDate$(this.client.client_id);
      }),
      map((minimumStartDate: IMinimumStartDate) => {
        this.minimumStartDate = minimumStartDate.minimumStartDate;
        this.rolloverExclusionDate = minimumStartDate.rolloverExclusionDate;
        this.startDateFilter = (m: Moment): boolean => {
          // Filters out the rollover exclusion day from being an available start date.
          return !moment.utc(m).isSame(moment.utc(this.rolloverExclusionDate), 'day');
        }
      })
    ).subscribe();
  }
4

1 回答 1

0

我不确定这是否更优雅,但这是另一种方式

ngOnInit() {
    this.clientService.getClient$().pipe(
      filter(client => client != null),
      map(client => {
         this.client = client;
         this.clientContractForm.get('client_id').setValue(client_id);
         return client.client_id;
      },
      switchMap(client_id => this.doStuffWithClientId(client_id)),
      map((minimumStartDate: IMinimumStartDate) => {
        this.minimumStartDate = minimumStartDate.minimumStartDate;
        this.rolloverExclusionDate = minimumStartDate.rolloverExclusionDate;
        this.startDateFilter = (m: Moment): boolean => {
          // Filters out the rollover exclusion day from being an available start date.
          return !moment.utc(m).isSame(moment.utc(this.rolloverExclusionDate), 'day');
        }
      })
    ).subscribe();
  }

doStuffWithClientId(clientId: string) {
   return this.contractAddService.getAlreadyPendingContract$(client_id).pipe(
      tap(alreadyPendingContract => this.alreadyPendingContract = alreadyPendingContract),
      switchMap(() => this.contractAddService.getAlreadyActiveContract$(clientId)),
      tap(alreadyActiveContract => this.alreadyActiveContract = alreadyActiveContract),
      switchMap(() => this.contractAddService.getMinimumStartDate$(clientId)),
   )
}

我没有测试过代码,所以很可能有语法错误。基本思想是将所有依赖的东西隔离client_id到一个函数中,该函数接收clientId作为输入,因此使其在整个函数中可见。

于 2018-09-20T06:29:22.853 回答