Skip to content

Commit 3fcad1e

Browse files
committed
Update parallel tutorial
1 parent 9ea7184 commit 3fcad1e

File tree

4 files changed

+64
-40
lines changed

4 files changed

+64
-40
lines changed

cloudapp/src/app/multi-select/select-entities/select-entities.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ export class SelectItem {
5050
checked: boolean;
5151
id: string;
5252
description: string;
53+
code: string;
5354
name: string;
5455

5556
constructor(item: Partial<SelectItem>, checker: (id: string) => boolean) {
5657
Object.assign(this, item);
57-
this.name = [this.description, this.id].join(' / ');
58+
this.name = (this.description || this.code) || this.id;
5859
this.checked = checker(item.id);
5960
}
6061
}
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
<div class="loading-shade" *ngIf="loading">
2+
<mat-progress-spinner
3+
mode="indeterminate"
4+
diameter="50"
5+
>
6+
</mat-progress-spinner>
7+
</div>
8+
<mat-form-field>
9+
<mat-label>Number of users to retrieve</mat-label>
10+
<mat-select [(value)]="num">
11+
<mat-option [value]="5">5</mat-option>
12+
<mat-option [value]="10">10</mat-option>
13+
<mat-option [value]="20">20</mat-option>
14+
<mat-option [value]="30">30</mat-option>
15+
<mat-option [value]="40">40</mat-option>
16+
<mat-option [value]="50">50</mat-option>
17+
</mat-select>
18+
</mat-form-field>
119
<p>
2-
<button mat-stroked-button type="button" color="primary" (click)="run()">Run</button>
20+
<button mat-stroked-button type="button" color="primary" (click)="run()">Retrieve users</button>
321
</p>
422
<ul>
523
<li *ngFor="let user of users">
6-
<strong>{{ user.name }}</strong>: ${{ user.fees.value }}
24+
<strong>{{ user.full_name }}</strong>: ${{ user.fees.value }}
725
</li>
826
</ul>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.loading-shade {
2+
position: fixed;
3+
top: 0;
4+
left: 0;
5+
height: 100%;
6+
width: 100%;
7+
background: rgba(0, 0, 0, 0.15);
8+
z-index: 1;
9+
display: flex;
10+
align-items: center;
11+
justify-content: center;
12+
}
Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { Component, OnInit } from '@angular/core';
22
import { 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';
55
import { 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
})
1412
export 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

7465
const getRandomInt = (max: number) => Math.floor(Math.random() * Math.floor(max));
7566
const 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

Comments
 (0)