data-pulse-provider.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { getErrorMessage, logMessage, } from './helpers';
  2. var DataPulseProvider = /** @class */ (function () {
  3. function DataPulseProvider(historyProvider, updateFrequency) {
  4. this._subscribers = {};
  5. this._requestsPending = 0;
  6. this._historyProvider = historyProvider;
  7. setInterval(this._updateData.bind(this), updateFrequency);
  8. }
  9. DataPulseProvider.prototype.subscribeBars = function (symbolInfo, resolution, newDataCallback, listenerGuid) {
  10. if (this._subscribers.hasOwnProperty(listenerGuid)) {
  11. logMessage("DataPulseProvider: already has subscriber with id=" + listenerGuid);
  12. return;
  13. }
  14. this._subscribers[listenerGuid] = {
  15. lastBarTime: null,
  16. listener: newDataCallback,
  17. resolution: resolution,
  18. symbolInfo: symbolInfo,
  19. };
  20. logMessage("DataPulseProvider: subscribed for #" + listenerGuid + " - {" + symbolInfo.name + ", " + resolution + "}");
  21. };
  22. DataPulseProvider.prototype.unsubscribeBars = function (listenerGuid) {
  23. delete this._subscribers[listenerGuid];
  24. logMessage("DataPulseProvider: unsubscribed for #" + listenerGuid);
  25. };
  26. DataPulseProvider.prototype._updateData = function () {
  27. var _this = this;
  28. if (this._requestsPending > 0) {
  29. return;
  30. }
  31. this._requestsPending = 0;
  32. var _loop_1 = function (listenerGuid) {
  33. this_1._requestsPending += 1;
  34. this_1._updateDataForSubscriber(listenerGuid)
  35. .then(function () {
  36. _this._requestsPending -= 1;
  37. logMessage("DataPulseProvider: data for #" + listenerGuid + " updated successfully, pending=" + _this._requestsPending);
  38. })
  39. .catch(function (reason) {
  40. _this._requestsPending -= 1;
  41. logMessage("DataPulseProvider: data for #" + listenerGuid + " updated with error=" + getErrorMessage(reason) + ", pending=" + _this._requestsPending);
  42. });
  43. };
  44. var this_1 = this;
  45. for (var listenerGuid in this._subscribers) {
  46. _loop_1(listenerGuid);
  47. }
  48. };
  49. DataPulseProvider.prototype._updateDataForSubscriber = function (listenerGuid) {
  50. var _this = this;
  51. var subscriptionRecord = this._subscribers[listenerGuid];
  52. var rangeEndTime = parseInt((Date.now() / 1000).toString());
  53. // BEWARE: please note we really need 2 bars, not the only last one
  54. // see the explanation below. `10` is the `large enough` value to work around holidays
  55. var rangeStartTime = rangeEndTime - periodLengthSeconds(subscriptionRecord.resolution, 10);
  56. return this._historyProvider.getBars(subscriptionRecord.symbolInfo, subscriptionRecord.resolution, rangeStartTime, rangeEndTime)
  57. .then(function (result) {
  58. _this._onSubscriberDataReceived(listenerGuid, result);
  59. });
  60. };
  61. DataPulseProvider.prototype._onSubscriberDataReceived = function (listenerGuid, result) {
  62. // means the subscription was cancelled while waiting for data
  63. if (!this._subscribers.hasOwnProperty(listenerGuid)) {
  64. logMessage("DataPulseProvider: Data comes for already unsubscribed subscription #" + listenerGuid);
  65. return;
  66. }
  67. var bars = result.bars;
  68. if (bars.length === 0) {
  69. return;
  70. }
  71. var lastBar = bars[bars.length - 1];
  72. var subscriptionRecord = this._subscribers[listenerGuid];
  73. if (subscriptionRecord.lastBarTime !== null && lastBar.time < subscriptionRecord.lastBarTime) {
  74. return;
  75. }
  76. var isNewBar = subscriptionRecord.lastBarTime !== null && lastBar.time > subscriptionRecord.lastBarTime;
  77. // Pulse updating may miss some trades data (ie, if pulse period = 10 secods and new bar is started 5 seconds later after the last update, the
  78. // old bar's last 5 seconds trades will be lost). Thus, at fist we should broadcast old bar updates when it's ready.
  79. if (isNewBar) {
  80. if (bars.length < 2) {
  81. throw new Error('Not enough bars in history for proper pulse update. Need at least 2.');
  82. }
  83. var previousBar = bars[bars.length - 2];
  84. subscriptionRecord.listener(previousBar);
  85. }
  86. subscriptionRecord.lastBarTime = lastBar.time;
  87. subscriptionRecord.listener(lastBar);
  88. };
  89. return DataPulseProvider;
  90. }());
  91. export { DataPulseProvider };
  92. function periodLengthSeconds(resolution, requiredPeriodsCount) {
  93. var daysCount = 0;
  94. if (resolution === 'D' || resolution === '1D') {
  95. daysCount = requiredPeriodsCount;
  96. }
  97. else if (resolution === 'M' || resolution === '1M') {
  98. daysCount = 31 * requiredPeriodsCount;
  99. }
  100. else if (resolution === 'W' || resolution === '1W') {
  101. daysCount = 7 * requiredPeriodsCount;
  102. }
  103. else {
  104. daysCount = requiredPeriodsCount * parseInt(resolution) / (24 * 60);
  105. }
  106. return daysCount * 24 * 60 * 60;
  107. }