-
Notifications
You must be signed in to change notification settings - Fork 576
/
blotterModel.js
146 lines (131 loc) · 4.64 KB
/
blotterModel.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import _ from 'lodash';
import { observeEvent } from 'esp-js';
import { viewBinding } from 'esp-js-react';
import { Environment } from '../../../system';
import { ModelBase, RegionManagerHelper } from '../../common';
import { RegionNames, view } from '../../regions';
import { TradeRow, TradeStatus, TradeNotification, RegionSettings } from '../../../services/model';
import { BlotterView } from '../views';
import logger from '../../../system/logger';
var _log = logger.create('BlotterModel');
const HIGHLIGHT_TRADE_FOR_IN_MS = 200;
@viewBinding(BlotterView)
export default class BlotterModel extends ModelBase {
_blotterService;
_regionManagerHelper;
_regionManager;
_regionName;
_regionSettings;
_schedulerService;
trades;
isConnected;
constructor(
modelId,
router,
blotterService,
regionManager,
openFin,
schedulerService,
) {
super(modelId, router);
this._blotterService = blotterService;
this.trades = [];
this.isConnected = false;
this._regionManager = regionManager;
this._regionName = RegionNames.blotter;
this._regionSettings = new RegionSettings('Blotter', 850, 280, false);
this._regionManagerHelper = new RegionManagerHelper(this._regionName, regionManager, this, this._regionSettings);
this._openFin = openFin;
this._schedulerService = schedulerService;
}
get canPopout() {
return Environment.isRunningInIE;
}
@observeEvent('init')
_onInit() {
_log.info(`Blotter starting`);
this._subscribeToConnectionStatus();
this._regionManagerHelper.init();
this.subscribeToOpenFinEvents();
}
@observeEvent('referenceDataLoaded')
_onReferenceDataLoaded() {
_log.info(`Ref data loaded, subscribing to trade stream`);
this._subscribeToTradeStream();
}
@observeEvent('tearOffBlotter')
_onTearOffBlotter() {
_log.info(`Popping out blotter`);
this._regionManagerHelper.popout();
}
@observeEvent('highlightTradeRow')
_highlightTradeRow(e) {
let index = _.findIndex(this.trades, traderRow => traderRow.trade.tradeId === e.trade.tradeId);
if (index >= 0) {
_log.debug(`Highlight trade ${e.trade.tradeId}`);
this.trades[index].isInFocus = true;
this._schedulerService.async.scheduleFuture('', HIGHLIGHT_TRADE_FOR_IN_MS, () => this.router.publishEvent(this.modelId, 'endHighlightTradeRow', {trade: e.trade}));
}
}
@observeEvent('endHighlightTradeRow')
_endHighlightTradeRow(e) {
let index = _.findIndex(this.trades, traderRow => traderRow.trade.tradeId === e.trade.tradeId);
if (index >= 0) {
_log.debug(`Stop highlighting trade ${e.trade.tradeId}`);
this.trades[index].isInFocus = false;
}
}
subscribeToOpenFinEvents(){
this._openFin.addSubscription('fetch-blotter', (msg, uuid) => {
let serialisedTrades = this.trades.map((t) => new TradeNotification(t.trade));
this._openFin.sendAllBlotterData(uuid, serialisedTrades);
});
}
_subscribeToTradeStream() {
this._disposables.add(
this._blotterService.getTradesStream()
.subscribeWithRouter(
this.router,
this.modelId,
(tradesUpdate) => {
let trades = tradesUpdate.trades;
if (tradesUpdate.isStateOfTheWorld) {
this.trades = _.sortBy(tradesUpdate.trades, 'tradeId').reverse().map(trade => new TradeRow(trade));
}else{
_.forEach(trades, (trade) => {
let existingTradeIndex = _.findIndex(this.trades, (t) => t.trade.tradeId === trade.tradeId);
if (existingTradeIndex !== -1) {
//update the existing trade
this.trades[existingTradeIndex] = new TradeRow(trade);
} else {
//add the existing trade and mark it as new
const tradeRow = new TradeRow(trade);
this.trades.unshift(tradeRow);
tradeRow.isNew = true;
}
//display a notification if the trade has a final status (Done or Rejected);
if ((trade.status === TradeStatus.Done || trade.status === TradeStatus.Rejected)) {
this._openFin.openTradeNotification(trade);
}
});
}
},
err => {
_log.error('Error on blotterService stream stream', err);
})
);
}
_subscribeToConnectionStatus() {
this.addDisposable(
this._blotterService.serviceStatusStream.subscribeWithRouter(
this.router,
this.modelId,
(status) => {
this.isConnected = status.isConnected;
if (!this.isConnected){
this.trades = [];
}
})
);
}
}