commit 2017-06-29 16:43:00.580192

Allow a feed to return more data than requested to optimize server requests #1814
This commit is contained in:
Jenkins
2017-06-29 11:43:29 -05:00
parent 2b65249f1f
commit e83c759d19
5 changed files with 30 additions and 21 deletions

View File

@@ -6,6 +6,15 @@
https://github.com/tradingview/charting_library/wiki/UDF
*/
function parseJSONorNot(mayBeJSON) {
if (typeof mayBeJSON === 'string') {
return JSON.parse(mayBeJSON);
} else {
return mayBeJSON;
}
}
var Datafeeds = {};
Datafeeds.UDFCompatibleDatafeed = function(datafeedURL, updateFrequency) {
@@ -101,7 +110,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype._initialize = function() {
this._send(this._datafeedURL + '/config')
.done(function(response) {
var configurationData = JSON.parse(response);
var configurationData = parseJSONorNot(response);
that._setupWithConfiguration(configurationData);
})
.fail(function(reason) {
@@ -168,7 +177,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype.getMarks = function(symbolInfo, rangeS
resolution: resolution
})
.done(function(response) {
onDataCallback(JSON.parse(response));
onDataCallback(parseJSONorNot(response));
})
.fail(function() {
onDataCallback([]);
@@ -185,7 +194,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype.getTimescaleMarks = function(symbolInf
resolution: resolution
})
.done(function(response) {
onDataCallback(JSON.parse(response));
onDataCallback(parseJSONorNot(response));
})
.fail(function() {
onDataCallback([]);
@@ -209,7 +218,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype.searchSymbols = function(searchString,
exchange: exchange
})
.done(function(response) {
var data = JSON.parse(response);
var data = parseJSONorNot(response);
for (var i = 0; i < data.length; ++i) {
if (!data[i].params) {
@@ -285,7 +294,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype.resolveSymbol = function(symbolName, o
symbol: symbolName ? symbolName.toUpperCase() : ''
})
.done(function(response) {
var data = JSON.parse(response);
var data = parseJSONorNot(response);
if (data.s && data.s !== 'ok') {
onResolveErrorCallback('unknown_symbol');
@@ -323,7 +332,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype.getBars = function(symbolInfo, resolut
to: rangeEndDate
})
.done(function(response) {
var data = JSON.parse(response);
var data = parseJSONorNot(response);
var nodata = data.s === 'no_data';
@@ -390,7 +399,7 @@ Datafeeds.UDFCompatibleDatafeed.prototype.calculateHistoryDepth = function(perio
Datafeeds.UDFCompatibleDatafeed.prototype.getQuotes = function(symbols, onDataCallback, onErrorCallback) {
this._send(this._datafeedURL + '/quotes', { symbols: symbols })
.done(function(response) {
var data = JSON.parse(response);
var data = parseJSONorNot(response);
if (data.s === 'ok') {
// JSON format is {s: "status", [{s: "symbol_status", n: "symbol_name", v: {"field1": "value1", "field2": "value2", ..., "fieldN": "valueN"}}]}
if (onDataCallback) {
@@ -458,7 +467,7 @@ Datafeeds.SymbolsStorage.prototype._requestFullSymbolsList = function() {
})
.done((function(exchange) {
return function(response) {
that._onExchangeDataReceived(exchange, JSON.parse(response));
that._onExchangeDataReceived(exchange, parseJSONorNot(response));
that._onAnyExchangeResponseReceived(exchange);
};
})(exchange))