пул с использованием Rx JS, пока все данные не будут обработаны - PullRequest
0 голосов
/ 31 октября 2019

Какой лучший способ написать эту короткую процедуру объединения с использованием 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.

1 Ответ

0 голосов
/ 31 октября 2019

Я думаю, что лучшим решением было бы использование 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();
);
...