@abraca/dabra 2.8.0 → 2.9.0
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.
|
@@ -1233,6 +1233,8 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1233
1233
|
this.identifier = 0;
|
|
1234
1234
|
this.intervals = { connectionChecker: null };
|
|
1235
1235
|
this.connectionAttempt = null;
|
|
1236
|
+
this.onlineListener = null;
|
|
1237
|
+
this.offlineListener = null;
|
|
1236
1238
|
this.receivedOnOpenPayload = void 0;
|
|
1237
1239
|
this.closeTries = 0;
|
|
1238
1240
|
this.setConfiguration(configuration);
|
|
@@ -1251,8 +1253,39 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1251
1253
|
this.on("close", this.onClose.bind(this));
|
|
1252
1254
|
this.on("message", this.onMessage.bind(this));
|
|
1253
1255
|
this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
|
|
1256
|
+
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
|
|
1257
|
+
this.onlineListener = this.handleOnline.bind(this);
|
|
1258
|
+
this.offlineListener = this.handleOffline.bind(this);
|
|
1259
|
+
window.addEventListener("online", this.onlineListener);
|
|
1260
|
+
window.addEventListener("offline", this.offlineListener);
|
|
1261
|
+
}
|
|
1254
1262
|
if (this.shouldConnect) this.connect();
|
|
1255
1263
|
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Whether the device currently believes it has network connectivity.
|
|
1266
|
+
*
|
|
1267
|
+
* Treats "unknown" as online: in Node and other non-browser environments
|
|
1268
|
+
* `navigator` (or `navigator.onLine`) is absent, and we must not gate
|
|
1269
|
+
* reconnection there — only the browser exposes a trustworthy signal.
|
|
1270
|
+
*/
|
|
1271
|
+
get isOnline() {
|
|
1272
|
+
return typeof navigator === "undefined" || navigator.onLine !== false;
|
|
1273
|
+
}
|
|
1274
|
+
handleOnline() {
|
|
1275
|
+
if (this.shouldConnect && this.status !== WebSocketStatus.Connected) this.connect();
|
|
1276
|
+
}
|
|
1277
|
+
handleOffline() {
|
|
1278
|
+
if (this.cancelWebsocketRetry) {
|
|
1279
|
+
this.cancelWebsocketRetry();
|
|
1280
|
+
this.cancelWebsocketRetry = void 0;
|
|
1281
|
+
}
|
|
1282
|
+
try {
|
|
1283
|
+
this.webSocket?.close();
|
|
1284
|
+
this.messageQueue = [];
|
|
1285
|
+
} catch (e) {
|
|
1286
|
+
console.error(e);
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1256
1289
|
async onOpen(event) {
|
|
1257
1290
|
this.status = WebSocketStatus.Connected;
|
|
1258
1291
|
this.emit("status", { status: WebSocketStatus.Connected });
|
|
@@ -1282,6 +1315,10 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1282
1315
|
}
|
|
1283
1316
|
async connect() {
|
|
1284
1317
|
if (this.status === WebSocketStatus.Connected) return;
|
|
1318
|
+
if (!this.isOnline) {
|
|
1319
|
+
this.shouldConnect = true;
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1285
1322
|
if (this.cancelWebsocketRetry) {
|
|
1286
1323
|
this.cancelWebsocketRetry();
|
|
1287
1324
|
this.cancelWebsocketRetry = void 0;
|
|
@@ -1446,7 +1483,7 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1446
1483
|
const isRateLimited = event?.code === 4429 || event === 4429;
|
|
1447
1484
|
this.emit("disconnect", { event });
|
|
1448
1485
|
if (isRateLimited) this.emit("rateLimited");
|
|
1449
|
-
if (!this.cancelWebsocketRetry && this.shouldConnect) {
|
|
1486
|
+
if (!this.cancelWebsocketRetry && this.shouldConnect && this.isOnline) {
|
|
1450
1487
|
const delay = isRateLimited ? 6e4 : this.configuration.delay;
|
|
1451
1488
|
setTimeout(() => {
|
|
1452
1489
|
this.connect();
|
|
@@ -1456,6 +1493,12 @@ var AbracadabraWS = class extends EventEmitter {
|
|
|
1456
1493
|
destroy() {
|
|
1457
1494
|
this.shouldConnect = false;
|
|
1458
1495
|
this.emit("destroy");
|
|
1496
|
+
if (typeof window !== "undefined" && typeof window.removeEventListener === "function") {
|
|
1497
|
+
if (this.onlineListener) window.removeEventListener("online", this.onlineListener);
|
|
1498
|
+
if (this.offlineListener) window.removeEventListener("offline", this.offlineListener);
|
|
1499
|
+
}
|
|
1500
|
+
this.onlineListener = null;
|
|
1501
|
+
this.offlineListener = null;
|
|
1459
1502
|
clearInterval(this.intervals.connectionChecker);
|
|
1460
1503
|
this.stopConnectionAttempt();
|
|
1461
1504
|
this.disconnect();
|