哪个 rxjs/Observable 函数用于将递归 Observable 分页数据合并为单个流或数组

Which rxjs/Observable function to use for merging a recursive Observable paged data as a single stream or array

我正在使用 ajax 的 rxjs/Observable 获取大量数据。服务器以页面的形式输出数据。我正在尝试使用递增的页码进行递归 ajax 调用。目前我的代码在没有分页的情况下工作。但 我无法进行寻呼。

我用我的用例创建了一个 plunker 模拟数据和服务来重新创建场景。

将不胜感激任何帮助或指导。谢谢

export class App implements OnInit {        
    //mock db
    db:{[Id:string]:{data:number[],links:{next:number}}};

    uiData: number[];

    constructor() {
        this.name = `Angular! v${VERSION.full}`;

       //populate mock db with some data
       this.db = {
           '1_1': {data: [1,2,3,4,5], links: {next: null}},
           '2_1': {data: [1,2,3,4,5], links: {next: 2}},
           '2_2': {data: [6,7,8,9,10], links: {next: 3}},
           '2_3': {data: [11,12,13,14], links: {next: null}}
       };
    }

    /**
     * Mock db layer with API
     */
    private queryMasterData(key: string, page: number=1) {
       return Observable.of(this.db[`${key}_${page}`]);
    }

    /** Will be a angular service layer */
    private getPaged(key: string, page: number) {
        return this.queryMasterData(key, page).map(response => {
            if (response && response.links && response.links.next) {
                //? What Observable method to call? 
                return Observable.concat(Observable.of(response.data), 
                       this.getPaged(key, response.links.next).map(data => data));
            }

            if(!!response) {
                return response.data;
            }

            return [];
         });
     }

     /** Will be a angular service layer. UI can onlu call this */
     public getAll(key: string) {
         return this.getPaged(key, 1);
     }

     /** This is a component layer call */
     populateUI(key:string) {
         this.getAll(key).subscribe(data=>{
         console.log('populateUI', data);
         this.uiData = data;
     });
  }

  ngOnInit() {

  }  
}

有多种方法可以解决这个问题,但最重要的部分是记住要保持你正在处理的双重反应相同。 适用于您的 plunker 的解决方案:

private getPaged(key: string, page: number) {
  return this.queryMasterData(key, page).flatMap(response => {
    if (response && response.links && response.links.next) {
        return Observable.forkJoin(Observable.of(response.data), this.getPaged(key, response.links.next).map(data => data)).map(x => {
          let combined = x[0].concat(x[1]);
          return combined
      });
    }                    
  
    if(!!response) {
      return Observable.of(response.data);
    }     
    
    return [];
  });
}