@clonegod/ttd-core 2.0.78 → 2.0.80

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.
@@ -14,6 +14,7 @@ export declare class WebSocketClient {
14
14
  private onMessageCallback;
15
15
  private onOpenCallback;
16
16
  private keepAliveTimer;
17
+ private isReconnecting;
17
18
  constructor(url: string, options?: WebSocketClientOptions);
18
19
  connect(): void;
19
20
  private reconnect;
@@ -24,5 +25,5 @@ export declare class WebSocketClient {
24
25
  isConnected(): boolean;
25
26
  private startKeepAlive;
26
27
  private stopKeepAlive;
27
- private setupGlobalErrorHandlers;
28
+ private forceCleanup;
28
29
  }
@@ -21,9 +21,9 @@ class WebSocketClient {
21
21
  this.onMessageCallback = null;
22
22
  this.onOpenCallback = null;
23
23
  this.keepAliveTimer = null;
24
+ this.isReconnecting = false;
24
25
  this.url = url;
25
26
  this.options = Object.assign({ reconnectInterval: 3000, rejectUnauthorized: false, keepAlive: false, keepAliveInterval: 30000 }, options);
26
- this.setupGlobalErrorHandlers();
27
27
  (0, dist_1.log_info)(`WebSocketClient constructor, url: ${this.url}`, this.options);
28
28
  }
29
29
  connect() {
@@ -32,6 +32,7 @@ class WebSocketClient {
32
32
  this.ws = new ws_1.default(this.url, { headers: this.options.headers });
33
33
  this.ws.on('open', () => {
34
34
  (0, dist_1.log_info)('ws connected');
35
+ this.isReconnecting = false;
35
36
  this.startKeepAlive();
36
37
  if (this.onOpenCallback)
37
38
  this.onOpenCallback();
@@ -62,9 +63,13 @@ class WebSocketClient {
62
63
  }
63
64
  reconnect() {
64
65
  return __awaiter(this, void 0, void 0, function* () {
65
- (0, dist_1.log_warn)(`Reconnecting in ${this.options.reconnectInterval}ms`);
66
+ if (this.isReconnecting) {
67
+ return;
68
+ }
69
+ this.isReconnecting = true;
70
+ (0, dist_1.log_info)(`Reconnecting in ${this.options.reconnectInterval}ms`);
66
71
  yield (0, dist_1.sleep)(this.options.reconnectInterval);
67
- this.disconnect();
72
+ this.forceCleanup();
68
73
  this.connect();
69
74
  });
70
75
  }
@@ -86,10 +91,11 @@ class WebSocketClient {
86
91
  }
87
92
  disconnect() {
88
93
  this.stopKeepAlive();
94
+ this.isReconnecting = false;
89
95
  if (this.ws) {
90
96
  this.ws.removeAllListeners();
91
97
  try {
92
- if (this.ws.readyState === ws_1.default.OPEN) {
98
+ if (this.ws.readyState === ws_1.default.OPEN || this.ws.readyState === ws_1.default.CONNECTING) {
93
99
  this.ws.close();
94
100
  }
95
101
  }
@@ -123,13 +129,24 @@ class WebSocketClient {
123
129
  (0, dist_1.log_info)('KeepAlive stopped');
124
130
  }
125
131
  }
126
- setupGlobalErrorHandlers() {
127
- process.on('uncaughtException', (error) => {
128
- (0, dist_1.log_error)('Uncaught Exception in WebSocketClient:', error);
129
- });
130
- process.on('unhandledRejection', (reason, promise) => {
131
- (0, dist_1.log_error)('Unhandled Rejection in WebSocketClient:', reason instanceof Error ? reason : new Error(String(reason)));
132
- });
132
+ forceCleanup() {
133
+ this.stopKeepAlive();
134
+ this.isReconnecting = false;
135
+ if (this.ws) {
136
+ this.ws.removeAllListeners('open');
137
+ this.ws.removeAllListeners('message');
138
+ this.ws.removeAllListeners('close');
139
+ this.ws.removeAllListeners('error');
140
+ this.ws.removeAllListeners();
141
+ try {
142
+ if (this.ws.readyState !== ws_1.default.CLOSED) {
143
+ this.ws.close();
144
+ }
145
+ }
146
+ catch (error) {
147
+ }
148
+ this.ws = null;
149
+ }
133
150
  }
134
151
  }
135
152
  exports.WebSocketClient = WebSocketClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "2.0.78",
3
+ "version": "2.0.80",
4
4
  "description": "Common types and utilities for trading systems - use `npm run push` to publish",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",