Skip to content

Commit f616de9

Browse files
committed
Added auto refresh for the trades.
1 parent 81fd61d commit f616de9

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

admin/src/app/asset-table/asset-table.component.ts

+31-35
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import {BotApi} from '../botapi';
55
import {BinanceService} from '../binance.service';
66
import {Mode, TradeDetailMode} from '../trade-details';
77
import {AuthService} from '../auth.service';
8-
import {Subscription} from 'rxjs';
8+
import {Observable, Subscription, timer} from 'rxjs';
99
import {TradeService} from '../trade.service';
1010
import {MatSort} from '@angular/material/sort';
1111
import {MatTableDataSource} from '@angular/material/table';
1212
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
1313
import { NotificationMessage, NotificationService, NotificatoinType } from '../services/notification.service';
1414
import { TradeDetailsComponent } from '../trade-details/trade-details.component';
15+
import { switchMap, tap } from 'rxjs/operators';
1516

1617
@Component({
1718
selector: 'app-asset-table',
@@ -23,15 +24,17 @@ export class AssetTableComponent implements OnInit, OnDestroy {
2324

2425
trades: TradeInfo[] = [];
2526
tradesDS: MatTableDataSource<TradeInfo> = null;
27+
ws_resubscribe: boolean = true;
2628
displayedColumns: string[] = ['ctrl-view-edit', 'sym', 'btc-val', 'price', 'balance', 'ctrl-pause', 'ctrl-close','ctrl-remove'];
27-
29+
refreshInterval: number = 60000;
2830
symTrade: { [symbol: string ]: TradeInfo} = {};
2931
tradesDisabled: Set<string> = new Set<string>();
3032
private selectedTrade: TradeInfo;
3133
private isCloseTradeAction?: boolean;
3234
private symbolObserver?: any;
33-
private loginSubscrition: Subscription;
35+
// private loginSubscrition: Subscription;
3436
private tradeNotificationSubscription: Subscription;
37+
private timerSubscibtion: Subscription;
3538
@ViewChild(MatSort) sort: MatSort;
3639

3740
constructor(
@@ -48,19 +51,7 @@ export class AssetTableComponent implements OnInit, OnDestroy {
4851
// }
4952

5053
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(); })
6455

6556
this.tradeNotificationSubscription = this.tradeService.eventAnounces$.subscribe(res => {
6657
if (res.type === 'created' || res.type === 'updated') {
@@ -70,31 +61,32 @@ export class AssetTableComponent implements OnInit, OnDestroy {
7061
}
7162

7263
ngOnDestroy() {
73-
this.loginSubscrition.unsubscribe();
64+
// this.loginSubscrition.unsubscribe();
7465
this.tradeNotificationSubscription.unsubscribe();
66+
this.timerSubscibtion.unsubscribe();
7567
}
7668

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(
9271
trades => {
9372
trades.forEach(t => t.btcVal = t.price * (t.avail + t.locked));
9473
this.trades = trades;
9574
this.tradesDS = new MatTableDataSource(this.trades);
9675
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+
9890
this.trades.forEach(t => this.symTrade[t.sym] = t);
9991
}
10092
, error => {
@@ -110,13 +102,17 @@ export class AssetTableComponent implements OnInit, OnDestroy {
110102
this.showAlert(error, NotificatoinType.Alert);
111103
}, () => {
112104
if (this.trades.length > 0) {
105+
if (!this.ws_resubscribe){
106+
return;
107+
}
108+
console.log(`Subscribing websockets`)
113109
if (this.symbolObserver) {
114110
this.symbolObserver.unsubscribe();
115111
}
116-
// this.binance.getOrderBookTickers().subscribe(orderbook => console.log(orderbook));
112+
117113
this.symbolObserver = this.binance.listenSymbols(
118114
this.trades.map(t => t.sym.toLowerCase())).subscribe(
119-
this.onExchangeSymbolRcvd.bind(this),
115+
res => this.onExchangeSymbolRcvd(res),
120116
err => console.log(err),
121117
() => console.log(`websocket completed`)
122118
);

0 commit comments

Comments
 (0)