market-data-tradingview-ws 2.1.1 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -35,6 +35,7 @@ module.exports = class Client {
35
35
  frame: data.frame,
36
36
  i: data.i,
37
37
  deleted: data.deleted === true,
38
+ broken: data.broken === true,
38
39
  limit: Number(data.limit || 1500),
39
40
  requestedLimit: Number(data.requestedLimit || data.limit || 1500),
40
41
  historyPending: data.historyPending === true,
@@ -56,28 +57,40 @@ module.exports = class Client {
56
57
  data.end = null;
57
58
  return data;
58
59
  },
60
+ break({ key }) {
61
+ const data = this.values[key];
62
+ if (!data) return null;
63
+
64
+ data.deleted = true;
65
+ data.broken = true;
66
+ data.historyPending = false;
67
+ data.historyCompletionsPending = 0;
68
+ data.historyBars = {};
69
+ data.seriesCreated = false;
70
+ data.end = null;
71
+
72
+ return data;
73
+ },
59
74
  get({ key }) {
60
75
  return this.values[key];
61
76
  },
62
77
  getDeleted() {
63
78
  for (const key of Object.keys(this.values)) {
64
79
  const item = this.values[key];
65
-
66
- if (item?.deleted) {
80
+ if (item?.deleted && item?.broken !== true) {
67
81
  return { key, data: item };
68
82
  }
69
83
  }
70
-
71
84
  return null;
72
85
  },
73
86
  claimDeleted({ key }) {
74
- if (!this.values[key]) return null;
75
- this.values[key].deleted = false;
76
- return this.values[key];
87
+ const data = this.values[key];
88
+ if (!data || data.broken === true) return null;
89
+ data.deleted = false;
90
+ return data;
77
91
  },
78
92
  getBySymbolPeriod({ symbol, period }) {
79
93
  for (let key of Object.keys(this.values)) {
80
- // console.warn(symbol, period, key, this.values[key], this.values[key].symbol === symbol, this.values[key].period === period);
81
94
  if (this.values[key].symbol === symbol && parseInt(this.values[key].period) === parseInt(period) && !this.values[key].deleted) {
82
95
  return key;
83
96
  }
@@ -126,6 +139,9 @@ module.exports = class Client {
126
139
  #connectPromise = null;
127
140
  #connectTimeoutMs = 20_000;
128
141
 
142
+ #chartQueuePaused = false;
143
+ #chartQueuePauseReason = null;
144
+
129
145
  #stopWatchdog() {
130
146
  if (!this.#watchdogTimer) return;
131
147
  clearInterval(this.#watchdogTimer);
@@ -203,6 +219,8 @@ module.exports = class Client {
203
219
  this.#symbols.values = {};
204
220
  this.#symbols.max = 1;
205
221
  this.#maxChartSessions = this.#configuredMaxChartSessions;
222
+ this.#chartQueuePaused = false;
223
+ this.#chartQueuePauseReason = null;
206
224
  this.#chartFrameMax = 0;
207
225
  this.#chartQueue = [];
208
226
  this.#queuedChartKeys.clear();
@@ -294,12 +312,11 @@ module.exports = class Client {
294
312
  }
295
313
 
296
314
  #scheduleQueue(delay = 0) {
297
- if (!this.#isOpen() || this.#queueTimer) return;
298
- this.#queueTimer = setTimeout(() => {
315
+ if (this.#chartQueuePaused || !this.#isOpen() || this.#queueTimer) return;
316
+ this.#queueTimer = setTimeout(() => {
299
317
  this.#queueTimer = null;
300
318
  void this.#processChartQueue();
301
319
  }, Math.max(0, delay));
302
-
303
320
  this.#queueTimer.unref?.();
304
321
  }
305
322
 
@@ -319,19 +336,13 @@ module.exports = class Client {
319
336
  if (!socket || socket.readyState !== WebSocket.OPEN) return false;
320
337
 
321
338
  if (socket.bufferedAmount > this.#maxBufferedAmount) {
322
- this.#emit('error', {
323
- type: 'ws_backpressure',
324
- bufferedAmount: socket.bufferedAmount,
325
- });
339
+ this.#emit('error', { type: 'ws_backpressure', bufferedAmount: socket.bufferedAmount });
326
340
  return false;
327
341
  }
328
342
 
329
343
  socket.send(data, (error) => {
330
344
  if (!error) return;
331
- this.#emit('error', {
332
- type: 'ws_send_error',
333
- message: error.message,
334
- });
345
+ this.#emit('error', { type: 'ws_send_error', message: error.message });
335
346
  });
336
347
 
337
348
  return true;
@@ -402,9 +413,9 @@ module.exports = class Client {
402
413
  }
403
414
 
404
415
  async #processChartQueue() {
405
- if (this.#queueProcessing || !this.#isOpen()) return;
406
- const generation = this.#connectGeneration;
407
- this.#queueProcessing = true;
416
+ if (this.#chartQueuePaused || this.#queueProcessing || !this.#isOpen()) return;
417
+ const generation = this.#connectGeneration;
418
+ this.#queueProcessing = true;
408
419
  try {
409
420
  const wait = this.#queueBlockedUntil - Date.now();
410
421
  if (wait > 0) {
@@ -413,7 +424,7 @@ module.exports = class Client {
413
424
  }
414
425
 
415
426
  while (this.#chartQueue.length > 0) {
416
- if (generation !== this.#connectGeneration || !this.#isOpen()) return;
427
+ if (this.#chartQueuePaused || generation !== this.#connectGeneration || !this.#isOpen()) return;
417
428
  const active = this.#countActiveCharts();
418
429
  if (active >= this.#maxChartSessions) {
419
430
  this.#logQueueState('queue_paused_capacity');
@@ -443,11 +454,32 @@ module.exports = class Client {
443
454
  } finally {
444
455
  if (generation === this.#connectGeneration) {
445
456
  this.#queueProcessing = false;
446
- if (this.#chartQueue.length > 0 && this.#isOpen()) this.#scheduleQueue(this.#queueRetryMs);
457
+ if (!this.#chartQueuePaused && this.#chartQueue.length > 0 && this.#isOpen()) this.#scheduleQueue(this.#queueRetryMs);
447
458
  }
448
459
  }
449
460
  }
450
461
 
462
+ pauseChartQueue({ reason = 'manual', extra = {} } = {}) {
463
+ this.#chartQueuePaused = true;
464
+ this.#chartQueuePauseReason = reason;
465
+ this.#stopQueueTimer();
466
+ this.#logQueueState('queue_paused', { reason, ...extra });
467
+ return this.getDebugState();
468
+ }
469
+
470
+ resumeChartQueue({ reason = 'manual', extra = {} } = {}) {
471
+ const previousReason = this.#chartQueuePauseReason;
472
+ this.#chartQueuePaused = false;
473
+ this.#chartQueuePauseReason = null;
474
+ this.#logQueueState('queue_resumed', { reason, previousReason, ...extra });
475
+ if (this.#chartQueue.length > 0) this.#scheduleQueue(0);
476
+ return this.getDebugState();
477
+ }
478
+
479
+ logQueueState(context = '', extra = {}) {
480
+ this.#logQueueState(context, extra);
481
+ }
482
+
451
483
  async close() {
452
484
  this.#intentionalClose = true;
453
485
  this.#connectGeneration++;
@@ -520,7 +552,10 @@ module.exports = class Client {
520
552
  tokenLen: this.#token ? this.#token.length : 0,
521
553
  sessionid: Boolean(this.#sessionid),
522
554
  sessionidsign: Boolean(this.#sessionidsign),
555
+ chartQueuePaused: this.#chartQueuePaused,
556
+ chartQueuePauseReason: this.#chartQueuePauseReason,
523
557
  chartsCount: charts.length,
558
+ brokenChartSessions: charts.filter((chart) => chart?.broken === true).length,
524
559
  activeChartSessions: charts.filter((chart) => chart && chart.deleted !== true).length,
525
560
  deletedChartSessions: charts.filter((chart) => chart?.deleted === true).length,
526
561
  quotesCount: Object.keys(this.#quotes.values).length,
@@ -770,9 +805,12 @@ module.exports = class Client {
770
805
  const normalized = symbol.trim();
771
806
  const key = this.#quotes.getBySymbol({ symbol: normalized });
772
807
  if (!key) return 'unlisted';
773
- this.#quotes.delete({ key, symbol: normalized });
808
+
774
809
  const sent = this.#send(tv.quoteRemoveSymbols({ session: key, symbol: normalized }));
775
- return sent ? 'delete' : 'not_connected';
810
+ if (!sent) return 'not_connected';
811
+
812
+ this.#quotes.delete({ key, symbol: normalized });
813
+ return 'delete';
776
814
  }
777
815
 
778
816
  #reuseDeletedChartSession({ key, symbol, period, limit }) {
@@ -943,11 +981,7 @@ module.exports = class Client {
943
981
  return { status: 'not_connected' };
944
982
  }
945
983
 
946
- this.#charts.set({
947
- key,
948
- data,
949
- });
950
-
984
+ this.#charts.set({ key, data });
951
985
  return { status: 'update', symbol: validation.symbol, period: validation.period, limit: validation.limit };
952
986
  }
953
987
 
@@ -1081,7 +1115,8 @@ module.exports = class Client {
1081
1115
  const period = parseInt(session.period);
1082
1116
  const message = detail.slice(2).map((value) => typeof value === 'string' ? value : JSON.stringify(value)).join(' ') || 'TradingView series error';
1083
1117
 
1084
- this.#charts.delete({ key: chartSessionKey });
1118
+ this.#charts.break({ key: chartSessionKey });
1119
+ this.#logQueueState('series_error_session_quarantined', { session: chartSessionKey, frame: errorFrame, symbol, period });
1085
1120
  this.#stopQueueTimer();
1086
1121
  this.#scheduleQueue(0);
1087
1122
  this.#emit('error', { type: 'chart_error', code: 'series_error', symbol, period, message, detail, retryable: false });
@@ -256,18 +256,16 @@ class TradingView {
256
256
  });
257
257
  }
258
258
 
259
+ quoteSymbol({ symbol }) {
260
+ return symbol.split(':')[0] === 'BINANCE' ? symbol : '={"adjustment":"splits","currency-id":"USD","symbol":"' + symbol + '"}';
261
+ }
262
+
259
263
  quoteAddSymbol({ session, symbol }) {
260
- const symbolString =
261
- symbol.split(':')[0] === 'BINANCE' ? symbol : '={"adjustment":"splits","currency-id":"USD","symbol":"' + symbol + '"}';
262
- // console.log('quoteAddSymbol', session, symbol, symbolString);
263
- // return this.formatWSPacket({ packet: { m: 'quote_add_symbols', p: [session, symbol] } });
264
- return this.formatWSPacket({
265
- packet: { m: 'quote_add_symbols', p: [session, symbolString] },
266
- });
264
+ return this.formatWSPacket({ packet: { m: 'quote_add_symbols', p: [session, this.quoteSymbol({ symbol })] }});
267
265
  }
268
266
 
269
267
  quoteRemoveSymbols({ session, symbol }) {
270
- return this.formatWSPacket({ packet: { m: 'quote_remove_symbols', p: [session, symbol] } });
268
+ return this.formatWSPacket({ packet: { m: 'quote_remove_symbols', p: [session, this.quoteSymbol({ symbol })] }});
271
269
  }
272
270
 
273
271
  quoteRemoveSeries({ session }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "market-data-tradingview-ws",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "marketData from Tradingview ws",
5
5
  "main": "index.js",
6
6
  "scripts": {