【问题标题】:pooling using Rx JS until all data is processed使用 Rxjs 轮询,直到处理完所有数据
【发布时间】:2019-10-31 15:39:57
【问题描述】:

使用 rx.js 编写这个简短的池化例程的最佳方法是什么

 1. call the function this.dataService.getRowsByAccountId(id) to return  Observable<Row[]> from back-end
 2. send the received data to this function this.refreshGrid(data);
 3. if one of items in the data meet this criteria r.stillProcessing==true
 4. then wait 2 seconds and start again from step-1
 5. if another call was made to this routine and there is a pending timer scheduled. Don't schedule another one because i don't want multiple timers running.

【问题讨论】:

  • 你想做什么?

标签: javascript angular rxjs observable


【解决方案1】:

我认为最好的解决方案是使用retryWhen

我不知道这是否可以与您的代码一起使用,但根据您的评论尝试调整它。

this.dataService.getRowsByAccountId(id)
 .pipe(
  tap((data: Row[]) => this.refreshGrid(data)),
  map((data: Row[]) => {
    if (data.some((r: Row) => r.stillProcessing === true) {
      //error will be picked up by retryWhen
      throw r.Name; //(or something else)
    }
    return r;
  }),
  retryWhen(errors =>
    errors.pipe(
      //log error message
      tap((r: Row) => console.log(`Row ${r} is still processing!`)),
      //restart in 2 seconds
      delayWhen(() => timer(2000))
    )
  ).subscribe();
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    相关资源
    最近更新 更多