@@ -5,13 +5,14 @@ import {BotApi} from '../botapi';
5
5
import { BinanceService } from '../binance.service' ;
6
6
import { Mode , TradeDetailMode } from '../trade-details' ;
7
7
import { AuthService } from '../auth.service' ;
8
- import { Subscription } from 'rxjs' ;
8
+ import { Observable , Subscription , timer } from 'rxjs' ;
9
9
import { TradeService } from '../trade.service' ;
10
10
import { MatSort } from '@angular/material/sort' ;
11
11
import { MatTableDataSource } from '@angular/material/table' ;
12
12
import { MatDialog , MatDialogRef } from '@angular/material/dialog' ;
13
13
import { NotificationMessage , NotificationService , NotificatoinType } from '../services/notification.service' ;
14
14
import { TradeDetailsComponent } from '../trade-details/trade-details.component' ;
15
+ import { switchMap , tap } from 'rxjs/operators' ;
15
16
16
17
@Component ( {
17
18
selector : 'app-asset-table' ,
@@ -23,15 +24,17 @@ export class AssetTableComponent implements OnInit, OnDestroy {
23
24
24
25
trades : TradeInfo [ ] = [ ] ;
25
26
tradesDS : MatTableDataSource < TradeInfo > = null ;
27
+ ws_resubscribe : boolean = true ;
26
28
displayedColumns : string [ ] = [ 'ctrl-view-edit' , 'sym' , 'btc-val' , 'price' , 'balance' , 'ctrl-pause' , 'ctrl-close' , 'ctrl-remove' ] ;
27
-
29
+ refreshInterval : number = 60000 ;
28
30
symTrade : { [ symbol : string ] : TradeInfo } = { } ;
29
31
tradesDisabled : Set < string > = new Set < string > ( ) ;
30
32
private selectedTrade : TradeInfo ;
31
33
private isCloseTradeAction ?: boolean ;
32
34
private symbolObserver ?: any ;
33
- private loginSubscrition : Subscription ;
35
+ // private loginSubscrition: Subscription;
34
36
private tradeNotificationSubscription : Subscription ;
37
+ private timerSubscibtion : Subscription ;
35
38
@ViewChild ( MatSort ) sort : MatSort ;
36
39
37
40
constructor (
@@ -48,19 +51,7 @@ export class AssetTableComponent implements OnInit, OnDestroy {
48
51
// }
49
52
50
53
ngOnInit ( ) {
51
- this . loginSubscrition = this . auth . loginEventAnounced$ . subscribe ( res => {
52
- if ( res === true ) {
53
- console . log ( "login" )
54
- this . refreshTrades ( ) ;
55
- }
56
- }
57
- ) ;
58
- // this.validateAllTradesPaused();
59
- // this.refreshTrades();
60
- // this.checkAuth();
61
- if ( this . auth . isLoggedIn ( ) ) {
62
- this . refreshTrades ( ) ;
63
- }
54
+ this . timerSubscibtion = timer ( 0 , this . refreshInterval ) . subscribe ( tick => { this . refreshTrades ( ) ; } )
64
55
65
56
this . tradeNotificationSubscription = this . tradeService . eventAnounces$ . subscribe ( res => {
66
57
if ( res . type === 'created' || res . type === 'updated' ) {
@@ -70,31 +61,32 @@ export class AssetTableComponent implements OnInit, OnDestroy {
70
61
}
71
62
72
63
ngOnDestroy ( ) {
73
- this . loginSubscrition . unsubscribe ( ) ;
64
+ // this.loginSubscrition.unsubscribe();
74
65
this . tradeNotificationSubscription . unsubscribe ( ) ;
66
+ this . timerSubscibtion . unsubscribe ( ) ;
75
67
}
76
68
77
- // checkAuth() {
78
- // if (!this.auth.isLoggedIn()) {
79
- // this.auth.login('test', 'test').subscribe(
80
- // res => { if (res.jwt) {
81
- // this.refreshTrades();
82
- // }}
83
- // );
84
- // } else {
85
- // this.refreshTrades();
86
- // }
87
- // }
88
-
89
- refreshTrades ( ) {
90
- console . log ( "refresh trades" )
91
- this . api . getActiveTrades ( ) . subscribe (
69
+ refreshTrades ( ) : Subscription {
70
+ return this . api . getActiveTrades ( ) . subscribe (
92
71
trades => {
93
72
trades . forEach ( t => t . btcVal = t . price * ( t . avail + t . locked ) ) ;
94
73
this . trades = trades ;
95
74
this . tradesDS = new MatTableDataSource ( this . trades ) ;
96
75
this . tradesDS . sort = this . sort ;
97
- this . symTrade = { } ;
76
+ this . ws_resubscribe = false ;
77
+
78
+ let newSymbols = this . trades . map ( t => t . sym )
79
+ let oldSymbols = Object . keys ( this . symTrade )
80
+
81
+ if ( newSymbols . length != oldSymbols . length ||
82
+ ( newSymbols . sort ( ) . join ( ) !== oldSymbols . sort ( ) . join ( ) ) ) {
83
+ this . ws_resubscribe = true ;
84
+ }
85
+
86
+ if ( this . ws_resubscribe ) {
87
+ this . symTrade = { } ;
88
+ }
89
+
98
90
this . trades . forEach ( t => this . symTrade [ t . sym ] = t ) ;
99
91
}
100
92
, error => {
@@ -110,13 +102,17 @@ export class AssetTableComponent implements OnInit, OnDestroy {
110
102
this . showAlert ( error , NotificatoinType . Alert ) ;
111
103
} , ( ) => {
112
104
if ( this . trades . length > 0 ) {
105
+ if ( ! this . ws_resubscribe ) {
106
+ return ;
107
+ }
108
+ console . log ( `Subscribing websockets` )
113
109
if ( this . symbolObserver ) {
114
110
this . symbolObserver . unsubscribe ( ) ;
115
111
}
116
- // this.binance.getOrderBookTickers().subscribe(orderbook => console.log(orderbook));
112
+
117
113
this . symbolObserver = this . binance . listenSymbols (
118
114
this . trades . map ( t => t . sym . toLowerCase ( ) ) ) . subscribe (
119
- this . onExchangeSymbolRcvd . bind ( this ) ,
115
+ res => this . onExchangeSymbolRcvd ( res ) ,
120
116
err => console . log ( err ) ,
121
117
( ) => console . log ( `websocket completed` )
122
118
) ;
0 commit comments