11import { Component , OnInit } from '@angular/core' ;
22import { CloudAppRestService , RestErrorResponse } from '@exlibris/exl-cloudapp-angular-lib' ;
3- import { mergeMap , map , catchError } from 'rxjs/operators' ;
4- import { from , of } from 'rxjs' ;
3+ import { mergeMap , map , catchError , switchMap } from 'rxjs/operators' ;
4+ import { from , of , forkJoin , Observable } from 'rxjs' ;
55import { AppService } from '../app.service' ;
66
7- const CONCURRENT_REQUESTS = 5 ;
8-
97@Component ( {
108 selector : 'app-parallel' ,
119 templateUrl : './parallel.component.html' ,
1210 styleUrls : [ './parallel.component.scss' ]
1311} )
1412export class ParallelComponent implements OnInit {
15- users : Array < { name : string , fees : number } > ;
13+ users : any [ ] ;
14+ num = 10 ;
15+ loading = false ;
1616
1717 constructor (
1818 private restService : CloudAppRestService ,
@@ -25,44 +25,35 @@ export class ParallelComponent implements OnInit {
2525
2626 run ( ) {
2727 this . users = [ ] ;
28- this . getUsers ( ) . subscribe ( users => this . loadUsers ( users . user ) ) ;
29- }
30-
31- loadUsers ( users : any [ ] ) {
32- from ( users ) . pipe (
33- mergeMap ( user => this . restService . call ( `/users/${ user . primary_id } ?expand=fees` ) ,
34- CONCURRENT_REQUESTS ) ,
35- map ( user => ( { name : user . full_name , fees : user . fees } ) )
36- )
37- . subscribe ( s => this . users . push ( s ) ) ;
28+ this . loadUsers ( ) ;
3829 }
3930
40- loadUsersWithErrors ( users : any [ ] ) {
41- from ( this . addErrors ( users ) ) . pipe (
42- mergeMap ( user => this . getUser ( user ) ,
43- CONCURRENT_REQUESTS )
31+ loadUsers ( ) {
32+ this . loading = true ;
33+ this . restService . call ( `/users?limit=${ this . num } ` )
34+ . pipe (
35+ map ( users =>
36+ this . addErrors ( users . user )
37+ . map ( user => withErrorChecking (
38+ this . restService . call ( `/users/${ user . primary_id } ?expand=fees` )
39+ ) )
40+ ) ,
41+ switchMap ( reqs => forkJoin ( reqs ) ) ,
4442 )
45- . subscribe ( s => {
46- if ( isRestErrorResponse ( s ) ) {
47- console . log ( 'Error retrieving user:' , s . message ) ;
48- } else {
49- this . users . push ( s )
50- }
43+ . subscribe ( {
44+ next : ( s : any [ ] ) => {
45+ s . forEach ( user => {
46+ if ( isRestErrorResponse ( user ) ) {
47+ console . log ( 'Error retrieving user: ' + user . message )
48+ } else {
49+ this . users . push ( user ) ;
50+ }
51+ } )
52+ } ,
53+ complete : ( ) => this . loading = false
5154 } ) ;
5255 }
5356
54- getUsers ( ) {
55- return this . restService . call ( '/users?limit=50' ) ;
56- }
57-
58- getUser ( user ) {
59- return this . restService . call ( `/users/${ user . primary_id } ?expand=fees` )
60- . pipe (
61- map ( user => ( { name : user . full_name , fees : user . fees } ) ) ,
62- catchError ( e => of ( e ) )
63- )
64- }
65-
6657 addErrors ( users : any [ ] ) {
6758 for ( let i = 0 ; i < Math . floor ( users . length * .25 ) ; i ++ ) {
6859 users . splice ( getRandomInt ( users . length - 1 ) , 0 , { primary_id : getRandomInt ( 1000000 ) } ) ;
@@ -73,3 +64,5 @@ export class ParallelComponent implements OnInit {
7364
7465const getRandomInt = ( max : number ) => Math . floor ( Math . random ( ) * Math . floor ( max ) ) ;
7566const isRestErrorResponse = ( object : any ) : object is RestErrorResponse => 'error' in object ;
67+ const withErrorChecking = ( obs : Observable < any > ) : Observable < any > =>
68+ obs . pipe ( catchError ( e => of ( e ) ) ) ;
0 commit comments