-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdisplay-balance.component.ts
106 lines (79 loc) · 2.96 KB
/
display-balance.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
96
97
98
99
100
101
102
103
104
105
106
import { Component, OnInit } from '@angular/core';
import { WebSocketsService } from '../websockets.service';
import { InputDisplayCommService} from '../inputDisplayComm.service';
import { AccountBalance } from '../entities/accountBalance';
import { Observer} from 'rxjs';
@Component({
selector: 'app-display-balance',
templateUrl: './display-balance.component.html',
styleUrls: ['./display-balance.component.css']
})
export class DisplayBalanceComponent implements OnInit {
balance: string;
showBalance: boolean;
private currentBalanceVersion: number;
private readonly webSocketsService: WebSocketsService;
private readonly inputDisplayCommService: InputDisplayCommService;
constructor(webSocketsService: WebSocketsService,
inputDisplayCommService: InputDisplayCommService) {
this.webSocketsService = webSocketsService;
this.inputDisplayCommService = inputDisplayCommService;
}
ngOnInit(): void {
this.currentBalanceVersion = -1;
// we need the "self" constant because we cannot use "this" inside the functions below
const self = this;
const balanceResponsesObserver: Observer<AccountBalance> = {
next: function(accountBalance: AccountBalance): void {
self.process(accountBalance);
},
error: function(err: any): void {
console.error('Error: %o', err);
},
complete: function(): void {
console.log('No more account balance responses');
}
};
this.webSocketsService.subscribeToBalanceResponses(balanceResponsesObserver);
const balanceUpdatesObserver: Observer<AccountBalance> = {
next: function(accountBalance: AccountBalance): void {
self.process(accountBalance);
},
error: function(err: any): void {
console.error('Error: %o', err);
},
complete: function(): void {
console.log('No more account balance updates');
}
};
this.webSocketsService.subscribeToBalanceUpdates(balanceUpdatesObserver);
const clearBalanceObserver: Observer<void> = {
next: function(): void {
self.clearBalance();
},
error: function(err: any): void {
console.error('Error: %o', err);
},
complete: function(): void {
console.log('Complete called on the clearBalanceObserver');
}
};
this.inputDisplayCommService.subscribeToClearBalance(clearBalanceObserver);
}
private process(accountBalance: AccountBalance): void {
console.debug('Account Balance received through the observer:\n%o', accountBalance);
if (accountBalance.version > this.currentBalanceVersion) {
if (this.currentBalanceVersion === -1) {
this.showBalance = true;
}
this.balance = String(accountBalance.balance);
this.currentBalanceVersion = accountBalance.version;
}
}
private clearBalance(): void {
console.log('Going to clear the balance displayed');
this.showBalance = false;
this.currentBalanceVersion = -1;
this.balance = '';
}
}