简体   繁体   中英

How to handle view for list of async progresses in angular

I need to show a list of progresses in angular. I want to send REST requests for all of the items in the list. Think we have list of items and we will change settings in the backend for each of the item. Even one request needs around 10 seconds. I want to send the requests parallely. I will show the progress of the situation in the list. I dont want requests should wait each other. So I searched the RxJS library documents from https://rxjs-dev.firebaseapp.com/ I see I can use forkJoin or merge or concat methods but neither of them is not suitable for my needs I think. Because if I use forkJoin it will wait for all of the requests. If I use merge I dont have the reliability I think, because if the two of the requests complete at the same time I will loose one of the responses. Concat method is processing the requests one by one. What I should do to achieve this?

EDIT So I have this method in myHttpService:

updateSetting(id: string, setting: Setting): Observable<any> {
    return this.http.post(this.updateSettingEndpoint + id, setting);
}

This endpoints takes a while to complete. I want to call this method in parallel for lots of ids. So in my other service I have this method

import { Observable, merge } from 'rxjs';
...
public saveMultipleSettings(idList: string[], setting: Setting) {

    const observables: Array<Observable<any>> = new Array();

    for (let i = 0; i < idList.length; i++) {
        observables.push(this.myHttpService.updateSetting(idList[i], setting));
    }
    return merge(observables);
}

Just need to take a percentage count of observables completed with scan

merge(observables).pipe(scan((curr,acc)=>
(1/observables.length+acc)*100 // compute percentage
,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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