market-data-tradingview-ws 2.1.2 → 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.
Files changed (2) hide show
  1. package/lib/client.js +59 -20
  2. package/package.json +1 -1
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,24 +57,37 @@ 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)) {
@@ -125,6 +139,9 @@ module.exports = class Client {
125
139
  #connectPromise = null;
126
140
  #connectTimeoutMs = 20_000;
127
141
 
142
+ #chartQueuePaused = false;
143
+ #chartQueuePauseReason = null;
144
+
128
145
  #stopWatchdog() {
129
146
  if (!this.#watchdogTimer) return;
130
147
  clearInterval(this.#watchdogTimer);
@@ -202,6 +219,8 @@ module.exports = class Client {
202
219
  this.#symbols.values = {};
203
220
  this.#symbols.max = 1;
204
221
  this.#maxChartSessions = this.#configuredMaxChartSessions;
222
+ this.#chartQueuePaused = false;
223
+ this.#chartQueuePauseReason = null;
205
224
  this.#chartFrameMax = 0;
206
225
  this.#chartQueue = [];
207
226
  this.#queuedChartKeys.clear();
@@ -293,12 +312,11 @@ module.exports = class Client {
293
312
  }
294
313
 
295
314
  #scheduleQueue(delay = 0) {
296
- if (!this.#isOpen() || this.#queueTimer) return;
297
- this.#queueTimer = setTimeout(() => {
315
+ if (this.#chartQueuePaused || !this.#isOpen() || this.#queueTimer) return;
316
+ this.#queueTimer = setTimeout(() => {
298
317
  this.#queueTimer = null;
299
318
  void this.#processChartQueue();
300
319
  }, Math.max(0, delay));
301
-
302
320
  this.#queueTimer.unref?.();
303
321
  }
304
322
 
@@ -395,9 +413,9 @@ module.exports = class Client {
395
413
  }
396
414
 
397
415
  async #processChartQueue() {
398
- if (this.#queueProcessing || !this.#isOpen()) return;
399
- const generation = this.#connectGeneration;
400
- this.#queueProcessing = true;
416
+ if (this.#chartQueuePaused || this.#queueProcessing || !this.#isOpen()) return;
417
+ const generation = this.#connectGeneration;
418
+ this.#queueProcessing = true;
401
419
  try {
402
420
  const wait = this.#queueBlockedUntil - Date.now();
403
421
  if (wait > 0) {
@@ -406,7 +424,7 @@ module.exports = class Client {
406
424
  }
407
425
 
408
426
  while (this.#chartQueue.length > 0) {
409
- if (generation !== this.#connectGeneration || !this.#isOpen()) return;
427
+ if (this.#chartQueuePaused || generation !== this.#connectGeneration || !this.#isOpen()) return;
410
428
  const active = this.#countActiveCharts();
411
429
  if (active >= this.#maxChartSessions) {
412
430
  this.#logQueueState('queue_paused_capacity');
@@ -436,11 +454,32 @@ module.exports = class Client {
436
454
  } finally {
437
455
  if (generation === this.#connectGeneration) {
438
456
  this.#queueProcessing = false;
439
- 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);
440
458
  }
441
459
  }
442
460
  }
443
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
+
444
483
  async close() {
445
484
  this.#intentionalClose = true;
446
485
  this.#connectGeneration++;
@@ -513,7 +552,10 @@ module.exports = class Client {
513
552
  tokenLen: this.#token ? this.#token.length : 0,
514
553
  sessionid: Boolean(this.#sessionid),
515
554
  sessionidsign: Boolean(this.#sessionidsign),
555
+ chartQueuePaused: this.#chartQueuePaused,
556
+ chartQueuePauseReason: this.#chartQueuePauseReason,
516
557
  chartsCount: charts.length,
558
+ brokenChartSessions: charts.filter((chart) => chart?.broken === true).length,
517
559
  activeChartSessions: charts.filter((chart) => chart && chart.deleted !== true).length,
518
560
  deletedChartSessions: charts.filter((chart) => chart?.deleted === true).length,
519
561
  quotesCount: Object.keys(this.#quotes.values).length,
@@ -939,11 +981,7 @@ module.exports = class Client {
939
981
  return { status: 'not_connected' };
940
982
  }
941
983
 
942
- this.#charts.set({
943
- key,
944
- data,
945
- });
946
-
984
+ this.#charts.set({ key, data });
947
985
  return { status: 'update', symbol: validation.symbol, period: validation.period, limit: validation.limit };
948
986
  }
949
987
 
@@ -1077,7 +1115,8 @@ module.exports = class Client {
1077
1115
  const period = parseInt(session.period);
1078
1116
  const message = detail.slice(2).map((value) => typeof value === 'string' ? value : JSON.stringify(value)).join(' ') || 'TradingView series error';
1079
1117
 
1080
- this.#charts.delete({ key: chartSessionKey });
1118
+ this.#charts.break({ key: chartSessionKey });
1119
+ this.#logQueueState('series_error_session_quarantined', { session: chartSessionKey, frame: errorFrame, symbol, period });
1081
1120
  this.#stopQueueTimer();
1082
1121
  this.#scheduleQueue(0);
1083
1122
  this.#emit('error', { type: 'chart_error', code: 'series_error', symbol, period, message, detail, retryable: false });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "market-data-tradingview-ws",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "marketData from Tradingview ws",
5
5
  "main": "index.js",
6
6
  "scripts": {