@fairfox/polly 0.27.2 → 0.27.4

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/dist/src/mesh.js CHANGED
@@ -1161,6 +1161,9 @@ function deserialiseMessage(bytes) {
1161
1161
  }
1162
1162
 
1163
1163
  // src/shared/lib/mesh-signaling-client.ts
1164
+ var RECONNECT_BASE_DELAY_MS = 250;
1165
+ var RECONNECT_MAX_DELAY_MS = 30000;
1166
+
1164
1167
  class MeshSignalingClient {
1165
1168
  url;
1166
1169
  peerId;
@@ -1173,6 +1176,8 @@ class MeshSignalingClient {
1173
1176
  onPeerLeft;
1174
1177
  socket;
1175
1178
  joined = false;
1179
+ stopping = false;
1180
+ reconnectTimer;
1176
1181
  WebSocketCtor;
1177
1182
  constructor(options) {
1178
1183
  this.url = options.url;
@@ -1197,27 +1202,56 @@ class MeshSignalingClient {
1197
1202
  this.WebSocketCtor = WS;
1198
1203
  }
1199
1204
  async connect() {
1205
+ this.stopping = false;
1206
+ if (this.reconnectTimer) {
1207
+ clearTimeout(this.reconnectTimer);
1208
+ this.reconnectTimer = undefined;
1209
+ }
1200
1210
  return new Promise((resolve, reject) => {
1201
1211
  const ws = new this.WebSocketCtor(this.url);
1202
1212
  this.socket = ws;
1213
+ let settled = false;
1203
1214
  ws.addEventListener("open", () => {
1204
1215
  ws.send(JSON.stringify({ type: "join", peerId: this.peerId }));
1205
1216
  this.joined = true;
1206
1217
  this.onOpen?.();
1207
- resolve();
1218
+ if (!settled) {
1219
+ settled = true;
1220
+ resolve();
1221
+ }
1208
1222
  });
1209
1223
  ws.addEventListener("message", (event) => {
1210
1224
  this.dispatchFrame(event.data);
1211
1225
  });
1212
1226
  ws.addEventListener("error", (err) => {
1213
- reject(err);
1227
+ if (!settled) {
1228
+ settled = true;
1229
+ reject(err);
1230
+ }
1214
1231
  });
1215
1232
  ws.addEventListener("close", () => {
1233
+ const wasOpen = this.joined;
1216
1234
  this.joined = false;
1217
1235
  this.onClose?.();
1236
+ if (!this.stopping && wasOpen) {
1237
+ this.scheduleReconnect(0);
1238
+ }
1218
1239
  });
1219
1240
  });
1220
1241
  }
1242
+ scheduleReconnect(attempt) {
1243
+ if (this.stopping)
1244
+ return;
1245
+ const delay = Math.min(RECONNECT_MAX_DELAY_MS, RECONNECT_BASE_DELAY_MS * 2 ** attempt);
1246
+ this.reconnectTimer = setTimeout(() => {
1247
+ this.reconnectTimer = undefined;
1248
+ if (this.stopping)
1249
+ return;
1250
+ this.connect().catch(() => {
1251
+ this.scheduleReconnect(attempt + 1);
1252
+ });
1253
+ }, delay);
1254
+ }
1221
1255
  dispatchFrame(raw) {
1222
1256
  let msg;
1223
1257
  try {
@@ -1264,6 +1298,11 @@ class MeshSignalingClient {
1264
1298
  return true;
1265
1299
  }
1266
1300
  close() {
1301
+ this.stopping = true;
1302
+ if (this.reconnectTimer) {
1303
+ clearTimeout(this.reconnectTimer);
1304
+ this.reconnectTimer = undefined;
1305
+ }
1267
1306
  this.socket?.close();
1268
1307
  this.socket = undefined;
1269
1308
  this.joined = false;
@@ -2508,4 +2547,4 @@ export {
2508
2547
  $meshCounter
2509
2548
  };
2510
2549
 
2511
- //# debugId=9F3B517D50E6C60564756E2164756E21
2550
+ //# debugId=64A1C8232384625664756E2164756E21