简体   繁体   English

Angular 8 中的链式后端请求

[英]Chain backend requests in Angular 8

I have a paged backend service that supports basic pagination so it can provide page x from the backend.我有一个支持基本分页的分页后端服务,因此它可以从后端提供页面 x。

I would like to call a service called refresh on all objects in the DB.我想在数据库中的所有对象上调用一个名为 refresh 的服务。

The first page tells me how manny pages I need to get.第一页告诉我需要多少页。

From there I need to iterate over the other pages and perform.从那里我需要遍历其他页面并执行。 the refresh action刷新动作

I would prefer to do this sequentially to do not DDOS my own server and since I can then show the user the progress of the operation.我宁愿按顺序执行此操作,以免对我自己的服务器进行 DDOS,因为我可以向用户显示操作的进度。

So far I have:到目前为止,我有:

        const pageSize = 20;
        this.service.Filter(null,null,null,1,pageSize).pipe(mergeMap(initalData => {
          const pageCount = initalData.pageInfo.pageCount;            
          let ids:string[] = initalData.payload.map(item => item.id);
          console.log('page',pageCount,1, initalData.pageInfo.page,ids);
          let ret = this.service.refresh(ids);
          for(let p=2;p<=pageCount;p++){
            console.log('refresh page', p);
            const pageResult = this.service.Filter(null,null,null,p,pageSize);
            pageResult.pipe(mergeMap(data => {
              let ids:string[] = data.payload.map(item => item.id);
              console.log('page', p, data.pageInfo.page,ids);
              const refreshResult = this.service.refresh(ids);
              return CombineLatest(ret, refreshResult);
            }));
          }                          
          return ret;
        })).subscribe(ret =>{
          console.log('final', ret);
        });

which only shows the log for the initial page and the final page also the refresh only happens on the first batch of items.它仅显示初始页面和最终页面的日志,并且刷新仅发生在第一批项目上。

EDIT:编辑:

This gets the job done but it feels like not clean code since you have to sub to every page request:这样就完成了工作,但感觉不像是干净的代码,因为您必须子到每个页面请求:

this.service.Filter(null,null,null,1,pageSize).pipe(mergeMap(initalData => {
          const pageCount = initalData.pageInfo.pageCount;            
          const result:Observable<any>[] = [];
          for(let p=1;p<=pageCount;p++){

            console.log('page', p);
            this.service.Filter(null,null,null,p,pageSize).subscribe(data => {
              if(data && data.payload) {
                let ids:string[] = data.payload.map(item => item.id);
                console.log('page',data.pageInfo.page, ids);
                let pageResult = this.service.refresh(ids);
                pageResult.subscribe();
                result.push(pageResult);
              }
            });                
          }                          
          return combineLatest(result);
        })).subscribe(ret =>{
          console.log('final', ret);
        });

As I understood you, forkJoin RxJS operator may be helpful in your case.据我了解, forkJoin RxJS 运算符可能对您的情况有所帮助。 This operator combines results of multiple HTTP requests.该运算符结合了多个 HTTP 请求的结果。 You get a tupil value from forkJoin operator.您从forkJoin运算符获得一个 tupil 值。 Multiple requests are performed in parallel, but tupil value is only emitted when all of these observables are completed.多个请求是并行执行的,但只有在所有这些 observables 都完成时才会发出 tupil 值。

Useful links to read more about this topic:阅读有关此主题的更多信息的有用链接:

https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin

https://rxjs.dev/api/index/function/forkJoin https://rxjs.dev/api/index/function/forkJoin

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

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