quidproquo-web 0.0.259 → 0.0.261

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.
@@ -17,6 +17,9 @@ export declare class WebsocketService {
17
17
  private eventListeners;
18
18
  private isDestroyed;
19
19
  private pendingMessages;
20
+ private reconnectAttempts;
21
+ private static readonly BASE_RECONNECT_MS;
22
+ private static readonly MAX_RECONNECT_MS;
20
23
  private subscriptions;
21
24
  constructor(url: string);
22
25
  destroy(): void;
@@ -14,6 +14,7 @@ class WebsocketService {
14
14
  this.eventListeners = {};
15
15
  this.isDestroyed = false;
16
16
  this.pendingMessages = [];
17
+ this.reconnectAttempts = 0;
17
18
  this.subscriptions = {
18
19
  [WebsocketServiceEvent.OPEN]: new Map(),
19
20
  [WebsocketServiceEvent.CLOSE]: new Map(),
@@ -106,13 +107,20 @@ class WebsocketService {
106
107
  this.eventListeners = {};
107
108
  }
108
109
  reconnectIfNotDestroyed() {
110
+ const baseDelay = Math.min(WebsocketService.BASE_RECONNECT_MS * Math.pow(2, this.reconnectAttempts), WebsocketService.MAX_RECONNECT_MS);
111
+ // Add jitter: ±25% of base delay to prevent thundering herd
112
+ const jitter = baseDelay * 0.25 * (Math.random() * 2 - 1);
113
+ const delay = Math.max(0, baseDelay + jitter);
114
+ this.reconnectAttempts++;
115
+ console.log("Delay: ", delay);
109
116
  setTimeout(() => {
110
117
  if (!this.isDestroyed) {
111
118
  this.connect();
112
119
  }
113
- }, 1000);
120
+ }, delay);
114
121
  }
115
122
  onConnect() {
123
+ this.reconnectAttempts = 0;
116
124
  this.notifySubscribers(WebsocketServiceEvent.OPEN);
117
125
  const messages = this.pendingMessages;
118
126
  this.pendingMessages = [];
@@ -150,3 +158,5 @@ class WebsocketService {
150
158
  }
151
159
  }
152
160
  exports.WebsocketService = WebsocketService;
161
+ WebsocketService.BASE_RECONNECT_MS = 1000;
162
+ WebsocketService.MAX_RECONNECT_MS = 60000;
@@ -17,6 +17,9 @@ export declare class WebsocketService {
17
17
  private eventListeners;
18
18
  private isDestroyed;
19
19
  private pendingMessages;
20
+ private reconnectAttempts;
21
+ private static readonly BASE_RECONNECT_MS;
22
+ private static readonly MAX_RECONNECT_MS;
20
23
  private subscriptions;
21
24
  constructor(url: string);
22
25
  destroy(): void;
@@ -11,6 +11,9 @@ export class WebsocketService {
11
11
  eventListeners = {};
12
12
  isDestroyed = false;
13
13
  pendingMessages = [];
14
+ reconnectAttempts = 0;
15
+ static BASE_RECONNECT_MS = 1000;
16
+ static MAX_RECONNECT_MS = 60_000;
14
17
  subscriptions = {
15
18
  [WebsocketServiceEvent.OPEN]: new Map(),
16
19
  [WebsocketServiceEvent.CLOSE]: new Map(),
@@ -101,13 +104,20 @@ export class WebsocketService {
101
104
  this.eventListeners = {};
102
105
  }
103
106
  reconnectIfNotDestroyed() {
107
+ const baseDelay = Math.min(WebsocketService.BASE_RECONNECT_MS * Math.pow(2, this.reconnectAttempts), WebsocketService.MAX_RECONNECT_MS);
108
+ // Add jitter: ±25% of base delay to prevent thundering herd
109
+ const jitter = baseDelay * 0.25 * (Math.random() * 2 - 1);
110
+ const delay = Math.max(0, baseDelay + jitter);
111
+ this.reconnectAttempts++;
112
+ console.log("Delay: ", delay);
104
113
  setTimeout(() => {
105
114
  if (!this.isDestroyed) {
106
115
  this.connect();
107
116
  }
108
- }, 1000);
117
+ }, delay);
109
118
  }
110
119
  onConnect() {
120
+ this.reconnectAttempts = 0;
111
121
  this.notifySubscribers(WebsocketServiceEvent.OPEN);
112
122
  const messages = this.pendingMessages;
113
123
  this.pendingMessages = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quidproquo-web",
3
- "version": "0.0.259",
3
+ "version": "0.0.261",
4
4
  "description": "",
5
5
  "main": "./lib/commonjs/index.js",
6
6
  "module": "./lib/esm/index.js",
@@ -32,9 +32,9 @@
32
32
  },
33
33
  "homepage": "https://github.com/joe-coady/quidproquo#readme",
34
34
  "devDependencies": {
35
- "quidproquo-core": "0.0.259",
36
- "quidproquo-tsconfig": "0.0.259",
37
- "quidproquo-webserver": "0.0.259",
35
+ "quidproquo-core": "0.0.261",
36
+ "quidproquo-tsconfig": "0.0.261",
37
+ "quidproquo-webserver": "0.0.261",
38
38
  "typescript": "^5.8.2"
39
39
  }
40
40
  }