Release v20.028 (from a477abd4)
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
import { getErrorMessage, logMessage, } from './helpers';
|
||||
function extractField(data, field, arrayIndex, valueIsArray) {
|
||||
var value = data[field];
|
||||
const value = data[field];
|
||||
if (Array.isArray(value) && (!valueIsArray || Array.isArray(value[0]))) {
|
||||
return value[arrayIndex];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
function symbolWithCurrencyKey(symbol, currency) {
|
||||
function symbolKey(symbol, currency, unit) {
|
||||
// here we're using a separator that quite possible shouldn't be in a real symbol name
|
||||
return symbol + (currency !== undefined ? '_%|#|%_' + currency : '');
|
||||
return symbol + (currency !== undefined ? '_%|#|%_' + currency : '') + (unit !== undefined ? '_%|#|%_' + unit : '');
|
||||
}
|
||||
var SymbolsStorage = /** @class */ (function () {
|
||||
function SymbolsStorage(datafeedUrl, datafeedSupportedResolutions, requester) {
|
||||
export class SymbolsStorage {
|
||||
constructor(datafeedUrl, datafeedSupportedResolutions, requester) {
|
||||
this._exchangesList = ['NYSE', 'FOREX', 'AMEX'];
|
||||
this._symbolsInfo = {};
|
||||
this._symbolsList = [];
|
||||
@@ -19,59 +19,53 @@ var SymbolsStorage = /** @class */ (function () {
|
||||
this._datafeedSupportedResolutions = datafeedSupportedResolutions;
|
||||
this._requester = requester;
|
||||
this._readyPromise = this._init();
|
||||
this._readyPromise.catch(function (error) {
|
||||
this._readyPromise.catch((error) => {
|
||||
// seems it is impossible
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error("SymbolsStorage: Cannot init, error=" + error.toString());
|
||||
console.error(`SymbolsStorage: Cannot init, error=${error.toString()}`);
|
||||
});
|
||||
}
|
||||
// BEWARE: this function does not consider symbol's exchange
|
||||
SymbolsStorage.prototype.resolveSymbol = function (symbolName, currencyCode) {
|
||||
var _this = this;
|
||||
return this._readyPromise.then(function () {
|
||||
var symbolInfo = _this._symbolsInfo[symbolWithCurrencyKey(symbolName, currencyCode)];
|
||||
resolveSymbol(symbolName, currencyCode, unitId) {
|
||||
return this._readyPromise.then(() => {
|
||||
const symbolInfo = this._symbolsInfo[symbolKey(symbolName, currencyCode, unitId)];
|
||||
if (symbolInfo === undefined) {
|
||||
return Promise.reject('invalid symbol');
|
||||
}
|
||||
return Promise.resolve(symbolInfo);
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype.searchSymbols = function (searchString, exchange, symbolType, maxSearchResults) {
|
||||
var _this = this;
|
||||
return this._readyPromise.then(function () {
|
||||
var weightedResult = [];
|
||||
var queryIsEmpty = searchString.length === 0;
|
||||
}
|
||||
searchSymbols(searchString, exchange, symbolType, maxSearchResults) {
|
||||
return this._readyPromise.then(() => {
|
||||
const weightedResult = [];
|
||||
const queryIsEmpty = searchString.length === 0;
|
||||
searchString = searchString.toUpperCase();
|
||||
var _loop_1 = function (symbolName) {
|
||||
var symbolInfo = _this._symbolsInfo[symbolName];
|
||||
for (const symbolName of this._symbolsList) {
|
||||
const symbolInfo = this._symbolsInfo[symbolName];
|
||||
if (symbolInfo === undefined) {
|
||||
return "continue";
|
||||
continue;
|
||||
}
|
||||
if (symbolType.length > 0 && symbolInfo.type !== symbolType) {
|
||||
return "continue";
|
||||
continue;
|
||||
}
|
||||
if (exchange && exchange.length > 0 && symbolInfo.exchange !== exchange) {
|
||||
return "continue";
|
||||
continue;
|
||||
}
|
||||
var positionInName = symbolInfo.name.toUpperCase().indexOf(searchString);
|
||||
var positionInDescription = symbolInfo.description.toUpperCase().indexOf(searchString);
|
||||
const positionInName = symbolInfo.name.toUpperCase().indexOf(searchString);
|
||||
const positionInDescription = symbolInfo.description.toUpperCase().indexOf(searchString);
|
||||
if (queryIsEmpty || positionInName >= 0 || positionInDescription >= 0) {
|
||||
var alreadyExists = weightedResult.some(function (item) { return item.symbolInfo === symbolInfo; });
|
||||
const alreadyExists = weightedResult.some((item) => item.symbolInfo === symbolInfo);
|
||||
if (!alreadyExists) {
|
||||
var weight = positionInName >= 0 ? positionInName : 8000 + positionInDescription;
|
||||
const weight = positionInName >= 0 ? positionInName : 8000 + positionInDescription;
|
||||
weightedResult.push({ symbolInfo: symbolInfo, weight: weight });
|
||||
}
|
||||
}
|
||||
};
|
||||
for (var _i = 0, _a = _this._symbolsList; _i < _a.length; _i++) {
|
||||
var symbolName = _a[_i];
|
||||
_loop_1(symbolName);
|
||||
}
|
||||
var result = weightedResult
|
||||
.sort(function (item1, item2) { return item1.weight - item2.weight; })
|
||||
const result = weightedResult
|
||||
.sort((item1, item2) => item1.weight - item2.weight)
|
||||
.slice(0, maxSearchResults)
|
||||
.map(function (item) {
|
||||
var symbolInfo = item.symbolInfo;
|
||||
.map((item) => {
|
||||
const symbolInfo = item.symbolInfo;
|
||||
return {
|
||||
symbol: symbolInfo.name,
|
||||
full_name: symbolInfo.full_name,
|
||||
@@ -84,13 +78,11 @@ var SymbolsStorage = /** @class */ (function () {
|
||||
});
|
||||
return Promise.resolve(result);
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype._init = function () {
|
||||
var _this = this;
|
||||
var promises = [];
|
||||
var alreadyRequestedExchanges = {};
|
||||
for (var _i = 0, _a = this._exchangesList; _i < _a.length; _i++) {
|
||||
var exchange = _a[_i];
|
||||
}
|
||||
_init() {
|
||||
const promises = [];
|
||||
const alreadyRequestedExchanges = {};
|
||||
for (const exchange of this._exchangesList) {
|
||||
if (alreadyRequestedExchanges[exchange]) {
|
||||
continue;
|
||||
}
|
||||
@@ -98,18 +90,17 @@ var SymbolsStorage = /** @class */ (function () {
|
||||
promises.push(this._requestExchangeData(exchange));
|
||||
}
|
||||
return Promise.all(promises)
|
||||
.then(function () {
|
||||
_this._symbolsList.sort();
|
||||
.then(() => {
|
||||
this._symbolsList.sort();
|
||||
logMessage('SymbolsStorage: All exchanges data loaded');
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype._requestExchangeData = function (exchange) {
|
||||
var _this = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this._requester.sendRequest(_this._datafeedUrl, 'symbol_info', { group: exchange })
|
||||
.then(function (response) {
|
||||
}
|
||||
_requestExchangeData(exchange) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._requester.sendRequest(this._datafeedUrl, 'symbol_info', { group: exchange })
|
||||
.then((response) => {
|
||||
try {
|
||||
_this._onExchangeDataReceived(exchange, response);
|
||||
this._onExchangeDataReceived(exchange, response);
|
||||
}
|
||||
catch (error) {
|
||||
reject(error);
|
||||
@@ -117,25 +108,26 @@ var SymbolsStorage = /** @class */ (function () {
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(function (reason) {
|
||||
logMessage("SymbolsStorage: Request data for exchange '" + exchange + "' failed, reason=" + getErrorMessage(reason));
|
||||
.catch((reason) => {
|
||||
logMessage(`SymbolsStorage: Request data for exchange '${exchange}' failed, reason=${getErrorMessage(reason)}`);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype._onExchangeDataReceived = function (exchange, data) {
|
||||
var symbolIndex = 0;
|
||||
}
|
||||
_onExchangeDataReceived(exchange, data) {
|
||||
let symbolIndex = 0;
|
||||
try {
|
||||
var symbolsCount = data.symbol.length;
|
||||
var tickerPresent = data.ticker !== undefined;
|
||||
const symbolsCount = data.symbol.length;
|
||||
const tickerPresent = data.ticker !== undefined;
|
||||
for (; symbolIndex < symbolsCount; ++symbolIndex) {
|
||||
var symbolName = data.symbol[symbolIndex];
|
||||
var listedExchange = extractField(data, 'exchange-listed', symbolIndex);
|
||||
var tradedExchange = extractField(data, 'exchange-traded', symbolIndex);
|
||||
var fullName = tradedExchange + ':' + symbolName;
|
||||
var currencyCode = extractField(data, 'currency-code', symbolIndex);
|
||||
var ticker = tickerPresent ? extractField(data, 'ticker', symbolIndex) : symbolName;
|
||||
var symbolInfo = {
|
||||
const symbolName = data.symbol[symbolIndex];
|
||||
const listedExchange = extractField(data, 'exchange-listed', symbolIndex);
|
||||
const tradedExchange = extractField(data, 'exchange-traded', symbolIndex);
|
||||
const fullName = tradedExchange + ':' + symbolName;
|
||||
const currencyCode = extractField(data, 'currency-code', symbolIndex);
|
||||
const unitId = extractField(data, 'unit-id', symbolIndex);
|
||||
const ticker = tickerPresent ? extractField(data, 'ticker', symbolIndex) : symbolName;
|
||||
const symbolInfo = {
|
||||
ticker: ticker,
|
||||
name: symbolName,
|
||||
base_name: [listedExchange + ':' + symbolName],
|
||||
@@ -144,6 +136,9 @@ var SymbolsStorage = /** @class */ (function () {
|
||||
exchange: tradedExchange,
|
||||
currency_code: currencyCode,
|
||||
original_currency_code: extractField(data, 'original-currency-code', symbolIndex),
|
||||
unit_id: unitId,
|
||||
original_unit_id: extractField(data, 'original-unit-id', symbolIndex),
|
||||
unit_conversion_types: extractField(data, 'unit-conversion-types', symbolIndex, true),
|
||||
description: extractField(data, 'description', symbolIndex),
|
||||
has_intraday: definedValueOrDefault(extractField(data, 'has-intraday', symbolIndex), false),
|
||||
has_no_volume: definedValueOrDefault(extractField(data, 'has-no-volume', symbolIndex), false),
|
||||
@@ -165,21 +160,19 @@ var SymbolsStorage = /** @class */ (function () {
|
||||
this._symbolsInfo[ticker] = symbolInfo;
|
||||
this._symbolsInfo[symbolName] = symbolInfo;
|
||||
this._symbolsInfo[fullName] = symbolInfo;
|
||||
if (currencyCode !== undefined) {
|
||||
this._symbolsInfo[symbolWithCurrencyKey(ticker, currencyCode)] = symbolInfo;
|
||||
this._symbolsInfo[symbolWithCurrencyKey(symbolName, currencyCode)] = symbolInfo;
|
||||
this._symbolsInfo[symbolWithCurrencyKey(fullName, currencyCode)] = symbolInfo;
|
||||
if (currencyCode !== undefined || unitId !== undefined) {
|
||||
this._symbolsInfo[symbolKey(ticker, currencyCode, unitId)] = symbolInfo;
|
||||
this._symbolsInfo[symbolKey(symbolName, currencyCode, unitId)] = symbolInfo;
|
||||
this._symbolsInfo[symbolKey(fullName, currencyCode, unitId)] = symbolInfo;
|
||||
}
|
||||
this._symbolsList.push(symbolName);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
throw new Error("SymbolsStorage: API error when processing exchange " + exchange + " symbol #" + symbolIndex + " (" + data.symbol[symbolIndex] + "): " + error.message);
|
||||
throw new Error(`SymbolsStorage: API error when processing exchange ${exchange} symbol #${symbolIndex} (${data.symbol[symbolIndex]}): ${error.message}`);
|
||||
}
|
||||
};
|
||||
return SymbolsStorage;
|
||||
}());
|
||||
export { SymbolsStorage };
|
||||
}
|
||||
}
|
||||
function definedValueOrDefault(value, defaultValue) {
|
||||
return value !== undefined ? value : defaultValue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user