UNPKG

dm-web-react

Version:

The DM web client with React.

724 lines 41.5 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import * as _ from "lodash"; import * as Cookies from "js-cookie"; import BrokerMarketSharedWorker from "shared-worker-loader!../workers/backgroundSharedWorker"; import { plainToClass, deserialize } from "class-transformer"; import { WorkerMessageType } from "../models/enum/workerMessageType"; import { SocketIoEventType } from "../models/enum/socketIoEventType"; import { SocketIoEventMessage } from "../models/socketIo/socketIoEventMessage"; import { SocketIoInnodealingMessage } from "../models/socketIo/socketIoInnodealingMessage"; import { DmLiteDatabaseService } from "./dmLiteDatabaseService"; import { HttpService } from "./httpService"; import { BondInfoTable } from "../databases/table/bondInfoTable"; import { DmLiteMessageService } from "./dmLiteMessageService"; import { BrokerMarketBestPriceTable } from "../databases/table/brokerMarketBestPriceTable"; import { BrokerMarketBestPrice, BondInfo } from "../models/entity/table"; import { InnodealingMessageType } from "../models/constants"; import { SocketIoEventWorkerMessage } from "../models/workerMessage/socketIoEventWorkerMessage"; import { SocketIoInnodealingWorkerMessage } from "../models/workerMessage/socketIoInnodealingWorkerMessage"; import { ArrayUtils, ObjectUtils } from "ts-commons"; import { PreloadingHeartbeatWorkerMessage } from "../models/workerMessage/preloadingHeartbeatWorkerMessage"; import { MessagePortHeartbeatWorkerMessage } from "../models/workerMessage/messagePortHeartbeatWorkerMessage"; var DmLitePreloadingService = /** @class */ (function () { function DmLitePreloadingService() { var _this = this; this.browserId = "main"; this.fetchBinlogCount = 100; this.tickerCount = 0; this.initBondInfoReady = false; this.initBrokerMarketBestPriceReady = false; //#endregion //#region socketio this.brokerMarketBestPricePushDataCache = []; this.bondInfoPushDataCache = []; this.isSavePushDataToDatabaseBusy = false; this.savePushDataToDatabaseTicker = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!!this.isSavePushDataToDatabaseBusy) return [3 /*break*/, 2]; this.isSavePushDataToDatabaseBusy = true; return [4 /*yield*/, this.savePushDataToDatabase()]; case 1: _a.sent(); this.isSavePushDataToDatabaseBusy = false; _a.label = 2; case 2: return [2 /*return*/]; } }); }); }; this.savePushDataToDatabase = function () { return __awaiter(_this, void 0, void 0, function () { var result, e_1, errMsg_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 5, , 6]); if (!(this.brokerMarketBestPricePushDataCache.length > 0)) return [3 /*break*/, 2]; return [4 /*yield*/, this.updateBrokerMarketBestPrices(this.brokerMarketBestPricePushDataCache)]; case 1: result = _a.sent(); console.log("* savePushDataToDatabase.brokerMarketBestPricePushDataCache: ", result); this.brokerMarketBestPricePushDataCache = []; _a.label = 2; case 2: if (!(this.bondInfoPushDataCache.length > 0)) return [3 /*break*/, 4]; return [4 /*yield*/, this.updateBondInfos(this.bondInfoPushDataCache)]; case 3: _a.sent(); console.log("* savePushDataToDatabase.bondInfoPushDataCache: ", this.bondInfoPushDataCache.length); this.bondInfoPushDataCache = []; _a.label = 4; case 4: return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 5: e_1 = _a.sent(); errMsg_1 = "* savePushDataToDatabase errer: " + (e_1.message || JSON.parse(e_1)); console.error(errMsg_1); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_1); })]; case 6: return [2 /*return*/]; } }); }); }; this.updateDataOnTicker = function () { return __awaiter(_this, void 0, void 0, function () { var index, e_2, errMsg; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 8, 9, 10]); console.log("initDataOnTicker start"); index = this.tickerCount % 9; if (!(index === 0)) return [3 /*break*/, 3]; if (!this.initBondInfoReady) return [3 /*break*/, 2]; return [4 /*yield*/, this.updateBondInfoByBinlog()]; case 1: _a.sent(); _a.label = 2; case 2: return [3 /*break*/, 7]; case 3: if (!(index === 1 || index === 3 || index === 5 || index === 7)) return [3 /*break*/, 6]; if (!this.initBrokerMarketBestPriceReady) return [3 /*break*/, 5]; return [4 /*yield*/, this.updateBrokerMarketBestPriceByBinlog()]; case 4: _a.sent(); _a.label = 5; case 5: return [3 /*break*/, 7]; case 6: if (index === 2 || index === 4 || index === 6 || index === 8) { // TODO update exchangeImageV } _a.label = 7; case 7: this.tickerCount++; return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 8: e_2 = _a.sent(); errMsg = "initDataOnTicker error: " + (e_2.message || JSON.stringify(e_2)); this.pushError(errMsg); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(e_2); })]; case 9: console.log("initDataOnTicker end"); return [7 /*endfinally*/]; case 10: return [2 /*return*/]; } }); }); }; this.databaseService = DmLiteDatabaseService.Instance; this.dmLiteMessageService = DmLiteMessageService.Instance; this.httpService = HttpService.Instance; var brokerMarketSharedWorker = new BrokerMarketSharedWorker(); this.brokerMarketSharedWorkerMessagePort = brokerMarketSharedWorker.port; this.brokerMarketSharedWorkerMessagePort.addEventListener("message", function (evt) { var data = evt.data; if (data.messageType === WorkerMessageType.SOCKETIO_INNODEALING_MESSAGE) { var message = plainToClass(SocketIoInnodealingWorkerMessage, data); var innodealingMessage = plainToClass(SocketIoInnodealingMessage, message.message); _this.handleSocketIoInnodealingMessage(innodealingMessage); } else if (data.messageType === WorkerMessageType.SOCKETIO_EVENT_MESSAGE) { var message = plainToClass(SocketIoEventWorkerMessage, data); var eventMessage = plainToClass(SocketIoEventMessage, message.message); _this.handleSocketIoEventMessage(eventMessage); } }); this.brokerMarketSharedWorkerMessagePort.start(); this.startPolling(); } Object.defineProperty(DmLitePreloadingService, "Instance", { get: function () { return this.instance; }, enumerable: true, configurable: true }); DmLitePreloadingService.prototype.registerProgessMsgEvent = function (event) { this.progressMsgEvent = event; }; //#region initialize data. DmLitePreloadingService.prototype.initUserInfo = function () { return __awaiter(this, void 0, void 0, function () { var vcard, e_3, errMsg_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, 3, 4]); console.log("initUserInfo start"); return [4 /*yield*/, this.httpService.getVcard()]; case 1: vcard = _a.sent(); console.log("get varcd success"); Cookies.set("userId", vcard.userid, { domain: ".innodealing.com", }); this.userId = vcard.userid; return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 2: e_3 = _a.sent(); console.error(e_3); errMsg_2 = "initUserInfo error: " + (e_3.message || JSON.stringify(e_3)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_2); })]; case 3: console.log("initUserInfo end"); return [7 /*endfinally*/]; case 4: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.initBinlogTable = function () { return __awaiter(this, void 0, void 0, function () { var createBinlogInfoTableResult, e_4, errMsg_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, 3, 4]); console.log("initBinlog start"); return [4 /*yield*/, this.databaseService.createBinlogInfoTable(this.browserId)]; case 1: createBinlogInfoTableResult = _a.sent(); console.log("createBinlogInfoTable succes: " + JSON.stringify(createBinlogInfoTableResult)); return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 2: e_4 = _a.sent(); console.error(e_4); errMsg_3 = "initBinlogTable error: " + (e_4.message || JSON.stringify(e_4)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_3); })]; case 3: console.log("initBinlog end"); return [7 /*endfinally*/]; case 4: return [2 /*return*/]; } }); }); }; //#region init bondInfo DmLitePreloadingService.prototype.initBondInfo = function () { return __awaiter(this, void 0, void 0, function () { var createBondInfoTableResult, binlogInfo, position, e_5, errMsg_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, 8, 9]); console.log("initBondInfo start"); console.log("create bond info table"); return [4 /*yield*/, this.databaseService.createBondInfoTable(this.browserId)]; case 1: createBondInfoTableResult = _a.sent(); console.log("createBondInfoTable success: " + JSON.stringify(createBondInfoTableResult)); if (!(createBondInfoTableResult > 0)) return [3 /*break*/, 3]; // need clear binlog bse new table created. return [4 /*yield*/, this.databaseService.deleteTablePosition(this.browserId, BondInfoTable.tableName)]; case 2: // need clear binlog bse new table created. _a.sent(); console.log("clear bondinfo position success"); _a.label = 3; case 3: return [4 /*yield*/, this.databaseService.getBinlogInfoByTableName(this.browserId, BondInfoTable.tableName)]; case 4: binlogInfo = _a.sent(); position = ObjectUtils.isNullOrUndefined(binlogInfo) ? 0 : binlogInfo.position; console.log("table: " + BondInfoTable.tableName + " position: " + position); return [4 /*yield*/, this.fillBondInfoWithCdn(position)]; case 5: _a.sent(); return [4 /*yield*/, this.updateBondInfoByBinlog()]; case 6: _a.sent(); this.initBondInfoReady = true; return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 7: e_5 = _a.sent(); console.error(e_5); errMsg_4 = "initBondInfo error: " + (e_5.message || JSON.stringify(e_5)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_4); })]; case 8: console.log("initBondInfo end"); return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.fillBondInfoWithCdn = function (position) { return __awaiter(this, void 0, void 0, function () { var config, i, url, msg, e_6, errMsg_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 8, 9, 10]); console.log("fillBondInfoWithCdn start"); return [4 /*yield*/, this.httpService.getSliceConfig(this.userId, position)]; case 1: config = _a.sent(); i = 0; _a.label = 2; case 2: if (!(i < config.urls.length)) return [3 /*break*/, 6]; url = config.urls[i]; return [4 /*yield*/, this.databaseService.fillBondInfoWithJsonGzipUrl(this.browserId, url)]; case 3: _a.sent(); msg = "(" + (i + 1) + "/" + config.urls.length + ")"; console.log(msg); return [4 /*yield*/, this.progressMsgEvent(msg)]; case 4: _a.sent(); _a.label = 5; case 5: i++; return [3 /*break*/, 2]; case 6: return [4 /*yield*/, this.databaseService.updateBinlogInfo(this.browserId, BondInfoTable.tableName, config.position)]; case 7: _a.sent(); console.log("update " + BondInfoTable.tableName + " postion success"); return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 8: e_6 = _a.sent(); console.error(e_6); errMsg_5 = "fillBondInfoWithCdn error: " + (e_6.message || JSON.stringify(e_6)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_5); })]; case 9: console.log("fillBondInfoWithCdn end"); return [7 /*endfinally*/]; case 10: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.updateBondInfoByBinlog = function () { return __awaiter(this, void 0, void 0, function () { var currentBinlogInfo, currentPosition, binlog, bondInfos, maxPosition, e_7, errMsg_6; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, 8, 9]); console.log("updateBondInfoByBinlog start"); return [4 /*yield*/, this.databaseService.getBinlogInfoByTableName(this.browserId, BondInfoTable.tableName)]; case 1: currentBinlogInfo = _a.sent(); currentPosition = 0; if (!ObjectUtils.isNullOrUndefined(currentBinlogInfo) && currentBinlogInfo && !ObjectUtils.isNullOrUndefined(currentBinlogInfo.position)) { currentPosition = currentBinlogInfo.position; } return [4 /*yield*/, this.httpService.getBondInfoBinlogs(this.userId, currentPosition, this.fetchBinlogCount)]; case 2: binlog = _a.sent(); console.log("fetch binlog, count: " + binlog.count + ", itemCount: " + binlog.binlogs.length); if (!!ArrayUtils.isEmpty(binlog.binlogs)) return [3 /*break*/, 6]; bondInfos = _.map(binlog.binlogs, function (x) { return deserialize(BondInfo, x.data); }); return [4 /*yield*/, this.databaseService.updateBondInfo(this.browserId, bondInfos)]; case 3: _a.sent(); maxPosition = _.max(_.map(binlog.binlogs, function (x) { return x.position; })); if (!maxPosition) return [3 /*break*/, 6]; return [4 /*yield*/, this.databaseService.updateBinlogInfo(this.browserId, BondInfoTable.tableName, maxPosition)]; case 4: _a.sent(); console.log("updateBondInfoByBinlog success, currentPosition: " + currentPosition); if (!(binlog.binlogs.length === this.fetchBinlogCount)) return [3 /*break*/, 6]; return [4 /*yield*/, this.updateBondInfoByBinlog()]; case 5: _a.sent(); _a.label = 6; case 6: return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 7: e_7 = _a.sent(); console.error(e_7); errMsg_6 = "updateBondInfoByBinlog error: " + (e_7.message || JSON.stringify(e_7)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_6); })]; case 8: console.log("updateBondInfoByBinlog end"); return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); }; //#endregion //#region init brokermarket DmLitePreloadingService.prototype.initBrokerMarketBestPrice = function () { return __awaiter(this, void 0, void 0, function () { var createBrokerMarketBestPriceTableResult, binlogInfo, position, e_8, errMsg_7; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, 8, 9]); console.log("initBrokerMarketBestPrice start"); return [4 /*yield*/, this.databaseService.createBrokerMarketBestPriceTable(this.browserId)]; case 1: createBrokerMarketBestPriceTableResult = _a.sent(); console.log("createBrokerMarketBestPriceTable success: " + JSON.stringify(createBrokerMarketBestPriceTableResult)); if (!(createBrokerMarketBestPriceTableResult > 0)) return [3 /*break*/, 3]; // need clear binlog bse new table created. return [4 /*yield*/, this.databaseService.deleteTablePosition(this.browserId, BrokerMarketBestPriceTable.tableName)]; case 2: // need clear binlog bse new table created. _a.sent(); console.log("clear BrokerMarketBestPrice position success"); _a.label = 3; case 3: return [4 /*yield*/, this.databaseService.getBinlogInfoByTableName(this.browserId, BrokerMarketBestPriceTable.tableName)]; case 4: binlogInfo = _a.sent(); position = ObjectUtils.isNullOrUndefined(binlogInfo) ? 0 : binlogInfo.position; console.log("table: " + BrokerMarketBestPriceTable.tableName + " position: " + position); return [4 /*yield*/, this.fillBrokerMarketBestPriceWithCdn(position)]; case 5: _a.sent(); return [4 /*yield*/, this.updateBrokerMarketBestPriceByBinlog()]; case 6: _a.sent(); this.initBrokerMarketBestPriceReady = true; return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 7: e_8 = _a.sent(); console.error(e_8); errMsg_7 = "initBrokerMarketBestPrice error: " + (e_8.message || JSON.stringify(e_8)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_7); })]; case 8: console.log("initBrokerMarketBestPrice end"); return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.fillBrokerMarketBestPriceWithCdn = function (position) { return __awaiter(this, void 0, void 0, function () { var config, i, url, msg, e_9, errMsg_8; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 8, 9, 10]); console.log("fillBrokerMarketBestPriceWithCdn start"); return [4 /*yield*/, this.httpService.getBrokeConfig(this.userId, position)]; case 1: config = _a.sent(); i = 0; _a.label = 2; case 2: if (!(i < config.urls.length)) return [3 /*break*/, 6]; url = config.urls[i]; return [4 /*yield*/, this.databaseService.fillBrokerMarketBestPriceWithJsonGzipUrl(this.browserId, url)]; case 3: _a.sent(); msg = "(" + (i + 1) + "/" + config.urls.length + ")"; console.log(msg); return [4 /*yield*/, this.progressMsgEvent(msg)]; case 4: _a.sent(); _a.label = 5; case 5: i++; return [3 /*break*/, 2]; case 6: return [4 /*yield*/, this.databaseService.updateBinlogInfo(this.browserId, BrokerMarketBestPriceTable.tableName, config.position)]; case 7: _a.sent(); console.log("update " + BrokerMarketBestPriceTable.tableName + " postion success"); return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 8: e_9 = _a.sent(); console.error(e_9); errMsg_8 = "fillBrokerMarketBestPriceWithCdn error: " + (e_9.message || JSON.stringify(e_9)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_8); })]; case 9: console.log("fillBrokerMarketBestPriceWithCdn end"); return [7 /*endfinally*/]; case 10: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.updateBrokerMarketBestPriceByBinlog = function () { return __awaiter(this, void 0, void 0, function () { var currentBinlogInfo, currentPosition, binlog, brokerMarketBestPrices, maxPosition, saveResult, e_10, errMsg_9; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, 8, 9]); console.log("updateBrokerMarketBestPriceByBinlog start"); return [4 /*yield*/, this.databaseService.getBinlogInfoByTableName(this.browserId, BrokerMarketBestPriceTable.tableName)]; case 1: currentBinlogInfo = _a.sent(); currentPosition = 0; if (!ObjectUtils.isNullOrUndefined(currentBinlogInfo) && currentBinlogInfo && !ObjectUtils.isNullOrUndefined(currentBinlogInfo.position)) { currentPosition = currentBinlogInfo.position; } return [4 /*yield*/, this.httpService.getBrokerMarketBestPriceBinlogs(this.userId, currentPosition, this.fetchBinlogCount)]; case 2: binlog = _a.sent(); console.log("fetch binlog, count: " + binlog.count + ", itemCount: " + binlog.binlogs.length); if (!!ArrayUtils.isEmpty(binlog.binlogs)) return [3 /*break*/, 6]; brokerMarketBestPrices = _.map(binlog.binlogs, function (x) { return deserialize(BrokerMarketBestPrice, x.data); }); return [4 /*yield*/, this.databaseService.updateBrokerMarketBestPrice(this.browserId, brokerMarketBestPrices)]; case 3: _a.sent(); maxPosition = _.max(_.map(binlog.binlogs, function (x) { return x.position; })); if (!maxPosition) return [3 /*break*/, 6]; return [4 /*yield*/, this.databaseService.updateBinlogInfo(this.browserId, BrokerMarketBestPriceTable.tableName, maxPosition)]; case 4: saveResult = _a.sent(); console.log("updateBrokerMarketBestPriceByBinlog success, currentPosition: " + currentPosition + ", saveResult: " + JSON.stringify(saveResult)); if (!(binlog.binlogs.length === this.fetchBinlogCount)) return [3 /*break*/, 6]; return [4 /*yield*/, this.updateBrokerMarketBestPriceByBinlog()]; case 5: _a.sent(); _a.label = 6; case 6: return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 7: e_10 = _a.sent(); console.error(e_10); errMsg_9 = "updateBrokerMarketBestPriceByBinlog error: " + (e_10.message || JSON.stringify(e_10)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_9); })]; case 8: console.log("updateBrokerMarketBestPriceByBinlog end"); return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); }; //#endregion DmLitePreloadingService.prototype.InitAppContent = function () { return __awaiter(this, void 0, void 0, function () { var config, result, e_11, errMsg_10; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, 3, 4]); console.log("InitAppContent start"); return [4 /*yield*/, this.httpService.getDMLiteConfig()]; case 1: config = _a.sent(); console.log("InitAppContent get config success"); result = this.dmLiteMessageService.InitAppContent(JSON.stringify(config)); console.log("InitAppContent result: " + ObjectUtils.toSafeString(result)); return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 2: e_11 = _a.sent(); console.error(e_11); errMsg_10 = "InitAppContent error: " + (e_11.message || JSON.stringify(e_11)); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_10); })]; case 3: console.log("InitAppContent end"); return [7 /*endfinally*/]; case 4: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.handleSocketIoInnodealingMessage = function (message) { console.log("handleSocketIoInnodealingMessage: ", message.messageId); try { switch (message.messageType) { case InnodealingMessageType.BROKER_MARKET_BEST_PRICE: if (this.initBrokerMarketBestPriceReady) { var bestPrice = plainToClass(BrokerMarketBestPrice, message.data); this.brokerMarketBestPricePushDataCache.push(bestPrice); } break; case InnodealingMessageType.BOND_INFO: if (this.initBondInfoReady) { var bondInfo = plainToClass(BondInfo, message.data); this.bondInfoPushDataCache.push(bondInfo); } break; } } catch (e) { console.error("handleSocketIoInnodealingMessage error: " + (e.message || JSON.parse(e))); } }; DmLitePreloadingService.prototype.handleSocketIoEventMessage = function (message) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(message.eventType === SocketIoEventType.CONNECT)) return [3 /*break*/, 1]; console.info("handleSocketIoEventMessage CONNECT!"); return [3 /*break*/, 5]; case 1: if (!(message.eventType === SocketIoEventType.DISCONNECT)) return [3 /*break*/, 2]; console.info("handleSocketIoEventMessage DISCONNECT!"); this.stopPolling(); return [3 /*break*/, 5]; case 2: if (!(message.eventType === SocketIoEventType.RECONNECT)) return [3 /*break*/, 4]; console.info("handleSocketIoEventMessage RECONNECT!"); return [4 /*yield*/, this.initDataOnSocketIoReconnect()]; case 3: _a.sent(); this.startPolling(); return [3 /*break*/, 5]; case 4: if (message.eventType === SocketIoEventType.ERROR) { console.error("handleSocketIoEventMessage error: " + message.error); } _a.label = 5; case 5: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.initDataOnSocketIoReconnect = function () { return __awaiter(this, void 0, void 0, function () { var e_12, errMsg_11; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, 4, 5]); console.log("initDataOnSocketIoReconnect start"); return [4 /*yield*/, this.initBondInfo()]; case 1: _a.sent(); console.log("initDataOnSocketIoReconnect.initBondInfo success"); return [4 /*yield*/, this.initBrokerMarketBestPrice()]; case 2: _a.sent(); console.log("initDataOnSocketIoReconnect.initBrokerMarketBestPrice success"); // TODO: notify data ready with shared worker. return [2 /*return*/, new Promise(function (resolve) { return resolve(); })]; case 3: e_12 = _a.sent(); errMsg_11 = "initDataOnSocketIoReconnect error: " + (e_12.message || JSON.stringify(e_12)); this.pushError(errMsg_11); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(errMsg_11); })]; case 4: console.log("initDataOnSocketIoReconnect end"); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.startPolling = function () { var _this = this; console.info("startPolling start"); this.stopPolling(); this.updateDataOnTickerHandle = window.setInterval(this.updateDataOnTicker, 10000); this.savePushDataToDatabaseTickerHandle = window.setInterval(this.savePushDataToDatabaseTicker, 1000); this.heartbeatTickerHandle = window.setInterval(function () { _this.sendPreloadingHeartbeat(); _this.sendMessagePortHeartbeat(); }, 10000); console.info("startPolling end"); }; DmLitePreloadingService.prototype.stopPolling = function () { console.info("stopPolling start"); window.clearInterval(this.updateDataOnTickerHandle); window.clearInterval(this.savePushDataToDatabaseTickerHandle); window.clearInterval(this.heartbeatTickerHandle); console.info("stopPolling end"); }; DmLitePreloadingService.prototype.updateBrokerMarketBestPrices = function (bestPrices) { return __awaiter(this, void 0, void 0, function () { var result_1, e_13; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.databaseService.updateBrokerMarketBestPrices(this.browserId, bestPrices)]; case 1: result_1 = _a.sent(); return [2 /*return*/, new Promise(function (resolve) { return resolve(result_1); })]; case 2: e_13 = _a.sent(); console.error(e_13); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(e_13); })]; case 3: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.updateBondInfos = function (bondInfos) { return __awaiter(this, void 0, void 0, function () { var result_2, e_14; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.databaseService.updateBondInfo(this.browserId, bondInfos)]; case 1: result_2 = _a.sent(); return [2 /*return*/, new Promise(function (resolve) { return resolve(result_2); })]; case 2: e_14 = _a.sent(); console.error(e_14); return [2 /*return*/, new Promise(function (resolve, reject) { return reject(e_14); })]; case 3: return [2 /*return*/]; } }); }); }; DmLitePreloadingService.prototype.sendPreloadingHeartbeat = function () { var heartbeat = new PreloadingHeartbeatWorkerMessage(); this.brokerMarketSharedWorkerMessagePort.postMessage(heartbeat); console.log("sendPreloadingHeartbeat: ", heartbeat.messageId); }; DmLitePreloadingService.prototype.sendMessagePortHeartbeat = function () { var heartbeat = new MessagePortHeartbeatWorkerMessage(); this.brokerMarketSharedWorkerMessagePort.postMessage(heartbeat); console.log("sendMessagePortHeartbeat: ", heartbeat.messageId); }; //#endregion DmLitePreloadingService.prototype.pushError = function (errMsg) { this.dmLiteMessageService.pushError(this.browserId, errMsg); }; DmLitePreloadingService.instance = new DmLitePreloadingService(); return DmLitePreloadingService; }()); export { DmLitePreloadingService }; //# sourceMappingURL=dmLitePreloadingService.js.map