@danidoble/webserial 4.4.1 → 4.4.2

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.
@@ -10,7 +10,7 @@ const S = /* @__PURE__ */ Object.create(null);
10
10
  Object.keys(m).forEach((n) => {
11
11
  S[m[n]] = n;
12
12
  });
13
- const D = { type: "error", data: "parser error" }, te = typeof Blob == "function" || typeof Blob < "u" && Object.prototype.toString.call(Blob) === "[object BlobConstructor]", se = typeof ArrayBuffer == "function", ne = (n) => typeof ArrayBuffer.isView == "function" ? ArrayBuffer.isView(n) : n && n.buffer instanceof ArrayBuffer, H = ({ type: n, data: e }, t, s) => te && e instanceof Blob ? t ? s(e) : J(e, s) : se && (e instanceof ArrayBuffer || ne(e)) ? t ? s(e) : J(new Blob([e]), s) : s(m[n] + (e || "")), J = (n, e) => {
13
+ const q = { type: "error", data: "parser error" }, te = typeof Blob == "function" || typeof Blob < "u" && Object.prototype.toString.call(Blob) === "[object BlobConstructor]", se = typeof ArrayBuffer == "function", ne = (n) => typeof ArrayBuffer.isView == "function" ? ArrayBuffer.isView(n) : n && n.buffer instanceof ArrayBuffer, H = ({ type: n, data: e }, t, s) => te && e instanceof Blob ? t ? s(e) : J(e, s) : se && (e instanceof ArrayBuffer || ne(e)) ? t ? s(e) : J(new Blob([e]), s) : s(m[n] + (e || "")), J = (n, e) => {
14
14
  const t = new FileReader();
15
15
  return t.onload = function() {
16
16
  const s = t.result.split(",")[1];
@@ -21,7 +21,7 @@ function Q(n) {
21
21
  return n instanceof Uint8Array ? n : n instanceof ArrayBuffer ? new Uint8Array(n) : new Uint8Array(n.buffer, n.byteOffset, n.byteLength);
22
22
  }
23
23
  let P;
24
- function de(n, e) {
24
+ function pe(n, e) {
25
25
  if (te && n.data instanceof Blob)
26
26
  return n.data.arrayBuffer().then(Q).then(e);
27
27
  if (se && (n.data instanceof ArrayBuffer || ne(n.data)))
@@ -30,15 +30,15 @@ function de(n, e) {
30
30
  P || (P = new TextEncoder()), e(P.encode(t));
31
31
  });
32
32
  }
33
- const X = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", k = typeof Uint8Array > "u" ? [] : new Uint8Array(256);
33
+ const X = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", C = typeof Uint8Array > "u" ? [] : new Uint8Array(256);
34
34
  for (let n = 0; n < X.length; n++)
35
- k[X.charCodeAt(n)] = n;
35
+ C[X.charCodeAt(n)] = n;
36
36
  const ye = (n) => {
37
37
  let e = n.length * 0.75, t = n.length, s, i = 0, r, o, l, c;
38
38
  n[n.length - 1] === "=" && (e--, n[n.length - 2] === "=" && e--);
39
39
  const f = new ArrayBuffer(e), u = new Uint8Array(f);
40
40
  for (s = 0; s < t; s += 4)
41
- r = k[n.charCodeAt(s)], o = k[n.charCodeAt(s + 1)], l = k[n.charCodeAt(s + 2)], c = k[n.charCodeAt(s + 3)], u[i++] = r << 2 | o >> 4, u[i++] = (o & 15) << 4 | l >> 2, u[i++] = (l & 3) << 6 | c & 63;
41
+ r = C[n.charCodeAt(s)], o = C[n.charCodeAt(s + 1)], l = C[n.charCodeAt(s + 2)], c = C[n.charCodeAt(s + 3)], u[i++] = r << 2 | o >> 4, u[i++] = (o & 15) << 4 | l >> 2, u[i++] = (l & 3) << 6 | c & 63;
42
42
  return f;
43
43
  }, ge = typeof ArrayBuffer == "function", W = (n, e) => {
44
44
  if (typeof n != "string")
@@ -55,7 +55,7 @@ const ye = (n) => {
55
55
  data: n.substring(1)
56
56
  } : {
57
57
  type: S[t]
58
- } : D;
58
+ } : q;
59
59
  }, me = (n, e) => {
60
60
  if (ge) {
61
61
  const t = ye(n);
@@ -70,7 +70,7 @@ const ye = (n) => {
70
70
  default:
71
71
  return n instanceof ArrayBuffer ? n : n.buffer;
72
72
  }
73
- }, re = "", be = (n, e) => {
73
+ }, re = "", we = (n, e) => {
74
74
  const t = n.length, s = new Array(t);
75
75
  let i = 0;
76
76
  n.forEach((r, o) => {
@@ -78,7 +78,7 @@ const ye = (n) => {
78
78
  s[o] = l, ++i === t && e(s.join(re));
79
79
  });
80
80
  });
81
- }, we = (n, e) => {
81
+ }, be = (n, e) => {
82
82
  const t = n.split(re), s = [];
83
83
  for (let i = 0; i < t.length; i++) {
84
84
  const r = W(t[i], e);
@@ -90,7 +90,7 @@ const ye = (n) => {
90
90
  function ve() {
91
91
  return new TransformStream({
92
92
  transform(n, e) {
93
- de(n, (t) => {
93
+ pe(n, (t) => {
94
94
  const s = t.length;
95
95
  let i;
96
96
  if (s < 126)
@@ -110,10 +110,10 @@ function ve() {
110
110
  });
111
111
  }
112
112
  let N;
113
- function C(n) {
113
+ function T(n) {
114
114
  return n.reduce((e, t) => e + t.length, 0);
115
115
  }
116
- function T(n, e) {
116
+ function A(n, e) {
117
117
  if (n[0].length === e)
118
118
  return n.shift();
119
119
  const t = new Uint8Array(e);
@@ -130,32 +130,32 @@ function Ee(n, e) {
130
130
  transform(o, l) {
131
131
  for (t.push(o); ; ) {
132
132
  if (s === 0) {
133
- if (C(t) < 1)
133
+ if (T(t) < 1)
134
134
  break;
135
- const c = T(t, 1);
135
+ const c = A(t, 1);
136
136
  r = (c[0] & 128) === 128, i = c[0] & 127, i < 126 ? s = 3 : i === 126 ? s = 1 : s = 2;
137
137
  } else if (s === 1) {
138
- if (C(t) < 2)
138
+ if (T(t) < 2)
139
139
  break;
140
- const c = T(t, 2);
140
+ const c = A(t, 2);
141
141
  i = new DataView(c.buffer, c.byteOffset, c.length).getUint16(0), s = 3;
142
142
  } else if (s === 2) {
143
- if (C(t) < 8)
143
+ if (T(t) < 8)
144
144
  break;
145
- const c = T(t, 8), f = new DataView(c.buffer, c.byteOffset, c.length), u = f.getUint32(0);
145
+ const c = A(t, 8), f = new DataView(c.buffer, c.byteOffset, c.length), u = f.getUint32(0);
146
146
  if (u > Math.pow(2, 21) - 1) {
147
- l.enqueue(D);
147
+ l.enqueue(q);
148
148
  break;
149
149
  }
150
150
  i = u * Math.pow(2, 32) + f.getUint32(4), s = 3;
151
151
  } else {
152
- if (C(t) < i)
152
+ if (T(t) < i)
153
153
  break;
154
- const c = T(t, i);
154
+ const c = A(t, i);
155
155
  l.enqueue(W(r ? c : N.decode(c), e)), s = 0;
156
156
  }
157
157
  if (i === 0 || i > n) {
158
- l.enqueue(D);
158
+ l.enqueue(q);
159
159
  break;
160
160
  }
161
161
  }
@@ -212,13 +212,13 @@ _.prototype.listeners = function(n) {
212
212
  _.prototype.hasListeners = function(n) {
213
213
  return !!this.listeners(n).length;
214
214
  };
215
- const O = typeof Promise == "function" && typeof Promise.resolve == "function" ? (e) => Promise.resolve().then(e) : (e, t) => t(e, 0), d = typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), Ce = "arraybuffer";
215
+ const O = typeof Promise == "function" && typeof Promise.resolve == "function" ? (e) => Promise.resolve().then(e) : (e, t) => t(e, 0), p = typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), Ce = "arraybuffer";
216
216
  function ae(n, ...e) {
217
217
  return e.reduce((t, s) => (n.hasOwnProperty(s) && (t[s] = n[s]), t), {});
218
218
  }
219
- const Te = d.setTimeout, Ae = d.clearTimeout;
219
+ const Te = p.setTimeout, Ae = p.clearTimeout;
220
220
  function L(n, e) {
221
- e.useNativeTimers ? (n.setTimeoutFn = Te.bind(d), n.clearTimeoutFn = Ae.bind(d)) : (n.setTimeoutFn = d.setTimeout.bind(d), n.clearTimeoutFn = d.clearTimeout.bind(d));
221
+ e.useNativeTimers ? (n.setTimeoutFn = Te.bind(p), n.clearTimeoutFn = Ae.bind(p)) : (n.setTimeoutFn = p.setTimeout.bind(p), n.clearTimeoutFn = p.clearTimeout.bind(p));
222
222
  }
223
223
  const Se = 1.33;
224
224
  function xe(n) {
@@ -406,7 +406,7 @@ class Pe extends K {
406
406
  return this.onClose({ description: "transport closed by the server" }), !1;
407
407
  this.onPacket(s);
408
408
  };
409
- we(e, this.socket.binaryType).forEach(t), this.readyState !== "closed" && (this._polling = !1, this.emitReserved("pollComplete"), this.readyState === "open" && this._poll());
409
+ be(e, this.socket.binaryType).forEach(t), this.readyState !== "closed" && (this._polling = !1, this.emitReserved("pollComplete"), this.readyState === "open" && this._poll());
410
410
  }
411
411
  /**
412
412
  * For polling, send a close packet.
@@ -426,7 +426,7 @@ class Pe extends K {
426
426
  * @protected
427
427
  */
428
428
  write(e) {
429
- this.writable = !1, be(e, (t) => {
429
+ this.writable = !1, we(e, (t) => {
430
430
  this.doWrite(t, () => {
431
431
  this.writable = !0, this.emitReserved("drain");
432
432
  });
@@ -450,7 +450,7 @@ try {
450
450
  const Ne = le;
451
451
  function Ie() {
452
452
  }
453
- class qe extends Pe {
453
+ class De extends Pe {
454
454
  /**
455
455
  * XHR Polling constructor.
456
456
  *
@@ -595,7 +595,7 @@ if (typeof document < "u") {
595
595
  if (typeof attachEvent == "function")
596
596
  attachEvent("onunload", j);
597
597
  else if (typeof addEventListener == "function") {
598
- const n = "onpagehide" in d ? "pagehide" : "unload";
598
+ const n = "onpagehide" in p ? "pagehide" : "unload";
599
599
  addEventListener(n, j, !1);
600
600
  }
601
601
  }
@@ -603,17 +603,17 @@ function j() {
603
603
  for (let n in g.requests)
604
604
  g.requests.hasOwnProperty(n) && g.requests[n].abort();
605
605
  }
606
- const De = (function() {
606
+ const qe = (function() {
607
607
  const n = he({
608
608
  xdomain: !1
609
609
  });
610
610
  return n && n.responseType !== null;
611
611
  })();
612
- class Ue extends qe {
612
+ class Ue extends De {
613
613
  constructor(e) {
614
614
  super(e);
615
615
  const t = e && e.forceBase64;
616
- this.supportsBinary = De && !t;
616
+ this.supportsBinary = qe && !t;
617
617
  }
618
618
  request(e = {}) {
619
619
  return Object.assign(e, { xd: this.xd }, this.opts), new g(he, this.uri(), e);
@@ -628,7 +628,7 @@ function he(n) {
628
628
  }
629
629
  if (!e)
630
630
  try {
631
- return new d[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
631
+ return new p[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
632
632
  } catch {
633
633
  }
634
634
  }
@@ -689,7 +689,7 @@ class Me extends K {
689
689
  return this.opts.timestampRequests && (t[this.opts.timestampParam] = ce()), this.supportsBinary || (t.b64 = 1), this.createUri(e, t);
690
690
  }
691
691
  }
692
- const I = d.WebSocket || d.MozWebSocket;
692
+ const I = p.WebSocket || p.MozWebSocket;
693
693
  class Fe extends Me {
694
694
  createSocket(e, t, s) {
695
695
  return ue ? new I(e, t, s) : t ? new I(e, t) : new I(e);
@@ -1078,26 +1078,26 @@ class Ye extends b {
1078
1078
  let t = this.createTransport(e), s = !1;
1079
1079
  b.priorWebsocketSuccess = !1;
1080
1080
  const i = () => {
1081
- s || (t.send([{ type: "ping", data: "probe" }]), t.once("packet", (p) => {
1081
+ s || (t.send([{ type: "ping", data: "probe" }]), t.once("packet", (d) => {
1082
1082
  if (!s)
1083
- if (p.type === "pong" && p.data === "probe") {
1083
+ if (d.type === "pong" && d.data === "probe") {
1084
1084
  if (this.upgrading = !0, this.emitReserved("upgrading", t), !t)
1085
1085
  return;
1086
1086
  b.priorWebsocketSuccess = t.name === "websocket", this.transport.pause(() => {
1087
1087
  s || this.readyState !== "closed" && (u(), this.setTransport(t), t.send([{ type: "upgrade" }]), this.emitReserved("upgrade", t), t = null, this.upgrading = !1, this.flush());
1088
1088
  });
1089
1089
  } else {
1090
- const v = new Error("probe error");
1091
- v.transport = t.name, this.emitReserved("upgradeError", v);
1090
+ const E = new Error("probe error");
1091
+ E.transport = t.name, this.emitReserved("upgradeError", E);
1092
1092
  }
1093
1093
  }));
1094
1094
  };
1095
1095
  function r() {
1096
1096
  s || (s = !0, u(), t.close(), t = null);
1097
1097
  }
1098
- const o = (p) => {
1099
- const v = new Error("probe error: " + p);
1100
- v.transport = t.name, r(), this.emitReserved("upgradeError", v);
1098
+ const o = (d) => {
1099
+ const E = new Error("probe error: " + d);
1100
+ E.transport = t.name, r(), this.emitReserved("upgradeError", E);
1101
1101
  };
1102
1102
  function l() {
1103
1103
  o("transport closed");
@@ -1105,8 +1105,8 @@ class Ye extends b {
1105
1105
  function c() {
1106
1106
  o("socket closed");
1107
1107
  }
1108
- function f(p) {
1109
- t && p.name !== t.name && r();
1108
+ function f(d) {
1109
+ t && d.name !== t.name && r();
1110
1110
  }
1111
1111
  const u = () => {
1112
1112
  t.removeListener("open", i), t.removeListener("error", o), t.removeListener("close", l), this.off("close", c), this.off("upgrading", f);
@@ -1548,8 +1548,8 @@ class fe extends _ {
1548
1548
  data: t
1549
1549
  };
1550
1550
  if (o.options = {}, o.options.compress = this.flags.compress !== !1, typeof t[t.length - 1] == "function") {
1551
- const u = this.ids++, p = t.pop();
1552
- this._registerAckCallback(u, p), o.id = u;
1551
+ const u = this.ids++, d = t.pop();
1552
+ this._registerAckCallback(u, d), o.id = u;
1553
1553
  }
1554
1554
  const l = (i = (s = this.io.engine) === null || s === void 0 ? void 0 : s.transport) === null || i === void 0 ? void 0 : i.writable, c = this.connected && !(!((r = this.io.engine) === null || r === void 0) && r._hasPingExpired());
1555
1555
  return this.flags.volatile && !l || (c ? (this.notifyOutgoingListeners(o), this.packet(o)) : this.sendBuffer.push(o)), this.flags = {}, this;
@@ -2015,10 +2015,10 @@ class fe extends _ {
2015
2015
  }
2016
2016
  }
2017
2017
  }
2018
- function w(n) {
2018
+ function v(n) {
2019
2019
  n = n || {}, this.ms = n.min || 100, this.max = n.max || 1e4, this.factor = n.factor || 2, this.jitter = n.jitter > 0 && n.jitter <= 1 ? n.jitter : 0, this.attempts = 0;
2020
2020
  }
2021
- w.prototype.duration = function() {
2021
+ v.prototype.duration = function() {
2022
2022
  var n = this.ms * Math.pow(this.factor, this.attempts++);
2023
2023
  if (this.jitter) {
2024
2024
  var e = Math.random(), t = Math.floor(e * this.jitter * n);
@@ -2026,22 +2026,22 @@ w.prototype.duration = function() {
2026
2026
  }
2027
2027
  return Math.min(n, this.max) | 0;
2028
2028
  };
2029
- w.prototype.reset = function() {
2029
+ v.prototype.reset = function() {
2030
2030
  this.attempts = 0;
2031
2031
  };
2032
- w.prototype.setMin = function(n) {
2032
+ v.prototype.setMin = function(n) {
2033
2033
  this.ms = n;
2034
2034
  };
2035
- w.prototype.setMax = function(n) {
2035
+ v.prototype.setMax = function(n) {
2036
2036
  this.max = n;
2037
2037
  };
2038
- w.prototype.setJitter = function(n) {
2038
+ v.prototype.setJitter = function(n) {
2039
2039
  this.jitter = n;
2040
2040
  };
2041
2041
  class V extends _ {
2042
2042
  constructor(e, t) {
2043
2043
  var s;
2044
- super(), this.nsps = {}, this.subs = [], e && typeof e == "object" && (t = e, e = void 0), t = t || {}, t.path = t.path || "/socket.io", this.opts = t, L(this, t), this.reconnection(t.reconnection !== !1), this.reconnectionAttempts(t.reconnectionAttempts || 1 / 0), this.reconnectionDelay(t.reconnectionDelay || 1e3), this.reconnectionDelayMax(t.reconnectionDelayMax || 5e3), this.randomizationFactor((s = t.randomizationFactor) !== null && s !== void 0 ? s : 0.5), this.backoff = new w({
2044
+ super(), this.nsps = {}, this.subs = [], e && typeof e == "object" && (t = e, e = void 0), t = t || {}, t.path = t.path || "/socket.io", this.opts = t, L(this, t), this.reconnection(t.reconnection !== !1), this.reconnectionAttempts(t.reconnectionAttempts || 1 / 0), this.reconnectionDelay(t.reconnectionDelay || 1e3), this.reconnectionDelayMax(t.reconnectionDelayMax || 5e3), this.randomizationFactor((s = t.randomizationFactor) !== null && s !== void 0 ? s : 0.5), this.backoff = new v({
2045
2045
  min: this.reconnectionDelay(),
2046
2046
  max: this.reconnectionDelayMax(),
2047
2047
  jitter: this.randomizationFactor()
@@ -2276,12 +2276,12 @@ class V extends _ {
2276
2276
  this._reconnecting = !1, this.backoff.reset(), this.emitReserved("reconnect", e);
2277
2277
  }
2278
2278
  }
2279
- const E = {};
2279
+ const k = {};
2280
2280
  function B(n, e) {
2281
2281
  typeof n == "object" && (e = n, n = void 0), e = e || {};
2282
- const t = Qe(n, e.path || "/socket.io"), s = t.source, i = t.id, r = t.path, o = E[i] && r in E[i].nsps, l = e.forceNew || e["force new connection"] || e.multiplex === !1 || o;
2282
+ const t = Qe(n, e.path || "/socket.io"), s = t.source, i = t.id, r = t.path, o = k[i] && r in k[i].nsps, l = e.forceNew || e["force new connection"] || e.multiplex === !1 || o;
2283
2283
  let c;
2284
- return l ? c = new V(s, e) : (E[i] || (E[i] = new V(s, e)), c = E[i]), t.query && !e.query && (e.query = t.queryKey), c.socket(t.path, e);
2284
+ return l ? c = new V(s, e) : (k[i] || (k[i] = new V(s, e)), c = k[i]), t.query && !e.query && (e.query = t.queryKey), c.socket(t.path, e);
2285
2285
  }
2286
2286
  Object.assign(B, {
2287
2287
  Manager: V,
@@ -2294,37 +2294,100 @@ class Z extends CustomEvent {
2294
2294
  super(e, t);
2295
2295
  }
2296
2296
  }
2297
- class pe extends EventTarget {
2297
+ class de extends EventTarget {
2298
2298
  __listeners__ = {
2299
2299
  debug: !1
2300
2300
  };
2301
2301
  __debug__ = !1;
2302
2302
  __listenersCallbacks__ = [];
2303
+ /**
2304
+ * Dispatches an event with the specified type and data
2305
+ * @param type - The event type to dispatch
2306
+ * @param data - Optional data to attach to the event
2307
+ * @example
2308
+ * ```typescript
2309
+ * dispatcher.dispatch('connected', { port: 'COM3' });
2310
+ * ```
2311
+ */
2303
2312
  dispatch(e, t = null) {
2304
2313
  const s = new Z(e, { detail: t });
2305
2314
  this.dispatchEvent(s), this.__debug__ && this.dispatchEvent(new Z("debug", { detail: { type: e, data: t } }));
2306
2315
  }
2316
+ /**
2317
+ * Dispatches an event asynchronously after a specified delay
2318
+ * @param type - The event type to dispatch
2319
+ * @param data - Optional data to attach to the event
2320
+ * @param ms - Delay in milliseconds (default: 100)
2321
+ * @example
2322
+ * ```typescript
2323
+ * dispatcher.dispatchAsync('timeout', { reason: 'no response' }, 500);
2324
+ * ```
2325
+ */
2307
2326
  dispatchAsync(e, t = null, s = 100) {
2308
2327
  const i = this;
2309
2328
  setTimeout(() => {
2310
2329
  i.dispatch(e, t);
2311
2330
  }, s);
2312
2331
  }
2332
+ /**
2333
+ * Registers an event listener for the specified event type
2334
+ * @param type - The event type to listen to
2335
+ * @param callback - The callback function to execute when the event is triggered
2336
+ * @example
2337
+ * ```typescript
2338
+ * dispatcher.on('connected', (event) => {
2339
+ * console.log('Device connected', event.detail);
2340
+ * });
2341
+ * ```
2342
+ */
2313
2343
  on(e, t) {
2314
2344
  typeof this.__listeners__[e] < "u" && !this.__listeners__[e] && (this.__listeners__[e] = !0), this.__listenersCallbacks__.push({ key: e, callback: t }), this.addEventListener(e, t);
2315
2345
  }
2346
+ /**
2347
+ * Removes an event listener for the specified event type
2348
+ * @param type - The event type to stop listening to
2349
+ * @param callback - The callback function to remove
2350
+ * @example
2351
+ * ```typescript
2352
+ * const handler = (event) => console.log(event.detail);
2353
+ * dispatcher.on('data', handler);
2354
+ * dispatcher.off('data', handler);
2355
+ * ```
2356
+ */
2316
2357
  off(e, t) {
2317
2358
  this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((s) => !(s.key === e && s.callback === t)), this.removeEventListener(e, t);
2318
2359
  }
2360
+ /**
2361
+ * Registers an available listener type for tracking
2362
+ * @param type - The event type to register
2363
+ * @internal
2364
+ */
2319
2365
  serialRegisterAvailableListener(e) {
2320
2366
  this.__listeners__[e] || (this.__listeners__[e] = !1);
2321
2367
  }
2368
+ /**
2369
+ * Gets the list of all available listeners and their state
2370
+ * @returns Array of listener objects with type and listening status
2371
+ * @example
2372
+ * ```typescript
2373
+ * const listeners = dispatcher.availableListeners;
2374
+ * console.log(listeners); // [{ type: 'connected', listening: true }, ...]
2375
+ * ```
2376
+ */
2322
2377
  get availableListeners() {
2323
2378
  return Object.keys(this.__listeners__).sort().map((e) => ({
2324
2379
  type: e,
2325
2380
  listening: this.__listeners__[e]
2326
2381
  }));
2327
2382
  }
2383
+ /**
2384
+ * Removes all event listeners except internal ones (like queue listeners)
2385
+ * Resets all listener states to false
2386
+ * @example
2387
+ * ```typescript
2388
+ * dispatcher.removeAllListeners();
2389
+ * ```
2390
+ */
2328
2391
  removeAllListeners() {
2329
2392
  for (const e of this.__listenersCallbacks__)
2330
2393
  ["internal:queue"].includes(e.key) || (this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((t) => !(t.key === e.key && t.callback === e.callback)), this.removeEventListener(e.key, e.callback));
@@ -2332,7 +2395,7 @@ class pe extends EventTarget {
2332
2395
  this.__listeners__[e] = !1;
2333
2396
  }
2334
2397
  }
2335
- class a extends pe {
2398
+ class a extends de {
2336
2399
  static instance;
2337
2400
  static devices = {};
2338
2401
  constructor() {
@@ -2347,9 +2410,25 @@ class a extends pe {
2347
2410
  const t = new Error();
2348
2411
  throw t.message = `Type ${e} is not supported`, t.name = "DeviceTypeError", t;
2349
2412
  }
2413
+ /**
2414
+ * Registers a new device type in the registry
2415
+ * @param type - The type name of the device (e.g., 'arduino', 'esp32')
2416
+ * @internal
2417
+ */
2350
2418
  static registerType(e) {
2351
2419
  typeof a.devices[e] > "u" && (a.devices = { ...a.devices, [e]: {} });
2352
2420
  }
2421
+ /**
2422
+ * Adds a device to the registry
2423
+ * @param device - The Core device instance to add
2424
+ * @returns The index of the device in its type registry
2425
+ * @throws {Error} If device with the same ID already exists
2426
+ * @example
2427
+ * ```typescript
2428
+ * const arduino = new Arduino();
2429
+ * Devices.add(arduino);
2430
+ * ```
2431
+ */
2353
2432
  static add(e) {
2354
2433
  const t = e.typeDevice;
2355
2434
  typeof a.devices[t] > "u" && a.registerType(t);
@@ -2358,6 +2437,17 @@ class a extends pe {
2358
2437
  throw new Error(`Device with id ${s} already exists`);
2359
2438
  return a.devices[t][s] = e, a.$dispatchChange(e), Object.keys(a.devices[t]).indexOf(s);
2360
2439
  }
2440
+ /**
2441
+ * Gets a specific device by type and UUID
2442
+ * @param type - The device type
2443
+ * @param id - The device UUID
2444
+ * @returns The device instance
2445
+ * @throws {Error} If the device type is not supported
2446
+ * @example
2447
+ * ```typescript
2448
+ * const device = Devices.get('arduino', 'uuid-123');
2449
+ * ```
2450
+ */
2361
2451
  static get(e, t) {
2362
2452
  return typeof a.devices[e] > "u" && a.registerType(e), typeof a.devices[e] > "u" && a.typeError(e), a.devices[e][t];
2363
2453
  }
@@ -2413,57 +2503,86 @@ function ee(n = 100) {
2413
2503
  );
2414
2504
  }
2415
2505
  class ct {
2416
- #t = "http://localhost:3000";
2417
- #s = {
2506
+ #s = "http://localhost:3000";
2507
+ #n = {
2418
2508
  transports: ["websocket"]
2419
2509
  };
2420
- #e;
2421
- #r = !1;
2422
- #o = {};
2510
+ #e = null;
2511
+ #i = !1;
2512
+ #a = !1;
2513
+ #t;
2514
+ constructor() {
2515
+ this.#t = {
2516
+ onResponse: this.onResponse.bind(this),
2517
+ onDisconnect: () => {
2518
+ this.#i = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
2519
+ },
2520
+ onConnect: () => {
2521
+ this.#i = !0, window.dispatchEvent(new Event("serial:socket:connected"));
2522
+ },
2523
+ onConnectError: (e) => {
2524
+ console.debug("Socket connection error", e), this.#i = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
2525
+ }
2526
+ };
2527
+ }
2423
2528
  set uri(e) {
2424
2529
  const t = new URL(e);
2425
2530
  if (!["http:", "https:", "ws:", "wss:"].includes(t.protocol))
2426
2531
  throw new Error("URI must start with http://, https://, ws://, or wss://");
2427
- this.#t = e;
2532
+ this.#s = e;
2428
2533
  }
2429
2534
  get uri() {
2430
- return this.#t;
2535
+ return this.#s;
2431
2536
  }
2432
2537
  set options(e) {
2433
2538
  if (typeof e != "object")
2434
2539
  throw new Error("Options must be an object");
2435
- this.#s = e;
2540
+ this.#n = e;
2436
2541
  }
2437
2542
  get options() {
2438
- return this.#s;
2543
+ return this.#n;
2439
2544
  }
2440
- constructor() {
2441
- this.#o.onResponse = this.onResponse.bind(this);
2545
+ get socketId() {
2546
+ return this.#e && this.#e.id ? this.#e.id : null;
2442
2547
  }
2443
2548
  disconnect() {
2444
- this.#e && (this.#e.off("response", this.#o.onResponse), this.#e.disconnect(), this.#e = null), this.#r = !1;
2549
+ this.#e && (this.#e.off("response", this.#t.onResponse), this.#e.off("disconnect", this.#t.onDisconnect), this.#e.off("connect", this.#t.onConnect), this.#e.off("connect_error", this.#t.onConnectError), this.#e.disconnect(), this.#e = null, this.#a = !1), this.#i = !1;
2445
2550
  }
2446
2551
  prepare() {
2447
- this.#r || (this.#e = B(this.#t, this.#s), this.#r = !0, this.#e.on("response", this.#o.onResponse));
2552
+ this.#i || this.#a || (this.#e = B(this.#s, this.#n), this.#a = !0, this.#e.on("disconnect", this.#t.onDisconnect), this.#e.on("response", this.#t.onResponse), this.#e.on("connect", this.#t.onConnect), this.#e.on("connect_error", this.#t.onConnectError));
2448
2553
  }
2449
2554
  connectDevice(e) {
2555
+ if (!this.#e)
2556
+ throw new Error("Socket not connected. Call prepare() first.");
2450
2557
  this.#e.emit("connectDevice", { config: e });
2451
2558
  }
2452
2559
  disconnectDevice(e) {
2560
+ if (!this.#e)
2561
+ throw new Error("Socket not connected. Call prepare() first.");
2453
2562
  this.#e.emit("disconnectDevice", { config: e });
2454
2563
  }
2455
2564
  disconnectAllDevices() {
2565
+ if (!this.#e)
2566
+ throw new Error("Socket not connected. Call prepare() first.");
2456
2567
  this.#e.emit("disconnectAll");
2457
2568
  }
2458
2569
  write(e) {
2570
+ if (!this.#e)
2571
+ throw new Error("Socket not connected. Call prepare() first.");
2459
2572
  this.#e.emit("cmd", e);
2460
2573
  }
2461
2574
  onResponse(e) {
2462
2575
  let t = a.get(e.name, e.uuid);
2463
2576
  t || (t = a.getByNumber(e.name, e.deviceNumber)), t && t.socketResponse(e);
2464
2577
  }
2578
+ isConnected() {
2579
+ return this.#i;
2580
+ }
2581
+ isDisconnected() {
2582
+ return !this.#i;
2583
+ }
2465
2584
  }
2466
- const A = new ct(), q = {
2585
+ const w = new ct(), D = {
2467
2586
  baudRate: 9600,
2468
2587
  dataBits: 8,
2469
2588
  stopBits: 1,
@@ -2471,7 +2590,7 @@ const A = new ct(), q = {
2471
2590
  bufferSize: 32768,
2472
2591
  flowControl: "none"
2473
2592
  };
2474
- class ht extends pe {
2593
+ class ht extends de {
2475
2594
  __internal__ = {
2476
2595
  bypassSerialBytesConnection: !1,
2477
2596
  auto_response: !1,
@@ -2519,7 +2638,7 @@ class ht extends pe {
2519
2638
  delay_first_connection: 200,
2520
2639
  bytes_connection: null,
2521
2640
  filters: [],
2522
- config_port: q,
2641
+ config_port: D,
2523
2642
  queue: [],
2524
2643
  running_queue: !1,
2525
2644
  auto_response: null,
@@ -2545,17 +2664,17 @@ class ht extends pe {
2545
2664
  reconnection: 0
2546
2665
  }
2547
2666
  };
2548
- #t = null;
2667
+ #s = null;
2549
2668
  constructor({
2550
2669
  filters: e = null,
2551
- config_port: t = q,
2670
+ config_port: t = D,
2552
2671
  no_device: s = 1,
2553
2672
  device_listen_on_channel: i = 1,
2554
2673
  bypassSerialBytesConnection: r = !1,
2555
2674
  socket: o = !1
2556
2675
  } = {
2557
2676
  filters: null,
2558
- config_port: q,
2677
+ config_port: D,
2559
2678
  no_device: 1,
2560
2679
  device_listen_on_channel: 1,
2561
2680
  bypassSerialBytesConnection: !1,
@@ -2563,7 +2682,7 @@ class ht extends pe {
2563
2682
  }) {
2564
2683
  if (super(), !("serial" in navigator))
2565
2684
  throw new Error("Web Serial not supported");
2566
- e && (this.serialFilters = e), t && (this.serialConfigPort = t), r && (this.__internal__.bypassSerialBytesConnection = r), s && this.#w(s), i && ["number", "string"].includes(typeof i) && (this.listenOnChannel = i), this.__internal__.serial.socket = o, this.#y(), this.#g();
2685
+ e && (this.serialFilters = e), t && (this.serialConfigPort = t), r && (this.__internal__.bypassSerialBytesConnection = r), s && this.#b(s), i && ["number", "string"].includes(typeof i) && (this.listenOnChannel = i), this.__internal__.serial.socket = o, this.#y(), this.#g();
2567
2686
  }
2568
2687
  set listenOnChannel(e) {
2569
2688
  if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
@@ -2597,15 +2716,15 @@ class ht extends pe {
2597
2716
  this.__internal__.serial.useRTSCTS = e;
2598
2717
  }
2599
2718
  get isConnected() {
2600
- const e = this.__internal__.serial.connected, t = this.#s(this.__internal__.serial.port);
2719
+ const e = this.__internal__.serial.connected, t = this.#n(this.__internal__.serial.port);
2601
2720
  return e && !t && this.#e({ error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
2602
2721
  }
2603
2722
  get isConnecting() {
2604
2723
  return this.__internal__.serial.connecting;
2605
2724
  }
2606
2725
  get isDisconnected() {
2607
- const e = this.__internal__.serial.connected, t = this.#s(this.__internal__.serial.port);
2608
- return !e && t && (this.dispatch("serial:connected"), this.#i(!1), a.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
2726
+ const e = this.__internal__.serial.connected, t = this.#n(this.__internal__.serial.port);
2727
+ return !e && t && (this.dispatch("serial:connected"), this.#o(!1), a.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
2609
2728
  }
2610
2729
  get deviceNumber() {
2611
2730
  return this.__internal__.device_number;
@@ -2778,8 +2897,8 @@ class ht extends pe {
2778
2897
  }
2779
2898
  };
2780
2899
  }
2781
- #s(e) {
2782
- return this.useSocket ? this.__internal__.serial.connected : !!(e && e.readable && e.writable);
2900
+ #n(e) {
2901
+ return this.useSocket ? this.__internal__.serial.connected && w.isConnected() : !!(e && e.readable && e.writable);
2783
2902
  }
2784
2903
  async timeout(e, t) {
2785
2904
  this.__internal__.last_error.message = "Operation response timed out.", this.__internal__.last_error.action = t, this.__internal__.last_error.code = e, this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), t === "connect" ? (this.__internal__.serial.connected = !1, this.dispatch("serial:reconnect", {}), a.$dispatchChange(this)) : t === "connection:start" && (await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector += 1, a.$dispatchChange(this), await this.serialConnect()), this.__internal__.serial.queue.length > 0 && this.dispatch("internal:queue", {}), this.dispatch("serial:timeout", {
@@ -2794,13 +2913,13 @@ class ht extends pe {
2794
2913
  #e(e = null) {
2795
2914
  this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e), a.$dispatchChange(this);
2796
2915
  }
2797
- #r(e) {
2916
+ #i(e) {
2798
2917
  this.__internal__.serial.aux_connecting = e.detail.active ? "connecting" : "finished";
2799
2918
  }
2800
2919
  socketResponse(e) {
2801
2920
  const t = this.__internal__.serial.connected;
2802
- if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), a.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#i(!1)), e.type === "success")
2803
- this.#n(new Uint8Array(e.data));
2921
+ if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), a.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#o(!1)), e.type === "success")
2922
+ this.#r(new Uint8Array(e.data));
2804
2923
  else if (e.type === "error") {
2805
2924
  const s = new Error("The port is closed or is not readable/writable");
2806
2925
  this.serialErrors(s);
@@ -2809,9 +2928,9 @@ class ht extends pe {
2809
2928
  }
2810
2929
  async connect() {
2811
2930
  return this.isConnected ? !0 : (this.__internal__.serial.aux_connecting = "idle", new Promise((e, t) => {
2812
- this.#t || (this.#t = this.#r.bind(this)), this.on("internal:connecting", this.#t);
2931
+ this.#s || (this.#s = this.#i.bind(this)), this.on("internal:connecting", this.#s);
2813
2932
  const s = setInterval(() => {
2814
- this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(s), this.__internal__.serial.aux_connecting = "idle", this.#t !== null && this.off("internal:connecting", this.#t), this.isConnected ? e(!0) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`)) : this.__internal__.serial.aux_connecting === "connecting" && (this.__internal__.serial.aux_connecting = "idle", this.dispatch("internal:connecting", { active: !0 }), this.dispatch("serial:connecting", { active: !0 }));
2933
+ this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(s), this.__internal__.serial.aux_connecting = "idle", this.#s !== null && this.off("internal:connecting", this.#s), this.isConnected ? e(!0) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`)) : this.__internal__.serial.aux_connecting === "connecting" && (this.__internal__.serial.aux_connecting = "idle", this.dispatch("internal:connecting", { active: !0 }), this.dispatch("serial:connecting", { active: !0 }));
2815
2934
  }, 100);
2816
2935
  this.serialConnect();
2817
2936
  }));
@@ -2819,7 +2938,7 @@ class ht extends pe {
2819
2938
  async serialDisconnect() {
2820
2939
  try {
2821
2940
  if (this.useSocket)
2822
- A.disconnectDevice(this.configDeviceSocket);
2941
+ w.isConnected() && w.disconnectDevice(this.configDeviceSocket);
2823
2942
  else {
2824
2943
  const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
2825
2944
  e && (await e.cancel().catch((s) => this.serialErrors(s)), await this.__internal__.serial.input_done), t && (await t.getWriter().close(), await this.__internal__.serial.output_done), this.__internal__.serial.connected && this.__internal__.serial && this.__internal__.serial.port && await this.__internal__.serial.port.close();
@@ -2830,15 +2949,17 @@ class ht extends pe {
2830
2949
  this.__internal__.serial.reader = null, this.__internal__.serial.input_done = null, this.__internal__.serial.output_stream = null, this.__internal__.serial.output_done = null, this.__internal__.serial.connected = !1, this.__internal__.serial.port = null, a.$dispatchChange(this);
2831
2950
  }
2832
2951
  }
2833
- async #o(e) {
2952
+ async #a(e) {
2953
+ if (w.isDisconnected())
2954
+ throw this.#e({ error: "Socket is disconnected." }), new Error("The socket is disconnected");
2834
2955
  if (this.isDisconnected)
2835
2956
  throw this.#e({ error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
2836
2957
  const t = this.validateBytes(e);
2837
- A.write({ config: this.configDeviceSocket, bytes: Array.from(t) });
2958
+ w.write({ config: this.configDeviceSocket, bytes: Array.from(t) });
2838
2959
  }
2839
- async #a(e) {
2960
+ async #t(e) {
2840
2961
  if (this.useSocket) {
2841
- await this.#o(e);
2962
+ await this.#a(e);
2842
2963
  return;
2843
2964
  }
2844
2965
  const t = this.__internal__.serial.port;
@@ -2859,10 +2980,10 @@ class ht extends pe {
2859
2980
  await ee(100);
2860
2981
  }
2861
2982
  }
2862
- #n(e = new Uint8Array([]), t = !1) {
2983
+ #r(e = new Uint8Array([]), t = !1) {
2863
2984
  if (e && e.length > 0) {
2864
2985
  const s = this.__internal__.serial.connected;
2865
- if (this.__internal__.serial.connected = this.#s(this.__internal__.serial.port), a.$dispatchChange(this), !s && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#i(!1)), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), this.__internal__.serial.response.as === "hex")
2986
+ if (this.__internal__.serial.connected = this.#n(this.__internal__.serial.port), a.$dispatchChange(this), !s && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#o(!1)), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), this.__internal__.serial.response.as === "hex")
2866
2987
  t ? this.serialCorruptMessage(this.parseUint8ToHex(e)) : this.serialMessage(this.parseUint8ToHex(e));
2867
2988
  else if (this.__internal__.serial.response.as === "uint8")
2868
2989
  t ? this.serialCorruptMessage(e) : this.serialMessage(e);
@@ -2902,7 +3023,7 @@ class ht extends pe {
2902
3023
  return e.length === 0 ? t : t.filter((s) => {
2903
3024
  const i = s.getInfo();
2904
3025
  return e.some((r) => i.usbProductId === r.usbProductId && i.usbVendorId === r.usbVendorId);
2905
- }).filter((s) => !this.#s(s));
3026
+ }).filter((s) => !this.#n(s));
2906
3027
  }
2907
3028
  async serialPortsSaved(e) {
2908
3029
  const t = this.serialFilters;
@@ -2971,7 +3092,7 @@ class ht extends pe {
2971
3092
  }
2972
3093
  async #u() {
2973
3094
  this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
2974
- this.__internal__.serial.response.buffer && this.#n(this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
3095
+ this.__internal__.serial.response.buffer && this.#r(this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
2975
3096
  }, this.__internal__.serial.free_timeout_ms || 50);
2976
3097
  }
2977
3098
  async #_() {
@@ -2980,10 +3101,10 @@ class ht extends pe {
2980
3101
  if (this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), !(e === null || !t || t.length === 0)) {
2981
3102
  for (; t.length >= e; ) {
2982
3103
  const s = t.slice(0, e);
2983
- this.#n(s), t = t.slice(e);
3104
+ this.#r(s), t = t.slice(e);
2984
3105
  }
2985
3106
  this.__internal__.serial.response.buffer = t, t.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
2986
- this.#n(this.__internal__.serial.response.buffer, !0);
3107
+ this.#r(this.__internal__.serial.response.buffer, !0);
2987
3108
  }, this.__internal__.serial.free_timeout_ms || 50));
2988
3109
  }
2989
3110
  }
@@ -3022,26 +3143,26 @@ class ht extends pe {
3022
3143
  o.push(new TextEncoder().encode(c[1])), f = u.lastIndex;
3023
3144
  } else if (s)
3024
3145
  for (; (c = e.exec(r)) !== null; ) {
3025
- const u = c.index, p = r.slice(f, u);
3026
- o.push(new TextEncoder().encode(p)), f = e.lastIndex;
3146
+ const u = c.index, d = r.slice(f, u);
3147
+ o.push(new TextEncoder().encode(d)), f = e.lastIndex;
3027
3148
  }
3028
3149
  else if (t) {
3029
3150
  const u = r.split(e);
3030
3151
  u.shift();
3031
- for (const p of u)
3032
- o.push(new TextEncoder().encode(p));
3152
+ for (const d of u)
3153
+ o.push(new TextEncoder().encode(d));
3033
3154
  r = "";
3034
3155
  }
3035
3156
  r = r.slice(f);
3036
3157
  }
3037
3158
  for (const c of o)
3038
- this.#n(c);
3159
+ this.#r(c);
3039
3160
  const l = new TextEncoder().encode(r);
3040
3161
  this.__internal__.serial.response.buffer = l, l.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
3041
- this.#n(this.__internal__.serial.response.buffer, !0), this.__internal__.serial.response.buffer = new Uint8Array(0);
3162
+ this.#r(this.__internal__.serial.response.buffer, !0), this.__internal__.serial.response.buffer = new Uint8Array(0);
3042
3163
  }, this.__internal__.serial.free_timeout_ms ?? 50));
3043
3164
  }
3044
- async #p() {
3165
+ async #d() {
3045
3166
  const e = this.__internal__.serial.port;
3046
3167
  if (!e || !e.readable) throw new Error("Port is not readable");
3047
3168
  const t = e.readable.getReader();
@@ -3058,19 +3179,21 @@ class ht extends pe {
3058
3179
  t.releaseLock(), this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
3059
3180
  }
3060
3181
  }
3061
- #i(e) {
3182
+ #o(e) {
3062
3183
  e !== this.__internal__.serial.connecting && (this.__internal__.serial.connecting = e, this.dispatch("serial:connecting", { active: e }), this.dispatch("internal:connecting", { active: e }));
3063
3184
  }
3064
3185
  async serialConnect() {
3065
3186
  try {
3066
- if (this.#i(!0), this.useSocket)
3067
- A.prepare(), this.__internal__.serial.last_action = "connect", this.__internal__.timeout.until_response = setTimeout(async () => {
3187
+ if (this.#o(!0), this.useSocket) {
3188
+ if (w.prepare(), this.__internal__.serial.last_action = "connect", this.__internal__.timeout.until_response = setTimeout(async () => {
3068
3189
  await this.timeout(this.__internal__.serial.bytes_connection ?? [], "connection:start");
3069
- }, this.__internal__.time.response_connection), A.connectDevice(this.configDeviceSocket), this.dispatch("serial:sent", {
3190
+ }, this.__internal__.time.response_connection), w.isDisconnected())
3191
+ return;
3192
+ w.connectDevice(this.configDeviceSocket), this.dispatch("serial:sent", {
3070
3193
  action: "connect",
3071
3194
  bytes: this.__internal__.serial.bytes_connection
3072
3195
  });
3073
- else {
3196
+ } else {
3074
3197
  const e = await this.#l();
3075
3198
  if (e.length > 0)
3076
3199
  await this.serialPortsSaved(e);
@@ -3086,25 +3209,25 @@ class ht extends pe {
3086
3209
  await t.open(this.serialConfigPort);
3087
3210
  const s = this;
3088
3211
  t.onconnect = (i) => {
3089
- s.dispatch("serial:connected", i), s.#i(!1), a.$dispatchChange(this), s.__internal__.serial.queue.length > 0 ? s.dispatch("internal:queue", {}) : s.__internal__.serial.running_queue = !1;
3212
+ s.dispatch("serial:connected", i), s.#o(!1), a.$dispatchChange(this), s.__internal__.serial.queue.length > 0 ? s.dispatch("internal:queue", {}) : s.__internal__.serial.running_queue = !1;
3090
3213
  }, t.ondisconnect = async () => {
3091
3214
  await s.disconnect();
3092
3215
  }, await ee(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
3093
3216
  await s.timeout(s.__internal__.serial.bytes_connection ?? [], "connection:start");
3094
- }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#a(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
3217
+ }, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#t(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
3095
3218
  action: "connect",
3096
3219
  bytes: this.__internal__.serial.bytes_connection
3097
- }), this.__internal__.auto_response && this.#n(this.__internal__.serial.auto_response), await this.#p();
3220
+ }), this.__internal__.auto_response && this.#r(this.__internal__.serial.auto_response), await this.#d();
3098
3221
  }
3099
3222
  } catch (e) {
3100
- this.#i(!1), this.serialErrors(e);
3223
+ this.#o(!1), this.serialErrors(e);
3101
3224
  }
3102
3225
  }
3103
- async #d() {
3226
+ async #p() {
3104
3227
  return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
3105
3228
  }
3106
3229
  async serialForget() {
3107
- return await this.#d();
3230
+ return await this.#p();
3108
3231
  }
3109
3232
  decToHex(e) {
3110
3233
  return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
@@ -3148,8 +3271,15 @@ class ht extends pe {
3148
3271
  #g() {
3149
3272
  const e = this;
3150
3273
  this.on("internal:queue", async () => {
3151
- await e.#b();
3152
- }), this.#m();
3274
+ await e.#w();
3275
+ });
3276
+ const t = () => {
3277
+ e.isConnected && e.#e({ error: "Socket disconnected." });
3278
+ }, s = () => {
3279
+ e.isDisconnected && !e.isConnecting && e.serialConnect().catch(() => {
3280
+ });
3281
+ };
3282
+ this.useSocket && (window.addEventListener("serial:socket:disconnected", t), window.addEventListener("serial:socket:connected", s)), this.#m();
3153
3283
  }
3154
3284
  #m() {
3155
3285
  const e = this;
@@ -3158,8 +3288,10 @@ class ht extends pe {
3158
3288
  });
3159
3289
  });
3160
3290
  }
3161
- async #b() {
3162
- if (!this.#s(this.__internal__.serial.port)) {
3291
+ async #w() {
3292
+ if (this.useSocket && w.isDisconnected())
3293
+ return;
3294
+ if (!this.#n(this.__internal__.serial.port)) {
3163
3295
  this.#e({ error: "Port is closed, not readable or writable." }), await this.serialConnect();
3164
3296
  return;
3165
3297
  }
@@ -3173,7 +3305,7 @@ class ht extends pe {
3173
3305
  let t = this.__internal__.time.response_general;
3174
3306
  if (e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
3175
3307
  await this.timeout(e.bytes, e.action);
3176
- }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#a(e.bytes), this.dispatch("serial:sent", {
3308
+ }, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#t(e.bytes), this.dispatch("serial:sent", {
3177
3309
  action: e.action,
3178
3310
  bytes: e.bytes
3179
3311
  }), this.__internal__.auto_response) {
@@ -3183,7 +3315,7 @@ class ht extends pe {
3183
3315
  } catch (r) {
3184
3316
  this.serialErrors(r);
3185
3317
  }
3186
- this.#n(i);
3318
+ this.#r(i);
3187
3319
  }
3188
3320
  const s = [...this.__internal__.serial.queue];
3189
3321
  this.__internal__.serial.queue = s.splice(1), this.__internal__.serial.queue.length > 0 && (this.__internal__.serial.running_queue = !0);
@@ -3211,7 +3343,7 @@ class ht extends pe {
3211
3343
  }
3212
3344
  this.__internal__.serial.queue.push({ bytes: s, action: t }), this.dispatch("internal:queue", {});
3213
3345
  }
3214
- #w(e = 1) {
3346
+ #b(e = 1) {
3215
3347
  this.__internal__.device_number = e, !this.__internal__.bypassSerialBytesConnection && (this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e));
3216
3348
  }
3217
3349
  serialSetConnectionConstant(e = 1) {
@@ -3319,7 +3451,7 @@ class ht extends pe {
3319
3451
  }
3320
3452
  }
3321
3453
  export {
3322
- a as s,
3323
- A as u,
3324
- ht as v
3454
+ ht as S,
3455
+ w as c,
3456
+ a as s
3325
3457
  };