-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdisplay-transfers.component.ts
95 lines (71 loc) · 2.72 KB
/
display-transfers.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Component, OnInit } from '@angular/core';
import { Transfer } from '../entities/transfer';
import { WebSocketsService } from '../websockets.service';
import { InputDisplayCommService} from '../inputDisplayComm.service';
import { Observer} from 'rxjs';
@Component({
selector: 'app-display-transfers',
templateUrl: './display-transfers.component.html',
styleUrls: ['./display-transfers.component.css']
})
export class DisplayTransfersComponent implements OnInit {
readonly transfers: Array<Transfer> = new Array<Transfer>();
private currentAccountId: string;
private readonly webSocketsService: WebSocketsService;
private readonly inputDisplayCommService: InputDisplayCommService;
constructor(webSocketsService: WebSocketsService,
inputDisplayCommService: InputDisplayCommService) {
this.webSocketsService = webSocketsService;
this.inputDisplayCommService = inputDisplayCommService;
}
ngOnInit(): void {
// we need the "self" constant because we cannot use "this" inside the functions below
const self = this;
const accountIdEnteredObserver: Observer<string> = {
next: function(accountId: string): void {
// Empty the array
self.transfers.length = 0;
self.currentAccountId = accountId;
},
error: function(err: any): void {
console.error('Error: %o', err);
},
complete: function(): void {
console.log('Observer completed, no more account ids coming from the user');
}
};
this.inputDisplayCommService.subscribeToAccountIdEntered(accountIdEnteredObserver);
const transferReceivedObserver: Observer<Transfer> = {
next: function(transfer: Transfer): void {
self.process(transfer);
},
error: function(err: any): void {
console.error('Error: %o', err);
},
complete: function(): void {
console.log('Observer completed, no more Transfer data messages coming from the server');
}
};
this.webSocketsService.subscribeToTransfers(transferReceivedObserver);
}
private process(transfer: Transfer): void {
if (transfer.accountId !== this.currentAccountId) {
console.error('The account id of the received transfer object is not the expected one, '
+ 'expected: ' + this.currentAccountId + ', received: ' + transfer.accountId );
return;
}
this.transfers.push(transfer);
this.transfers.sort((transfer1, transfer2) => {
if (transfer1.transferTS > transfer2.transferTS) {
return 1;
}
if (transfer1.transferTS < transfer2.transferTS) {
return -1;
}
return 0;
});
}
trackByFn(index: number, transfer: Transfer): string {
return transfer.id;
}
}