Подписка имеет внутреннюю подписку на каждый товар - PullRequest
0 голосов
/ 02 октября 2018

У меня есть следующий сервис:

public loadMatchAnalysis(clientId: string): void {
    this.logger.info(`Calling matchAnalysisService to get everything for the 'Match Analysis' page`);

    this.matchAnalysisService.getMatchAnalysis(clientId).subscribe(
      (matches: MatchAnalysis[]) => {
        matches.forEach(match => {
          forkJoin(
            this.matchAnalysisService.getMappings(clientId, match.id),
            this.matchAnalysisService.getCalculationMethods(clientId, match.id)
          ).subscribe(([mappings, calculations]) => {
            match.mappings = mappings.filter(m => m.id === match.id);
            match.children = calculations.filter(c => c.id === match.id);

            console.log('mappings', mappings);
            console.log('calculations', calculations);
            console.log('matches', matches);
          });
        });

        //THIS ONLY NEEDS TO BE CALLED ONCE
        new GetMatchAnalysisLoadedAction({ data: matches }).dispatch();
      },
      (error: HttpErrorResponse) => {
        new GetMatchAnalysisLoadedAction({ error: error }).dispatch();
      }
    );
  }

Что я пытаюсь сделать:

Мне нужно сделать вызов API, который вернет список matches.Каждый match имеет id, который мне нужен, чтобы сделать два новых вызова API.Как видите, я использую forkJoin для выполнения вызовов, и когда я получаю данные обратно, я изменяю matches данными, которые мне нужны.Как только все это будет завершено, мне нужно позвонить на GetMatchAnalysisLoadedAction() со всеми данными (matches).

Приведенный выше код работает по большей части, за исключением того, что только один раз вызывается GetMatchAnalysisLoadedAction()-И я понимаю, почему это происходит.

Есть ли какая-то магия RXJS, чтобы сделать все это красиво?

1 Ответ

0 голосов
/ 03 октября 2018

Я решил это так:

  public loadMatchAnalysis(clientId: string): void {
    this.logger.info(`Calling matchAnalysisService to get everything for the 'Match Analysis' page`);

    this.matchAnalysisService
      .getMatchAnalysis(clientId)
      .pipe(
        switchMap(matches => {
          const mappings = matches.map(match => this.matchAnalysisService.getMappings(clientId, match.id));
          const calculations = matches.map(match =>
            this.matchAnalysisService.getCalculationMethods(clientId, match.id)
          );

          return forkJoin(of(matches), ...mappings, ...calculations);
        })
      )
      .subscribe(([matches, mappings, calculations]: [MatchAnalysis[], MatchAnalysisMapping[], Calculation[]]) => {
        matches.forEach(match => {
          match.mappings = mappings.filter(m => m.id === match.id);
          match.children = calculations.filter(c => c.id === match.id);
        });

        new GetMatchAnalysisLoadedAction({ data: matches }).dispatch();
      });
  }
...