Fixes tradingview/charting_library#60 Fixes tradingview/charting_library#65 Fixes tradingview/charting_library#70 Fixes tradingview/charting_library#71 Fixes tradingview/charting_library#75 Fixes tradingview/charting_library#76 Fixes tradingview/charting_library#78 Fixes tradingview/charting_library#79 Fixes tradingview/charting_library#81 Fixes tradingview/charting_library#82 Fixes tradingview/charting_library#84 Fixes tradingview/charting_library#86 Fixes tradingview/charting_library#89 Fixes tradingview/charting_library#90 Fixes tradingview/charting_library#91 Fixes tradingview/charting_library#92 Fixes tradingview/charting_library#94 Fixes tradingview/charting_library#99 Fixes tradingview/charting_library#100 Fixes tradingview/charting_library#101 Fixes tradingview/charting_library#102 Fixes tradingview/charting_library#103 Fixes tradingview/charting_library#1995 Fixes tradingview/charting_library#5726 Fixes tradingview/charting_library#6025 Fixes tradingview/charting_library#6406 Fixes tradingview/charting_library#6636 Fixes tradingview/charting_library#6767 Fixes tradingview/charting_library#6775 Fixes tradingview/charting_library#6783 Fixes tradingview/charting_library#6864 Fixes tradingview/charting_library#6926 Fixes tradingview/charting_library#7060 Fixes tradingview/charting_library#7169 Fixes tradingview/charting_library#7307
63 lines
2.7 KiB
JavaScript
63 lines
2.7 KiB
JavaScript
import { getErrorMessage, logMessage, } from './helpers';
|
|
export class QuotesPulseProvider {
|
|
constructor(quotesProvider) {
|
|
this._subscribers = {};
|
|
this._requestsPending = 0;
|
|
this._timers = null;
|
|
this._quotesProvider = quotesProvider;
|
|
}
|
|
subscribeQuotes(symbols, fastSymbols, onRealtimeCallback, listenerGuid) {
|
|
this._subscribers[listenerGuid] = {
|
|
symbols: symbols,
|
|
fastSymbols: fastSymbols,
|
|
listener: onRealtimeCallback,
|
|
};
|
|
this._createTimersIfRequired();
|
|
logMessage(`QuotesPulseProvider: subscribed quotes with #${listenerGuid}`);
|
|
}
|
|
unsubscribeQuotes(listenerGuid) {
|
|
delete this._subscribers[listenerGuid];
|
|
if (Object.keys(this._subscribers).length === 0) {
|
|
this._destroyTimers();
|
|
}
|
|
logMessage(`QuotesPulseProvider: unsubscribed quotes with #${listenerGuid}`);
|
|
}
|
|
_createTimersIfRequired() {
|
|
if (this._timers === null) {
|
|
const fastTimer = setInterval(this._updateQuotes.bind(this, 1 /* SymbolsType.Fast */), 10000 /* UpdateTimeouts.Fast */);
|
|
const generalTimer = setInterval(this._updateQuotes.bind(this, 0 /* SymbolsType.General */), 60000 /* UpdateTimeouts.General */);
|
|
this._timers = { fastTimer, generalTimer };
|
|
}
|
|
}
|
|
_destroyTimers() {
|
|
if (this._timers !== null) {
|
|
clearInterval(this._timers.fastTimer);
|
|
clearInterval(this._timers.generalTimer);
|
|
this._timers = null;
|
|
}
|
|
}
|
|
_updateQuotes(updateType) {
|
|
if (this._requestsPending > 0) {
|
|
return;
|
|
}
|
|
// eslint-disable-next-line guard-for-in
|
|
for (const listenerGuid in this._subscribers) {
|
|
this._requestsPending++;
|
|
const subscriptionRecord = this._subscribers[listenerGuid];
|
|
this._quotesProvider.getQuotes(updateType === 1 /* SymbolsType.Fast */ ? subscriptionRecord.fastSymbols : subscriptionRecord.symbols)
|
|
.then((data) => {
|
|
this._requestsPending--;
|
|
if (!this._subscribers.hasOwnProperty(listenerGuid)) {
|
|
return;
|
|
}
|
|
subscriptionRecord.listener(data);
|
|
logMessage(`QuotesPulseProvider: data for #${listenerGuid} (${updateType}) updated successfully, pending=${this._requestsPending}`);
|
|
})
|
|
.catch((reason) => {
|
|
this._requestsPending--;
|
|
logMessage(`QuotesPulseProvider: data for #${listenerGuid} (${updateType}) updated with error=${getErrorMessage(reason)}, pending=${this._requestsPending}`);
|
|
});
|
|
}
|
|
}
|
|
}
|