nsd-ble 0.3.3 → 0.4.1

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/main.js CHANGED
@@ -1,192 +1,234 @@
1
- import { l as x, u as I, a as N, b as D, c as B, B as p, S as P } from "./Objects-C2BH61j-.js";
2
- import { Data as A, DataType as f, ActionType as c, ActionDataType as l } from "./enums.js";
3
- import { Image as $, Rectangle as k, Text as H } from "./elements.js";
4
- class O {
1
+ import { l as I, u as x, a as O, b as Z, c as Q, d as tt, B, S as mt, s as et } from "./Objects-ulh5Kfet.js";
2
+ import { Data as i, DataType as f, ActionType as A, ActionDataType as U, FloodTypes as v, Button as G } from "./enums.js";
3
+ import { Image as yt, Rectangle as Et, Text as Tt } from "./elements.js";
4
+ class Dt {
5
5
  data = [];
6
6
  size = 0;
7
7
  total = 0;
8
8
  }
9
- const T = 7, w = /* @__PURE__ */ new Map();
10
- function z(t) {
11
- return w.get(t);
9
+ const j = 7, _ = /* @__PURE__ */ new Map();
10
+ function It(t) {
11
+ return _.get(t);
12
12
  }
13
- function G(t) {
14
- w.delete(t);
13
+ function Ut(t) {
14
+ _.delete(t);
15
15
  }
16
- function V(t) {
17
- const e = t.getUint8(A.CACHE), n = t.getUint16(A.DATA_SIZE, !0), r = t.getUint16(A.POSITION, !0), o = t.byteLength - T;
18
- if (!w.has(e)) {
19
- const i = new O();
20
- i.size = n, w.set(e, i);
16
+ function Ct(t) {
17
+ const e = t.getUint8(i.CACHE), o = t.getUint16(i.DATA_SIZE, !0), n = t.getUint16(i.POSITION, !0), r = t.byteLength - j;
18
+ if (!_.has(e)) {
19
+ const a = new Dt();
20
+ a.size = o, _.set(e, a);
21
21
  }
22
- const s = w.get(e);
23
- for (let i = 0; i < o; i++)
24
- s.data[i + r] = t.getUint8(T + i);
25
- s.total += o;
22
+ const s = _.get(e);
23
+ for (let a = 0; a < r; a++)
24
+ s.data[a + n] = t.getUint8(j + a);
25
+ s.total += r;
26
26
  }
27
- function F(t) {
28
- const e = w.get(t);
27
+ function St(t) {
28
+ const e = _.get(t);
29
29
  return e.total == e.size || e.size == 0;
30
30
  }
31
- function W() {
32
- return { add: V, get: z, remove: G, isDone: F };
31
+ function bt() {
32
+ return { add: Ct, get: It, remove: Ut, isDone: St };
33
33
  }
34
- const { serviceUuid: Z, handshakeCharUuid: K } = N(), C = new x.SyncEvent(), { add: X, get: j, remove: q, isDone: J } = W();
35
- function Q() {
34
+ const { serviceUuid: _t, handshakeCharUuid: Nt } = O(), nt = new I.SyncEvent(), { add: Rt, get: xt, remove: Bt, isDone: Pt } = bt();
35
+ function Lt() {
36
36
  return new Promise((t, e) => {
37
- I().subscribe(Z, K, Y).then(() => t()).catch((n) => e(n));
37
+ x().subscribe(_t, Nt, Mt).then(() => t()).catch((o) => e(o));
38
38
  });
39
39
  }
40
- function Y(t) {
40
+ function Mt(t) {
41
41
  console.log(t, t.buffer.byteLength);
42
- const e = t.getUint8(A.CACHE);
43
- X(t), J(e) && (C.post(tt(e)), q(e));
44
- }
45
- function tt(t) {
46
- const e = j(t), n = /* @__PURE__ */ new Map();
47
- let r = 1;
48
- for (; r < e.size; ) {
49
- const o = e.data[r++], s = e.data[r++], i = [];
50
- for (let a = 0; a < s; a++)
51
- i.push(e.data[r++]);
52
- n.set(o, i);
42
+ const e = t.getUint8(i.CACHE);
43
+ Rt(t), Pt(e) && (nt.post(Ot(e)), Bt(e));
44
+ }
45
+ function Ot(t) {
46
+ const e = xt(t), o = /* @__PURE__ */ new Map();
47
+ let n = 1;
48
+ for (; n < e.size; ) {
49
+ const r = e.data[n++], s = e.data[n++], a = [];
50
+ for (let c = 0; c < s; c++)
51
+ a.push(e.data[n++]);
52
+ o.set(r, a);
53
53
  }
54
- return n;
55
- }
56
- function S() {
57
- return { subscribe: Q, onNodes: C };
54
+ return o;
58
55
  }
59
- const { onNodes: et } = S(), { onMessageDone: nt, onNode: rt, onNodeLost: ot } = D(), { onPlusButton: st, onMinButton: it, onEitherButton: at } = B(), {
60
- connect: ct,
61
- reconnect: ut,
62
- onConnect: ht,
63
- onDisconnect: ft
64
- } = I();
65
- function dt() {
66
- return new Promise((t, e) => {
67
- ct(N().options).then(() => _(t)).catch((n) => e(n));
56
+ function ot() {
57
+ return { subscribe: Lt, onNodes: nt };
58
+ }
59
+ const { onNodes: vt } = ot(), { onMessageDone: kt, onNode: $t, onNodeLost: zt } = Z(), { onPlusButton: Ht, onMinButton: Ft, onEitherButton: Gt } = Q(), {
60
+ connect: Wt,
61
+ reconnect: rt,
62
+ onConnect: Vt,
63
+ onDisconnect: st
64
+ } = x(), it = new I.SyncEvent(), at = new I.SyncEvent();
65
+ let k = !1, b = !1, N = !1;
66
+ st.attach(() => {
67
+ k && !b && (tt().abortAll(new Error("BLE disconnected")), Kt());
68
+ });
69
+ async function Zt() {
70
+ N = !0, await Wt(O().options), await Y(), k = !0;
71
+ }
72
+ async function Yt() {
73
+ N = !0, await rt(), await Y(), k = !0;
74
+ }
75
+ function Y() {
76
+ return Promise.all([
77
+ ot().subscribe(),
78
+ Q().subscribe(),
79
+ Z().subscribe()
80
+ ]).then(() => {
68
81
  });
69
82
  }
70
- function lt() {
71
- return new Promise((t, e) => {
72
- ut().then(() => _(t)).catch((n) => e(n));
73
- });
83
+ async function Kt() {
84
+ const { RECONNECT_DELAY: t, RECONNECT_MAX_RETRIES: e } = O();
85
+ b = !0, N = !1;
86
+ for (let o = 1; o <= e; o++) {
87
+ const n = Math.min(
88
+ t * Math.pow(2, o - 1),
89
+ 3e4
90
+ );
91
+ if (it.post({ attempt: o, maxRetries: e, delay: n }), await qt(n), N) {
92
+ b = !1, N = !1;
93
+ return;
94
+ }
95
+ try {
96
+ await rt(), await Y(), b = !1;
97
+ return;
98
+ } catch (r) {
99
+ console.warn(
100
+ `Auto-reconnect attempt ${o}/${e} failed:`,
101
+ r
102
+ );
103
+ }
104
+ }
105
+ b = !1, at.post();
106
+ }
107
+ function qt(t) {
108
+ return new Promise((e) => setTimeout(e, t));
74
109
  }
75
- function _(t) {
76
- Promise.all([
77
- S().subscribe(),
78
- B().subscribe(),
79
- D().subscribe()
80
- ]).then(t);
110
+ function Xt() {
111
+ k = !1, b = !1, N = !1;
81
112
  }
82
- function gt() {
113
+ function jt() {
83
114
  return {
84
- connect: dt,
85
- onConnect: ht,
86
- onDisconnect: ft,
87
- reconnect: lt,
88
- onNodes: et,
89
- onNode: rt,
90
- onNodeLost: ot,
91
- onPlusButton: st,
92
- onMinButton: it,
93
- onEitherButton: at,
94
- onMessageDone: nt
115
+ connect: Zt,
116
+ onConnect: Vt,
117
+ onDisconnect: st,
118
+ onReconnecting: it,
119
+ onReconnectFailed: at,
120
+ reconnect: Yt,
121
+ onNodes: vt,
122
+ onNode: $t,
123
+ onNodeLost: zt,
124
+ onPlusButton: Ht,
125
+ onMinButton: Ft,
126
+ onEitherButton: Gt,
127
+ onMessageDone: kt,
128
+ _resetForTesting: Xt
95
129
  };
96
130
  }
97
- function ee(t) {
131
+ function hn(t) {
98
132
  return {
99
- upload: (e, n) => {
100
- const r = new Uint8Array(n.length + 1);
101
- return r.set([e], 0), r.set(n, 1), new p(f.UPLOAD, t.id, r);
133
+ upload: (e, o) => {
134
+ const n = new Uint8Array(o.length + 1);
135
+ return n.set([e], 0), n.set(o, 1), new B(f.UPLOAD, t.id, n);
102
136
  },
103
- clear: () => new p(f.CLEAR_GRAPHICS, t.id)
137
+ clear: () => new B(f.CLEAR_GRAPHICS, t.id)
104
138
  };
105
139
  }
106
- class wt {
140
+ class Jt {
107
141
  id;
108
142
  buffers;
109
143
  totalPackets = 0;
110
- onProgress = new x.SyncEvent();
111
- constructor(e, n) {
112
- this.id = n, this.buffers = e, this.buffers.forEach((r) => {
113
- this.totalPackets += r.packets.length;
144
+ onProgress = new I.SyncEvent();
145
+ onError = new I.SyncEvent();
146
+ constructor(e, o) {
147
+ this.id = o, this.buffers = e, this.buffers.forEach((n) => {
148
+ this.totalPackets += n.packets.length;
114
149
  });
115
150
  }
116
151
  async send() {
117
- const e = this.totalPackets & 255, n = this.totalPackets >> 8 & 255, r = new Uint8Array([e, n]), o = new p(f.LOADER, this.id, r), s = this.buffers.length;
118
- let i = 0;
119
- await o.send();
120
- for (const a of this.buffers)
121
- await a.send(), i++, this.onProgress.post({
122
- current: i,
123
- total: s,
124
- percentage: i / s
125
- });
152
+ const e = this.totalPackets & 255, o = this.totalPackets >> 8 & 255, n = new Uint8Array([e, o]), r = new B(f.LOADER, this.id, n), s = this.buffers.length;
153
+ let a = 0;
154
+ try {
155
+ await r.send();
156
+ for (const c of this.buffers)
157
+ await c.send(), a++, this.onProgress.post({
158
+ current: a,
159
+ total: s,
160
+ percentage: a / s
161
+ });
162
+ } catch (c) {
163
+ throw this.onError.post({
164
+ buffer: a,
165
+ error: c
166
+ }), c;
167
+ }
126
168
  }
127
169
  }
128
- function ne(t) {
170
+ function gn(t) {
129
171
  return {
130
- load: (e) => new wt(e, t.id)
172
+ load: (e) => new Jt(e, t.id)
131
173
  };
132
174
  }
133
- const { onNodes: pt, onNode: yt, onNodeLost: mt } = gt();
134
- pt.attach(At);
135
- yt.attach(Et);
136
- mt.attach(Ut);
137
- const u = [], E = new x.SyncEvent();
138
- function At(t) {
175
+ const { onNodes: Qt, onNode: te, onNodeLost: ee } = jt();
176
+ Qt.attach(ne);
177
+ te.attach(oe);
178
+ ee.attach(re);
179
+ const w = [], $ = new I.SyncEvent();
180
+ function ne(t) {
139
181
  console.log("standees", t);
140
- for (const [e, n] of t)
141
- v(e), u.find((r) => r.id === e).neighbors = n;
142
- E.post(u);
182
+ for (const [e, o] of t)
183
+ ct(e), w.find((n) => n.id === e).neighbors = o;
184
+ $.post(w);
143
185
  }
144
- function Et(t) {
186
+ function oe(t) {
145
187
  console.log("nodes", t);
146
- const e = t.keys().next().value, n = t.get(e);
147
- v(e), u.find((r) => r.id === e).neighbors = n, E.post(u);
188
+ const e = t.keys().next().value, o = t.get(e);
189
+ ct(e), w.find((n) => n.id === e).neighbors = o, $.post(w);
148
190
  }
149
- function Ut(t) {
150
- xt(t), E.post(u);
191
+ function re(t) {
192
+ se(t), $.post(w);
151
193
  }
152
- function v(t) {
153
- u.some((e) => e.id === t) || u.push(new P(t));
194
+ function ct(t) {
195
+ w.some((e) => e.id === t) || w.push(new mt(t));
154
196
  }
155
- function xt(t) {
156
- const e = u.findIndex((n) => n.id === t);
157
- e !== -1 && u.splice(e, 1);
197
+ function se(t) {
198
+ const e = w.findIndex((o) => o.id === t);
199
+ e !== -1 && w.splice(e, 1);
158
200
  }
159
- function re() {
160
- return { standees: u, onChange: E };
201
+ function An() {
202
+ return { standees: w, onChange: $ };
161
203
  }
162
- function bt(t) {
204
+ function ie(t) {
163
205
  const e = () => {
164
- let r = Math.floor(Math.random() * 256);
165
- return n(r) ? e() : r;
166
- }, n = (r) => t.some((o) => o.id === r);
206
+ let n = Math.floor(Math.random() * 256);
207
+ return o(n) ? e() : n;
208
+ }, o = (n) => t.some((r) => r.id === n);
167
209
  return { generate: e };
168
210
  }
169
- function oe(t) {
170
- return { add: (r) => (r.id = bt(t.elements).generate(), t.elements.push(r), r), remove: (r) => {
211
+ function wn(t) {
212
+ return { add: (n) => (n.id = ie(t.elements).generate(), t.elements.push(n), n), remove: (n) => {
171
213
  t.elements = t.elements.filter(
172
- (o) => o.id !== r
214
+ (r) => r.id !== n
173
215
  );
174
216
  } };
175
217
  }
176
- function Tt(t) {
177
- return new p(f.CLEAR_SCREEN, t.id);
218
+ function ae(t) {
219
+ return new B(f.CLEAR_SCREEN, t.id);
178
220
  }
179
- function It(t) {
221
+ function ce(t) {
180
222
  let e = new Uint8Array();
181
- for (const n of t.elements)
182
- n.dirty !== !1 && (n instanceof $ && (e = U(e, Nt(n))), n instanceof k && (e = U(e, Dt(n))), n instanceof H && (e = U(e, Bt(n))), n.dirty = !1);
183
- return new p(f.ALL, t.id, e);
223
+ for (const o of t.elements)
224
+ o.dirty !== !1 && (o instanceof yt && (e = W(e, ue(o))), o instanceof Et && (e = W(e, fe(o))), o instanceof Tt && (e = W(e, le(o))), o.dirty = !1);
225
+ return new B(f.ALL, t.id, e);
184
226
  }
185
- function U(t, e) {
186
- const n = new Uint8Array(t.length + e.length);
187
- return n.set(t, 0), n.set(e, t.length), n;
227
+ function W(t, e) {
228
+ const o = new Uint8Array(t.length + e.length);
229
+ return o.set(t, 0), o.set(e, t.length), o;
188
230
  }
189
- function Nt(t) {
231
+ function ue(t) {
190
232
  return new Uint8Array([
191
233
  f.DRAW_IMAGE,
192
234
  7,
@@ -199,7 +241,7 @@ function Nt(t) {
199
241
  t.data
200
242
  ]);
201
243
  }
202
- function Dt(t) {
244
+ function fe(t) {
203
245
  return new Uint8Array([
204
246
  f.DRAW_RECTANGLE,
205
247
  9,
@@ -214,9 +256,9 @@ function Dt(t) {
214
256
  t.colorBlack ? 0 : 1
215
257
  ]);
216
258
  }
217
- function Bt(t) {
218
- const e = new TextEncoder().encode(t.text), n = new Uint8Array(9 + e.length);
219
- return n.set(
259
+ function le(t) {
260
+ const e = new TextEncoder().encode(t.text), o = new Uint8Array(9 + e.length);
261
+ return o.set(
220
262
  [
221
263
  f.DRAW_TEXT,
222
264
  7 + e.length,
@@ -229,50 +271,50 @@ function Bt(t) {
229
271
  t.center ? 1 : 0
230
272
  ],
231
273
  0
232
- ), n.set(e, 9), n;
233
- }
234
- function se() {
235
- return { clear: Tt, draw: It };
236
- }
237
- function Ct(t, e) {
238
- const n = Math.ceil(t.length / e), r = Math.ceil(e / 8), o = new Uint8Array(r * n);
239
- for (let s = 0; s < n; s++)
240
- for (let i = 0; i < r; i++) {
241
- let a = 0;
242
- for (let g = 0; g < 8; g++) {
243
- const y = i * 8 + g, m = s * e + y;
244
- y < e && m < t.length && t[m] && (a |= 1 << 7 - g);
274
+ ), o.set(e, 9), o;
275
+ }
276
+ function pn() {
277
+ return { clear: ae, draw: ce };
278
+ }
279
+ function de(t, e) {
280
+ const o = Math.ceil(t.length / e), n = Math.ceil(e / 8), r = new Uint8Array(n * o);
281
+ for (let s = 0; s < o; s++)
282
+ for (let a = 0; a < n; a++) {
283
+ let c = 0;
284
+ for (let u = 0; u < 8; u++) {
285
+ const l = a * 8 + u, E = s * e + l;
286
+ l < e && E < t.length && t[E] && (c |= 1 << 7 - u);
245
287
  }
246
- o[s * r + i] = R(a);
288
+ r[s * n + a] = ut(c);
247
289
  }
248
- return o;
290
+ return r;
249
291
  }
250
- function R(t) {
292
+ function ut(t) {
251
293
  return t = (t & 240) >> 4 | (t & 15) << 4, t = (t & 204) >> 2 | (t & 51) << 2, t = (t & 170) >> 1 | (t & 85) << 1, t;
252
294
  }
253
- function St(t) {
254
- const e = t.reduce((o, s) => o + s.length, 0), n = new Uint8Array(e);
255
- let r = 0;
256
- for (const o of t)
257
- n.set(o, r), r += o.length;
258
- return n;
295
+ function he(t) {
296
+ const e = t.reduce((r, s) => r + s.length, 0), o = new Uint8Array(e);
297
+ let n = 0;
298
+ for (const r of t)
299
+ o.set(r, n), n += r.length;
300
+ return o;
259
301
  }
260
- function b() {
302
+ function K() {
261
303
  return {
262
- pixelsToBytes: Ct,
263
- bitswap: R,
264
- flattenUint8Arrays: St
304
+ pixelsToBytes: de,
305
+ bitswap: ut,
306
+ flattenUint8Arrays: he
265
307
  };
266
308
  }
267
- const { flattenUint8Arrays: L } = b();
268
- class d {
309
+ const { flattenUint8Arrays: ft } = K();
310
+ class T {
269
311
  element_id;
270
312
  property;
271
- constructor(e, n) {
272
- this.element_id = e, this.property = n;
313
+ constructor(e, o) {
314
+ this.element_id = e, this.property = o;
273
315
  }
274
316
  }
275
- class h {
317
+ class p {
276
318
  target;
277
319
  constructor(e) {
278
320
  this.target = e;
@@ -281,85 +323,85 @@ class h {
281
323
  return new Uint8Array();
282
324
  }
283
325
  }
284
- class _t extends h {
326
+ class ge extends p {
285
327
  format() {
286
328
  return new Uint8Array([
287
- c.INCREMENT,
329
+ A.INCREMENT,
288
330
  2,
289
331
  this.target.element_id,
290
332
  this.target.property
291
333
  ]);
292
334
  }
293
335
  }
294
- class vt extends h {
336
+ class Ae extends p {
295
337
  format() {
296
338
  return new Uint8Array([
297
- c.DECREMENT,
339
+ A.DECREMENT,
298
340
  2,
299
341
  this.target.element_id,
300
342
  this.target.property
301
343
  ]);
302
344
  }
303
345
  }
304
- class Rt extends h {
346
+ class we extends p {
305
347
  format() {
306
- return new Uint8Array([c.SHOW, 1, this.target.element_id]);
348
+ return new Uint8Array([A.SHOW, 1, this.target.element_id]);
307
349
  }
308
350
  }
309
- class Lt extends h {
351
+ class pe extends p {
310
352
  format() {
311
- return new Uint8Array([c.HIDE, 1, this.target.element_id]);
353
+ return new Uint8Array([A.HIDE, 1, this.target.element_id]);
312
354
  }
313
355
  }
314
- class Mt extends h {
356
+ let me = class extends p {
315
357
  value;
316
- constructor(e, n) {
317
- super(e), this.value = n;
358
+ constructor(e, o) {
359
+ super(e), this.value = o;
318
360
  }
319
361
  format() {
320
- const e = this.dataType(), n = this.convertValue(e), r = new Uint8Array(5 + n.length), o = [
321
- c.SET,
322
- n.length + 3,
362
+ const e = this.dataType(), o = this.convertValue(e), n = new Uint8Array(5 + o.length), r = [
363
+ A.SET,
364
+ o.length + 3,
323
365
  e,
324
366
  this.target.element_id,
325
367
  this.target.property
326
368
  ];
327
- return r.set(o, 0), r.set(n, 5), r;
369
+ return n.set(r, 0), n.set(o, 5), n;
328
370
  }
329
371
  dataType() {
330
- return typeof this.value === "string" ? l.STRING : l.NUMBER;
372
+ return typeof this.value === "string" ? U.STRING : U.NUMBER;
331
373
  }
332
374
  convertValue(e) {
333
- return e === l.STRING ? new TextEncoder().encode(this.value) : e === l.NUMBER ? new Uint8Array([this.value]) : new Uint8Array();
375
+ return e === U.STRING ? new TextEncoder().encode(this.value) : e === U.NUMBER ? new Uint8Array([this.value]) : new Uint8Array();
334
376
  }
335
- }
336
- class Pt extends h {
377
+ };
378
+ class ye extends p {
337
379
  format() {
338
380
  return this.target.element_id !== 0 && this.target.property !== 0 ? new Uint8Array([
339
- c.BROADCAST,
381
+ A.BROADCAST,
340
382
  2,
341
383
  this.target.element_id,
342
384
  this.target.property
343
- ]) : new Uint8Array([c.BROADCAST, 0]);
385
+ ]) : new Uint8Array([A.BROADCAST, 0]);
344
386
  }
345
387
  }
346
- class $t extends h {
388
+ class Ee extends p {
347
389
  format() {
348
- return new Uint8Array([c.BREAK, 0]);
390
+ return new Uint8Array([A.BREAK, 0]);
349
391
  }
350
392
  }
351
- let kt = class extends h {
393
+ let Te = class extends p {
352
394
  fromLow;
353
395
  fromHigh;
354
396
  toLow;
355
397
  toHigh;
356
398
  value;
357
- constructor(e, n, r, o, s, i) {
358
- super(i), this.value = e, this.fromLow = n, this.fromHigh = r, this.toLow = o, this.toHigh = s;
399
+ constructor(e, o, n, r, s, a) {
400
+ super(a), this.value = e, this.fromLow = o, this.fromHigh = n, this.toLow = r, this.toHigh = s;
359
401
  }
360
402
  format() {
361
403
  return new Uint8Array([
362
- c.MAP,
404
+ A.MAP,
363
405
  8,
364
406
  this.value.element_id,
365
407
  this.value.property,
@@ -372,174 +414,486 @@ let kt = class extends h {
372
414
  ]);
373
415
  }
374
416
  };
375
- class Ht extends h {
417
+ class De extends p {
376
418
  deciseconds;
377
419
  resetable;
378
420
  onComplete;
379
- constructor(e, n, r) {
380
- super(new d(0, 0)), this.deciseconds = e, this.resetable = n, this.onComplete = r;
421
+ constructor(e, o, n) {
422
+ super(new T(0, 0)), this.deciseconds = e, this.resetable = o, this.onComplete = n;
381
423
  }
382
424
  format() {
383
- const e = L(
384
- this.onComplete.map((o) => o.format())
385
- ), n = new Uint8Array(4 + e.length), r = [
386
- c.TIMER,
425
+ const e = ft(
426
+ this.onComplete.map((r) => r.format())
427
+ ), o = new Uint8Array(4 + e.length), n = [
428
+ A.TIMER,
387
429
  e.length + 2,
388
430
  this.resetable ? 1 : 0,
389
431
  this.deciseconds
390
432
  ];
391
- return n.set(r, 0), e.forEach(
392
- (o, s) => n.set([o], s + 4)
393
- ), n;
433
+ return o.set(n, 0), e.forEach(
434
+ (r, s) => o.set([r], s + 4)
435
+ ), o;
394
436
  }
395
437
  }
396
- class Ot extends h {
438
+ class Ie extends p {
397
439
  left;
398
440
  operator;
399
441
  right;
400
442
  actions;
401
- constructor(e, n, r, o) {
402
- super(new d(0, 0)), this.actions = o, this.left = e, this.operator = n, this.right = r;
443
+ constructor(e, o, n, r) {
444
+ super(new T(0, 0)), this.actions = r, this.left = e, this.operator = o, this.right = n;
403
445
  }
404
446
  convertValue(e) {
405
447
  if (typeof e == "number")
406
- return new Uint8Array([l.NUMBER, 1, e]);
448
+ return new Uint8Array([U.NUMBER, 1, e]);
407
449
  if (typeof e == "string") {
408
- const n = new TextEncoder().encode(e);
450
+ const o = new TextEncoder().encode(e);
409
451
  return new Uint8Array([
410
- l.STRING,
411
- n.length,
412
- ...n
452
+ U.STRING,
453
+ o.length,
454
+ ...o
413
455
  ]);
414
456
  }
415
- return e instanceof d ? new Uint8Array([
416
- l.ELEMENT,
457
+ return e instanceof T ? new Uint8Array([
458
+ U.ELEMENT,
417
459
  2,
418
460
  e.element_id,
419
461
  e.property
420
462
  ]) : new Uint8Array();
421
463
  }
422
464
  format() {
423
- const e = L(
424
- this.actions.map((a) => a.format())
425
- ), n = this.convertValue(this.left), r = this.convertValue(this.right), o = 4 + n.length + r.length + e.length, s = new Uint8Array(o), i = [
426
- c.CONDITION,
427
- o - 2,
428
- n.length + r.length + 1
465
+ const e = ft(
466
+ this.actions.map((c) => c.format())
467
+ ), o = this.convertValue(this.left), n = this.convertValue(this.right), r = 4 + o.length + n.length + e.length, s = new Uint8Array(r), a = [
468
+ A.CONDITION,
469
+ r - 2,
470
+ o.length + n.length + 1
429
471
  ];
430
- return s.set(i, 0), s.set([this.operator], 3), s.set(n, 4), s.set(r, 4 + n.length), s.set(e, 4 + n.length + r.length), s;
472
+ return s.set(a, 0), s.set([this.operator], 3), s.set(o, 4), s.set(n, 4 + o.length), s.set(e, 4 + o.length + n.length), s;
431
473
  }
432
474
  }
433
- function ae() {
475
+ function En() {
434
476
  return {
435
477
  target(t, e) {
436
- return new d(t, e);
478
+ return new T(t, e);
437
479
  },
438
480
  increment(t) {
439
- return new _t(t);
481
+ return new ge(t);
440
482
  },
441
483
  decrement(t) {
442
- return new vt(t);
484
+ return new Ae(t);
443
485
  },
444
486
  show(t) {
445
- return new Rt(new d(t, 0));
487
+ return new we(new T(t, 0));
446
488
  },
447
489
  hide(t) {
448
- return new Lt(new d(t, 0));
490
+ return new pe(new T(t, 0));
449
491
  },
450
492
  set(t, e) {
451
- return new Mt(t, e);
493
+ return new me(t, e);
452
494
  },
453
495
  broadcast(t) {
454
- return new Pt(t || new d(0, 0));
496
+ return new ye(t || new T(0, 0));
455
497
  },
456
- timer(t, e, n) {
457
- return new Ht(t, e, n);
498
+ timer(t, e, o) {
499
+ return new De(t, e, o);
458
500
  },
459
501
  stop() {
460
- return new $t(new d(0, 0));
502
+ return new Ee(new T(0, 0));
461
503
  },
462
- condition(t, e, n, r) {
463
- return new Ot(t, e, n, r);
504
+ condition(t, e, o, n) {
505
+ return new Ie(t, e, o, n);
464
506
  },
465
- map(t, e, n, r, o, s) {
466
- return new kt(t, e, n, r, o, s);
507
+ map(t, e, o, n, r, s) {
508
+ return new Te(t, e, o, n, r, s);
467
509
  }
468
510
  };
469
511
  }
470
- const { flattenUint8Arrays: zt } = b();
471
- function ce(t) {
512
+ const { flattenUint8Arrays: Ue } = K();
513
+ function Tn(t) {
472
514
  return {
473
- setButton(e, n) {
474
- const r = zt(
475
- n.map((s) => s.format())
476
- ), o = new Uint8Array(1 + r.length);
477
- return o.set([e], 0), r.forEach(
478
- (s, i) => o.set([s], i + 1)
479
- ), new p(f.ACTIONS, t.id, o);
515
+ setButton(e, o) {
516
+ const n = Ue(
517
+ o.map((s) => s.format())
518
+ ), r = new Uint8Array(1 + n.length);
519
+ return r.set([e], 0), n.forEach(
520
+ (s, a) => r.set([s], a + 1)
521
+ ), new B(f.ACTIONS, t.id, r);
480
522
  }
481
523
  };
482
524
  }
483
- function Gt(t) {
484
- return new Promise((e, n) => {
485
- const r = new Image();
486
- r.onload = () => e(r), r.onerror = n, r.src = t;
525
+ function Ce(t) {
526
+ return new Promise((e, o) => {
527
+ const n = new Image();
528
+ n.onload = () => e(n), n.onerror = o, n.src = t;
487
529
  });
488
530
  }
489
- function Vt(t) {
490
- const e = document.createElement("canvas"), n = e.getContext("2d");
491
- if (e.width = t.width, e.height = t.height, !n)
531
+ function Se(t) {
532
+ const e = document.createElement("canvas"), o = e.getContext("2d");
533
+ if (e.width = t.width, e.height = t.height, !o)
492
534
  throw new Error("Failed to get 2D context for image data.");
493
- return n.drawImage(t, 0, 0), n.getImageData(0, 0, t.width, t.height);
494
- }
495
- function Ft(t) {
496
- const e = [], n = t.data, r = t.width, o = t.height;
497
- for (let s = 0; s < o; s++)
498
- for (let i = 0; i < r; i++) {
499
- const a = (s * r + i) * 4, g = n[a], y = n[a + 1], m = n[a + 2], M = 0.2126 * g + 0.7152 * y + 0.0722 * m;
500
- e.push(M < 128 ? 0 : 1);
535
+ return o.drawImage(t, 0, 0), o.getImageData(0, 0, t.width, t.height);
536
+ }
537
+ function be(t) {
538
+ const e = [], o = t.data, n = t.width, r = t.height;
539
+ for (let s = 0; s < r; s++)
540
+ for (let a = 0; a < n; a++) {
541
+ const c = (s * n + a) * 4, u = o[c], l = o[c + 1], E = o[c + 2], F = 0.2126 * u + 0.7152 * l + 0.0722 * E;
542
+ e.push(F < 128 ? 0 : 1);
501
543
  }
502
544
  return {
503
- width: r,
504
- height: o,
545
+ width: n,
546
+ height: r,
505
547
  pixels: e
506
548
  };
507
549
  }
508
- function Wt() {
550
+ function _e() {
509
551
  return {
510
- createImage: Gt,
511
- getImageData: Vt,
512
- imageDataToPixels: Ft
552
+ createImage: Ce,
553
+ getImageData: Se,
554
+ imageDataToPixels: be
513
555
  };
514
556
  }
515
- const { createImage: Zt, getImageData: Kt, imageDataToPixels: Xt } = Wt(), { pixelsToBytes: jt } = b();
516
- function qt(t) {
517
- return new Promise((e, n) => {
518
- Zt(t).then((r) => Kt(r)).then((r) => Xt(r)).then((r) => jt(r.pixels, r.width)).then((r) => e(r)).catch((r) => n(r));
557
+ const { createImage: Ne, getImageData: Re, imageDataToPixels: xe } = _e(), { pixelsToBytes: Be } = K();
558
+ function Pe(t) {
559
+ return new Promise((e, o) => {
560
+ Ne(t).then((n) => Re(n)).then((n) => xe(n)).then((n) => Be(n.pixels, n.width)).then((n) => e(n)).catch((n) => o(n));
561
+ });
562
+ }
563
+ function Dn() {
564
+ return {
565
+ imageToBytes: Pe
566
+ };
567
+ }
568
+ function Le(t, e, o, n, r) {
569
+ return (t - e) / (o - e) * (r - n) + n;
570
+ }
571
+ function In() {
572
+ return { map: Le };
573
+ }
574
+ let d = !1;
575
+ const C = [], y = /* @__PURE__ */ new Map();
576
+ let P = 500, V = !1;
577
+ const lt = new I.SyncEvent(), L = new I.SyncEvent(), dt = /* @__PURE__ */ new Map([
578
+ [f.UPLOAD, "UPLOAD"],
579
+ [f.DRAW_IMAGE, "DRAW_IMAGE"],
580
+ [f.DRAW_RECTANGLE, "DRAW_RECTANGLE"],
581
+ [f.BUTTON_PRESS, "BUTTON_PRESS"],
582
+ [f.DRAW_TEXT, "DRAW_TEXT"],
583
+ [f.ALL, "ALL"],
584
+ [f.CLEAR_GRAPHICS, "CLEAR_GRAPHICS"],
585
+ [f.ACTIONS, "ACTIONS"],
586
+ [f.CLEAR_SCREEN, "CLEAR_SCREEN"],
587
+ [f.LOADER, "LOADER"]
588
+ ]);
589
+ function Me() {
590
+ d = !0, V || (Ve(), V = !0);
591
+ }
592
+ function Oe() {
593
+ d = !1;
594
+ }
595
+ function ve() {
596
+ return d;
597
+ }
598
+ function ke(t) {
599
+ P = t, ht();
600
+ }
601
+ function $e() {
602
+ return C;
603
+ }
604
+ function ze() {
605
+ return Array.from(y.values());
606
+ }
607
+ function He(t) {
608
+ return y.get(t);
609
+ }
610
+ function Fe() {
611
+ C.length = 0, y.clear();
612
+ }
613
+ function Ge(t) {
614
+ const e = t.length > i.TYPE ? t[i.TYPE] : 0;
615
+ return {
616
+ cacheId: t[i.CACHE],
617
+ targetId: t[i.ID],
618
+ ttl: t[i.TTL],
619
+ position: t.length > 4 ? t[i.POSITION] | t[i.POSITION + 1] << 8 : 0,
620
+ dataSize: t.length > 6 ? t[i.DATA_SIZE] | t[i.DATA_SIZE + 1] << 8 : 0,
621
+ typeName: dt.get(e) ?? `UNKNOWN(${e})`,
622
+ typeValue: e,
623
+ payloadBytes: Math.max(0, t.length - i.DATA),
624
+ raw: Array.from(t)
625
+ };
626
+ }
627
+ function g(t, e) {
628
+ if (!d) return;
629
+ const o = {
630
+ type: t,
631
+ timestamp: Date.now(),
632
+ data: e
633
+ };
634
+ C.push(o), ht(), lt.post(o);
635
+ }
636
+ function ht() {
637
+ C.length > P && C.splice(0, C.length - P);
638
+ }
639
+ function We(t, e) {
640
+ if (!d) return;
641
+ const o = y.get(t);
642
+ o && (Object.assign(o, e), o.completedAt && o.queuedAt && (o.latencyMs = o.completedAt - o.queuedAt), L.post(o));
643
+ }
644
+ function Ve() {
645
+ const t = tt(), e = Z(), o = x();
646
+ t.onMessageQueued.attach((n) => {
647
+ d && (y.set(n.cacheId, {
648
+ cacheId: n.cacheId,
649
+ queuedAt: n.timestamp,
650
+ firstPacketAt: null,
651
+ completedAt: null,
652
+ latencyMs: null,
653
+ retransmitCount: 0,
654
+ timeoutCount: 0,
655
+ packetsSent: 0,
656
+ totalPackets: n.packetCount
657
+ }), g("message_queued", {
658
+ cacheId: n.cacheId,
659
+ typeName: dt.get(n.type) ?? `UNKNOWN(${n.type})`,
660
+ targetId: n.targetId,
661
+ packetCount: n.packetCount,
662
+ totalBytes: n.totalBytes
663
+ }));
664
+ }), t.onPacketSent.attach((n) => {
665
+ if (!d) return;
666
+ const r = y.get(n.cacheId);
667
+ r && (r.packetsSent++, r.firstPacketAt === null && (r.firstPacketAt = n.timestamp)), g("packet_sent", {
668
+ cacheId: n.cacheId,
669
+ position: n.position,
670
+ dataSize: n.dataSize,
671
+ batchIndex: n.batchIndex
672
+ });
673
+ }), t.onRetransmit.attach((n) => {
674
+ if (!d) return;
675
+ const r = y.get(n.cacheId);
676
+ r && (r.retransmitCount++, L.post(r)), g("retransmit", {
677
+ cacheId: n.cacheId,
678
+ packetCount: n.packetCount,
679
+ reason: n.reason
680
+ });
681
+ }), t.onTimeout.attach((n) => {
682
+ if (!d) return;
683
+ const r = y.get(n.cacheId);
684
+ r && (r.timeoutCount++, L.post(r)), g("timeout", {
685
+ cacheId: n.cacheId,
686
+ retryCount: n.retryCount,
687
+ maxRetries: n.maxRetries,
688
+ willRetry: n.willRetry
689
+ });
690
+ }), t.onMessageComplete.attach((n) => {
691
+ d && (We(n.cacheId, {
692
+ completedAt: n.timestamp
693
+ }), g("message_complete", {
694
+ cacheId: n.cacheId
695
+ }));
696
+ }), e.onMessageDone.attach((n) => {
697
+ d && g("message_done", { cacheId: n });
698
+ }), e.onReceivedPackets.attach((n) => {
699
+ d && g("received_packets", {
700
+ incompleteCaches: n.filter((r) => r !== 0)
701
+ });
702
+ }), e.onNode.attach((n) => {
703
+ if (d)
704
+ for (const [r, s] of n)
705
+ g("node_discovered", { nodeId: r, neighbors: s });
706
+ }), e.onNodeLost.attach((n) => {
707
+ d && g("node_lost", { nodeId: n });
708
+ }), o.onConnect.attach(() => {
709
+ d && g("connected", {});
710
+ }), o.onDisconnect.attach(() => {
711
+ d && g("disconnected", {});
519
712
  });
520
713
  }
521
- function ue() {
714
+ function Ze() {
715
+ d = !1, C.length = 0, y.clear(), V = !1, P = 500;
716
+ }
717
+ function Un() {
522
718
  return {
523
- imageToBytes: qt
719
+ enable: Me,
720
+ disable: Oe,
721
+ isEnabled: ve,
722
+ clear: Fe,
723
+ getLog: $e,
724
+ getTimings: ze,
725
+ getTimingForCache: He,
726
+ setMaxLogSize: ke,
727
+ decodePacket: Ge,
728
+ onDebugEvent: lt,
729
+ onTimingUpdate: L,
730
+ _resetForTesting: Ze
524
731
  };
525
732
  }
526
- function Jt(t, e, n, r, o) {
527
- return (t - e) / (n - e) * (o - r) + r;
733
+ const { messageCharUuid: gt, messageFloodCharUuid: z, handshakeCharUuid: At, BATCH_SIZE: Ye, PACKET_SIZE: M } = O();
734
+ let q = !1, R = !1, H = !0, h = { standees: [] };
735
+ const m = /* @__PURE__ */ new Map(), D = /* @__PURE__ */ new Map();
736
+ let S = 0;
737
+ const J = 7;
738
+ function Ke(t) {
739
+ h = {
740
+ responseDelay: 10,
741
+ dropRate: 0,
742
+ partialDelivery: !1,
743
+ ...t
744
+ }, q = !0, R = !1, H = !0, m.clear(), D.clear(), S = 0, et(rn);
745
+ }
746
+ function wt() {
747
+ q = !1, R = !1, H = !0, m.clear(), D.clear(), S = 0, et(null);
748
+ }
749
+ function qe() {
750
+ return q;
751
+ }
752
+ function Xe() {
753
+ return h;
754
+ }
755
+ function je(t) {
756
+ h = { ...h, ...t };
757
+ }
758
+ function Je(t, e, o) {
759
+ const n = m.get(gt);
760
+ if (!n) return;
761
+ const r = e === "plus" ? G.PLUS : e === "minus" ? G.MINUS : G.EITHER, s = o ? new TextEncoder().encode(o) : new Uint8Array(0), a = i.DATA + 1 + s.length, c = new ArrayBuffer(a), u = new DataView(c);
762
+ u.setUint8(i.ID, t), u.setUint8(i.TYPE, f.BUTTON_PRESS), u.setUint8(i.DATA, r);
763
+ for (let l = 0; l < s.length; l++)
764
+ u.setUint8(i.DATA + 1 + l, s[l]);
765
+ n(u);
766
+ }
767
+ function Qe(t) {
768
+ const e = m.get(z);
769
+ if (!e) return;
770
+ const o = new ArrayBuffer(i.DATA + 1), n = new DataView(o);
771
+ n.setUint8(i.ID, t), n.setUint8(i.TYPE, v.LOST_NODE), n.setUint8(i.DATA, t), e(n);
772
+ }
773
+ function tn(t) {
774
+ const e = m.get(z);
775
+ e && e(cn(t));
776
+ }
777
+ function en() {
778
+ R = !1, m.clear(), D.clear(), S = 0, x().onDisconnect.post();
779
+ }
780
+ function nn(t) {
781
+ H = t;
782
+ }
783
+ function on() {
784
+ wt();
785
+ }
786
+ const rn = {
787
+ async connect(t) {
788
+ R = !0, x().onConnect.post();
789
+ },
790
+ async reconnect() {
791
+ if (!H)
792
+ throw new Error("No devices found");
793
+ R = !0, x().onConnect.post();
794
+ },
795
+ async readCharacteristic(t, e) {
796
+ },
797
+ async writeCharacteristic(t, e, o) {
798
+ if (!R || e !== gt || h.dropRate !== void 0 && h.dropRate > 0 && Math.random() < h.dropRate)
799
+ return;
800
+ const n = o[i.CACHE], r = o[i.POSITION] | o[i.POSITION + 1] << 8, s = o[i.DATA_SIZE] | o[i.DATA_SIZE + 1] << 8;
801
+ D.has(n) || D.set(n, {
802
+ received: /* @__PURE__ */ new Set(),
803
+ dataSize: s
804
+ });
805
+ const a = D.get(n);
806
+ a.received.add(r), S++;
807
+ const c = Math.ceil(s / M);
808
+ if (a.received.size >= c) {
809
+ const l = h.responseDelay ?? 10;
810
+ setTimeout(() => {
811
+ sn(n);
812
+ }, l);
813
+ return;
814
+ }
815
+ if (h.partialDelivery && S >= Ye) {
816
+ S = 0;
817
+ const l = h.responseDelay ?? 10;
818
+ setTimeout(() => {
819
+ const E = [];
820
+ for (const [F, X] of D) {
821
+ const pt = Math.ceil(X.dataSize / M);
822
+ X.received.size < pt && E.push(F);
823
+ }
824
+ E.length > 0 && an(E);
825
+ }, l);
826
+ }
827
+ },
828
+ async subscribe(t, e, o) {
829
+ m.set(e, o), e === At && un();
830
+ }
831
+ };
832
+ function sn(t) {
833
+ const e = m.get(z);
834
+ if (!e) return;
835
+ const o = new ArrayBuffer(i.DATA + 1), n = new DataView(o);
836
+ n.setUint8(i.TYPE, v.MESSAGE_DONE), n.setUint8(i.DATA, t), D.delete(t), S = 0, e(n);
837
+ }
838
+ function an(t) {
839
+ const e = m.get(z);
840
+ if (!e) return;
841
+ const o = new ArrayBuffer(i.DATA + t.length), n = new DataView(o);
842
+ n.setUint8(i.TYPE, v.DATA_FEEDBACK);
843
+ for (let r = 0; r < t.length; r++)
844
+ n.setUint8(i.DATA + r, t[r]);
845
+ e(n);
846
+ }
847
+ function cn(t) {
848
+ const e = i.DATA + 1 + t.neighbors.length, o = new ArrayBuffer(e), n = new DataView(o);
849
+ n.setUint8(i.TYPE, v.NODES), n.setUint8(i.DATA, t.id);
850
+ for (let r = 0; r < t.neighbors.length; r++)
851
+ n.setUint8(i.DATA + 1 + r, t.neighbors[r]);
852
+ return n;
528
853
  }
529
- function he() {
530
- return { map: Jt };
854
+ function un() {
855
+ const t = m.get(At);
856
+ if (!t || h.standees.length === 0) return;
857
+ const e = [h.standees.length];
858
+ for (const r of h.standees)
859
+ e.push(r.id, r.neighbors.length, ...r.neighbors);
860
+ const o = 200, n = e.length;
861
+ for (let r = 0; r < n; r += M) {
862
+ const s = Math.min(M, n - r), a = J + s, c = new ArrayBuffer(a), u = new DataView(c);
863
+ u.setUint8(i.ID, 0), u.setUint8(i.CACHE, o), u.setUint8(i.TTL, 10), u.setUint16(i.POSITION, r, !0), u.setUint16(i.DATA_SIZE, n, !0);
864
+ for (let l = 0; l < s; l++)
865
+ u.setUint8(J + l, e[r + l]);
866
+ t(u);
867
+ }
868
+ }
869
+ function Cn() {
870
+ return {
871
+ activate: Ke,
872
+ deactivate: wt,
873
+ isActive: qe,
874
+ getConfig: Xe,
875
+ updateConfig: je,
876
+ pressButton: Je,
877
+ simulateNodeLost: Qe,
878
+ simulateNodeJoin: tn,
879
+ disconnect: en,
880
+ reconnectable: nn,
881
+ _resetForTesting: on
882
+ };
531
883
  }
532
884
  export {
533
- ae as useActions,
534
- b as useBinary,
535
- ce as useButtons,
536
- ue as useConverter,
537
- se as useDisplay,
538
- ee as useGraphics,
539
- Wt as useImage,
540
- ne as useLoader,
541
- gt as useMesh,
542
- oe as useStandeeElements,
543
- re as useStandees,
544
- he as useUtils
885
+ En as useActions,
886
+ K as useBinary,
887
+ Tn as useButtons,
888
+ Dn as useConverter,
889
+ Un as useDebug,
890
+ pn as useDisplay,
891
+ hn as useGraphics,
892
+ _e as useImage,
893
+ gn as useLoader,
894
+ jt as useMesh,
895
+ Cn as useMockBluetooth,
896
+ wn as useStandeeElements,
897
+ An as useStandees,
898
+ In as useUtils
545
899
  };