繁体   English   中英

优化 api 调用 rxjs

[英]optimise api calls rxjs

我有一个数组,我想遍历数组并调用每个项目的服务器数据。 我想在加载页面时显示一个微调器,因为 api 处于挂起状态。

当前解决方案:

  getRecordings(classroom) {
    return this.sysAdminService.getMeetingRecordings(classroom.meeting_id).subscribe(res => {
      classroom.recordings = res;
    });
  }

Api 调用:

 this.classRoomsToDisplay.map((classroom) => {
      classroom.showAttendees = false;
      classroom.recordings = null;
      this.getRecordings(classroom);
    });

如上所述,我订阅了数组中每个项目的 observable。我想知道是否有更好的方法使用 rxjs、concatMap 来做到这一点? 我不想等待整个数组的 api 调用完成,我想在该项目的 api 调用完成后立即显示结果。

从您显示的数据中,不清楚您使用哪个属性来显示数据。 我假设它是classroom.showAttendees因为它是一个布尔值。

您可以使用 RxJS forkJoin函数来组合多个 observable 并订阅一次,而不是有多个订阅。 您可以使用tap运算符切换数组中每个项目的showAttendees标志作为副作用。

尝试以下

complete$ = new Subject<any>();

getRecordings(classroom): Observable<any> {   // <-- return the HTTP request observable
  return this.sysAdminService.getMeetingRecordings(classroom.meeting_id).pipe(
    tap => {
      classroom.showAttendees = true;         // <-- set the flag to show data
      classroom.recordings = res;
    }
  );
}

ngOnInit() {
  // `reqs` is an array of observables `[getRecordings(classroom), getRecordings(classroom), ...]`
  const reqs = this.classRoomsToDisplay.map((classroom) => {
    classroom.showAttendees = false;
    classroom.recordings = null;
    return this.getRecordings(classroom);
  });

  forkJoin(reqs).pipe(takeUntil(this.complete$)).subscribe();   // <-- trigger the HTTP requests
}

ngOnDestroy() {
  this.complete$.next();        // <-- close open subscriptions
}

模板

<div *ngFor="classroom of classRoomsToDisplay">
  <ng-container *ngIf="classroom.showAttendees">
    {{ classroom.recordings }}
  </ng-container>
</div>

现在,由于我们在tap运算符中分配值,因此每个项目的数据将在它们到达时立即显示。

您应该更喜欢使用异步管道而不是订阅。

例子:

<ul>
  <li *ngFor="let attendee of attendeesWithData">Attendee: {{ attendee.attendee }}, duplicated: {{ (attendee.duplication | async) || 'pending' }}</li>
</ul>

在这个例子中,异步操作是字符串的重复,每次请求需要更长的时间来演示。

@Injectable()
export class DuplicateService {

  times = 1;

  constructor() { }

  getDuplicated(s: string): Observable<string> {
    return timer(1000 * this.times++).pipe(
      map(() => s + s),
    );
  }
}
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  private attendees: string[] = ['A', 'B', 'C', 'D', 'E', 'F'];

  attendeesWithData = this.attendees.map(attendee => ({ 
    attendee,
    duplication: this.duplicateService.getDuplicated(attendee),
  }));

  constructor(private duplicateService: DuplicateService) {}
}

结果,每一秒一个列表项都将其状态从挂起切换到重复的字符串。

另见stackblitz: https ://stackblitz.com/edit/angular-ivy-rtx6cg ? file = src/app/app.component.ts

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM