@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.
@@ -1203,6 +1203,8 @@ var AbracadabraWS = class extends EventEmitter {
1203
1203
  this.identifier = 0;
1204
1204
  this.intervals = { connectionChecker: null };
1205
1205
  this.connectionAttempt = null;
1206
+ this.onlineListener = null;
1207
+ this.offlineListener = null;
1206
1208
  this.receivedOnOpenPayload = void 0;
1207
1209
  this.closeTries = 0;
1208
1210
  this.setConfiguration(configuration);
@@ -1221,8 +1223,39 @@ var AbracadabraWS = class extends EventEmitter {
1221
1223
  this.on("close", this.onClose.bind(this));
1222
1224
  this.on("message", this.onMessage.bind(this));
1223
1225
  this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
1226
+ if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
1227
+ this.onlineListener = this.handleOnline.bind(this);
1228
+ this.offlineListener = this.handleOffline.bind(this);
1229
+ window.addEventListener("online", this.onlineListener);
1230
+ window.addEventListener("offline", this.offlineListener);
1231
+ }
1224
1232
  if (this.shouldConnect) this.connect();
1225
1233
  }
1234
+ /**
1235
+ * Whether the device currently believes it has network connectivity.
1236
+ *
1237
+ * Treats "unknown" as online: in Node and other non-browser environments
1238
+ * `navigator` (or `navigator.onLine`) is absent, and we must not gate
1239
+ * reconnection there — only the browser exposes a trustworthy signal.
1240
+ */
1241
+ get isOnline() {
1242
+ return typeof navigator === "undefined" || navigator.onLine !== false;
1243
+ }
1244
+ handleOnline() {
1245
+ if (this.shouldConnect && this.status !== WebSocketStatus.Connected) this.connect();
1246
+ }
1247
+ handleOffline() {
1248
+ if (this.cancelWebsocketRetry) {
1249
+ this.cancelWebsocketRetry();
1250
+ this.cancelWebsocketRetry = void 0;
1251
+ }
1252
+ try {
1253
+ this.webSocket?.close();
1254
+ this.messageQueue = [];
1255
+ } catch (e) {
1256
+ console.error(e);
1257
+ }
1258
+ }
1226
1259
  async onOpen(event) {
1227
1260
  this.status = WebSocketStatus.Connected;
1228
1261
  this.emit("status", { status: WebSocketStatus.Connected });
@@ -1252,6 +1285,10 @@ var AbracadabraWS = class extends EventEmitter {
1252
1285
  }
1253
1286
  async connect() {
1254
1287
  if (this.status === WebSocketStatus.Connected) return;
1288
+ if (!this.isOnline) {
1289
+ this.shouldConnect = true;
1290
+ return;
1291
+ }
1255
1292
  if (this.cancelWebsocketRetry) {
1256
1293
  this.cancelWebsocketRetry();
1257
1294
  this.cancelWebsocketRetry = void 0;
@@ -1416,7 +1453,7 @@ var AbracadabraWS = class extends EventEmitter {
1416
1453
  const isRateLimited = event?.code === 4429 || event === 4429;
1417
1454
  this.emit("disconnect", { event });
1418
1455
  if (isRateLimited) this.emit("rateLimited");
1419
- if (!this.cancelWebsocketRetry && this.shouldConnect) {
1456
+ if (!this.cancelWebsocketRetry && this.shouldConnect && this.isOnline) {
1420
1457
  const delay = isRateLimited ? 6e4 : this.configuration.delay;
1421
1458
  setTimeout(() => {
1422
1459
  this.connect();
@@ -1426,6 +1463,12 @@ var AbracadabraWS = class extends EventEmitter {
1426
1463
  destroy() {
1427
1464
  this.shouldConnect = false;
1428
1465
  this.emit("destroy");
1466
+ if (typeof window !== "undefined" && typeof window.removeEventListener === "function") {
1467
+ if (this.onlineListener) window.removeEventListener("online", this.onlineListener);
1468
+ if (this.offlineListener) window.removeEventListener("offline", this.offlineListener);
1469
+ }
1470
+ this.onlineListener = null;
1471
+ this.offlineListener = null;
1429
1472
  clearInterval(this.intervals.connectionChecker);
1430
1473
  this.stopConnectionAttempt();
1431
1474
  this.disconnect();