nsd-ble 0.4.0 → 0.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.
package/README.md CHANGED
@@ -253,7 +253,7 @@ Available actions:
253
253
  | `set(target, value)` | Set a property to a specific value |
254
254
  | `broadcast(target?)` | Broadcast a value across the mesh |
255
255
  | `stop()` | Break the action chain |
256
- | `timer(ms, resetable, onComplete)` | Run actions after a delay |
256
+ | `timer(deciseconds, resetable, onComplete)` | Run actions after a delay (1/10s) |
257
257
  | `condition(left, op, right, actions)` | Conditional action execution |
258
258
  | `map(value, fromLow, fromHigh, toLow, toHigh, target)` | Map a value between ranges |
259
259
 
@@ -265,6 +265,50 @@ const hp = actions.target(label.id, ElementData.TEXT);
265
265
  actions.condition(hp, ActionCondition.EQUAL, 0, [actions.show(gameOverElement.id)]);
266
266
  ```
267
267
 
268
+ ### Receiving Broadcast Data
269
+
270
+ The `broadcast()` action sends data from the standee back to your app through the mesh. When a physical button press triggers a `broadcast()`, the standee reads the targeted element property and floods it as a `BUTTON_PRESS` message. The library delivers this via the button events on `useMesh()`.
271
+
272
+ ```typescript
273
+ const { onPlusButton, onMinButton, onEitherButton } = useMesh();
274
+
275
+ onPlusButton.attach((result) => {
276
+ console.log(`Standee ${result.id} broadcast: ${result.data}`);
277
+ });
278
+ ```
279
+
280
+ The `ButtonResult` contains:
281
+ - **`id`** — the standee ID that sent the broadcast
282
+ - **`data`** — the element property value as a string
283
+
284
+ Which event fires depends on which physical button was pressed. Actions on `Button.PLUS` trigger `onPlusButton`, `Button.MINUS` triggers `onMinButton`, and `Button.EITHER` triggers `onEitherButton`.
285
+
286
+ **Broadcast with a target** sends the current value of that element property. For a Text element targeted at `ElementData.DATA`, `result.data` contains the text content (e.g. `"42"`):
287
+
288
+ ```typescript
289
+ const { target, increment, broadcast } = useActions();
290
+ const { setButton } = useButtons(standee);
291
+
292
+ const hp = target(counter.id, ElementData.DATA);
293
+ await setButton(Button.PLUS, [increment(hp), broadcast(hp)]).send();
294
+
295
+ onPlusButton.attach((result) => {
296
+ const value = parseInt(result.data, 10);
297
+ console.log(`HP is now: ${value}`); // "1", "2", "3", ...
298
+ });
299
+ ```
300
+
301
+ **Broadcast without a target** (`broadcast()` with no arguments) still triggers the button event, but `result.data` will be an empty string. This is useful for simple "button was pressed" notifications:
302
+
303
+ ```typescript
304
+ await setButton(Button.PLUS, [increment(hp), broadcast()]).send();
305
+
306
+ onPlusButton.attach((result) => {
307
+ // result.data === ""
308
+ console.log(`Standee ${result.id} plus pressed`);
309
+ });
310
+ ```
311
+
268
312
  ### useConverter()
269
313
 
270
314
  Converts images to the monochrome byte format the standees expect.
@@ -1 +1 @@
1
- "use strict";const y=require("./enums.cjs");var q={},U={},he;function K(){if(he)return U;he=1,Object.defineProperty(U,"__esModule",{value:!0}),U.BaseEvent=void 0;var e=(function(){function r(){}return r.prototype.attach=function(t,l){return this._attach(t,l,!1)},r.prototype.once=function(t,l){return this._attach(t,l,!0)},r.prototype._attach=function(t,l,h){var v=this,u,a,i,c;if(typeof t=="function")a=t,c=function(){return v.detach(a)};else if(!l&&typeof t.post=="function")i=t,c=function(){return v.detach(i)};else{if(typeof t!="object"||t===void 0)throw new Error("Expect a function or object as first argument");if(typeof l!="function")throw new Error("Expect a function as second argument");u=t,a=l,c=function(){return v.detach(u,a)}}return this._listeners?this._listeners=this._listeners.slice():this._listeners=[],this._listeners.push({deleted:!1,boundTo:u,handler:a,event:i,once:h}),c},r.prototype.detach=function(){for(var t=[],l=0;l<arguments.length;l++)t[l]=arguments[l];this._detach.apply(this,t)},r.prototype._detach=function(){for(var t=[],l=0;l<arguments.length;l++)t[l]=arguments[l];if(!(!this._listeners||this._listeners.length===0)){var h,v,u;t.length>=1&&(typeof t[0]=="function"?v=t[0]:t.length===1&&typeof t[0].post=="function"?u=t[0]:h=t[0]),t.length>=2&&(v=t[1]),this._listeners=this._listeners.filter(function(a){return(typeof v>"u"||a.handler===v)&&(typeof u>"u"||a.event===u)&&(typeof h>"u"||a.boundTo===h)?(a.deleted=!0,!1):!0}),this._listeners.length===0&&delete this._listeners}},r.prototype.post=function(t){throw new Error("abstract")},r.prototype.listenerCount=function(){return this._listeners?this._listeners.length:0},r.prototype._call=function(t,l){this._listeners&&(t.deleted||(t.once&&(t.deleted=!0,this._listeners=this._listeners.filter(function(h){return h!==t}),this._listeners.length===0&&delete this._listeners),t.event?t.event.post.apply(t.event,l):t.handler&&t.handler.apply(typeof t.boundTo=="object"?t.boundTo:this,l)))},r})();return U.BaseEvent=e,U}var m={},fe;function N(){if(fe)return m;fe=1;var e=m&&m.__extends||(function(){var v=function(u,a){return v=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(i,c){i.__proto__=c}||function(i,c){for(var s in c)c.hasOwnProperty(s)&&(i[s]=c[s])},v(u,a)};return function(u,a){v(u,a);function i(){this.constructor=u}u.prototype=a===null?Object.create(a):(i.prototype=a.prototype,new i)}})();Object.defineProperty(m,"__esModule",{value:!0}),m.ErrorSyncEvent=m.VoidSyncEvent=m.SyncEvent=void 0;var r=K(),t=(function(v){e(u,v);function u(){var a=v!==null&&v.apply(this,arguments)||this;return a._recursion=0,a}return Object.defineProperty(u.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new l),this._listenersChanged},enumerable:!1,configurable:!0}),u.prototype.post=function(){for(var a=[],i=0;i<arguments.length;i++)a[i]=arguments[i];if(!(!this._listeners||this._listeners.length===0)){if(this._recursion++,typeof u.MAX_RECURSION_DEPTH=="number"&&Number.isInteger(u.MAX_RECURSION_DEPTH)&&u.MAX_RECURSION_DEPTH>0&&this._recursion>u.MAX_RECURSION_DEPTH)throw new Error("event fired recursively");for(var c=this._listeners,s=0;s<c.length;++s){var n=c[s];this._call(n,a)}this._recursion--}},u.prototype._attach=function(a,i,c){var s,n,o,f,d=(n=(s=this._listeners)===null||s===void 0?void 0:s.length)!==null&&n!==void 0?n:0,p=v.prototype._attach.call(this,a,i,c);return this.evtListenersChanged&&d!==((f=(o=this._listeners)===null||o===void 0?void 0:o.length)!==null&&f!==void 0?f:0)&&this.evtListenersChanged.post(),p},u.prototype._detach=function(){for(var a,i,c,s,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var f=(i=(a=this._listeners)===null||a===void 0?void 0:a.length)!==null&&i!==void 0?i:0,d=v.prototype._detach.apply(this,n);return this.evtListenersChanged&&f!==((s=(c=this._listeners)===null||c===void 0?void 0:c.length)!==null&&s!==void 0?s:0)&&this.evtListenersChanged.post(),d},u.MAX_RECURSION_DEPTH=10,u})(r.BaseEvent);m.SyncEvent=t;var l=(function(v){e(u,v);function u(){return v!==null&&v.apply(this,arguments)||this}return u.prototype.post=function(){v.prototype.post.call(this,void 0)},u})(t);m.VoidSyncEvent=l;var h=(function(v){e(u,v);function u(){return v!==null&&v.apply(this,arguments)||this}return u.prototype.post=function(a){if(this.listenerCount()===0)throw new Error("error event posted while no listeners attached. Error: "+a.message);v.prototype.post.call(this,a)},u})(t);return m.ErrorSyncEvent=h,m}var C={},$={},ve;function x(){if(ve)return $;ve=1,Object.defineProperty($,"__esModule",{value:!0});var e=N(),r=(function(){function t(){this.evtFilled=new e.SyncEvent,this.evtDrained=new e.SyncEvent,this._queue=[],this._flushing=!1}return t.global=function(){return t._instance||t.resetGlobal(),t._instance},t.resetGlobal=function(){t._instance=new t},t.prototype.empty=function(){return this._queue.length===0},t.prototype.add=function(l){this._queue.push(l),this._queue.length===1&&!this._flushing&&this.evtFilled.post(this)},t.prototype.flushOnce=function(){var l=this._queue.length===0,h=this._flushing;this._flushing=!0;try{var v=this._queue;this._queue=[];for(var u=0;u<v.length;++u)v[u]()}finally{this._flushing=h,!l&&!h&&this._queue.length===0&&this.evtDrained.post(this)}},t.prototype.flush=function(l){l===void 0&&(l=10);var h=this._queue.length===0,v=this._flushing;this._flushing=!0;try{for(var u=0;this._queue.length>0;){if(typeof l=="number"&&u>=l)throw this._queue=[],new Error("unable to flush the queue due to recursively added event. Clearing queue now");this.flushOnce(),++u}}finally{this._flushing=v,!h&&!v&&this._queue.length===0&&this.evtDrained.post(this)}},t})();return $.default=r,$}var de;function me(){if(de)return C;de=1;var e=C&&C.__extends||(function(){var a=function(i,c){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(s,n){s.__proto__=n}||function(s,n){for(var o in n)n.hasOwnProperty(o)&&(s[o]=n[o])},a(i,c)};return function(i,c){a(i,c);function s(){this.constructor=i}i.prototype=c===null?Object.create(c):(s.prototype=c.prototype,new s)}})();Object.defineProperty(C,"__esModule",{value:!0}),C.ErrorQueuedEvent=C.VoidQueuedEvent=C.QueuedEvent=void 0;var r=K(),t=x(),l=N(),h=(function(a){e(i,a);function i(c){c===void 0&&(c={});var s=a.call(this)||this;return s._queued=!1,s.options=c,typeof c.condensed=="boolean"?s._condensed=c.condensed:s._condensed=!1,typeof c.queue=="object"&&c.queue!==null&&(s._queue=c.queue),s}return Object.defineProperty(i.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new l.VoidSyncEvent),this._listenersChanged},enumerable:!1,configurable:!0}),i.prototype.post=function(){for(var c=this,s=[],n=0;n<arguments.length;n++)s[n]=arguments[n];if(!(!this._listeners||this._listeners.length===0)){var o=this._queue?this._queue:t.default.global();if(this._condensed){if(this._queuedData=s,this._queuedListeners=this._listeners,this._queued)return;this._queued=!0,o.add(function(){c._queued=!1;for(var d=c._queuedData,p=c._queuedListeners,g=0;g<p.length;++g){var _=p[g];c._call(_,d)}})}else{var f=this._listeners;o.add(function(){for(var d=0;d<f.length;++d){var p=f[d];c._call(p,s)}})}}},i.prototype._attach=function(c,s,n){var o,f,d,p,g=(f=(o=this._listeners)===null||o===void 0?void 0:o.length)!==null&&f!==void 0?f:0,_=a.prototype._attach.call(this,c,s,n);return this.evtListenersChanged&&g!==((p=(d=this._listeners)===null||d===void 0?void 0:d.length)!==null&&p!==void 0?p:0)&&this.evtListenersChanged.post(),_},i.prototype._detach=function(){for(var c,s,n,o,f=[],d=0;d<arguments.length;d++)f[d]=arguments[d];var p=(s=(c=this._listeners)===null||c===void 0?void 0:c.length)!==null&&s!==void 0?s:0,g=a.prototype._detach.apply(this,f);return this.evtListenersChanged&&p!==((o=(n=this._listeners)===null||n===void 0?void 0:n.length)!==null&&o!==void 0?o:0)&&this.evtListenersChanged.post(),g},i})(r.BaseEvent);C.QueuedEvent=h;var v=(function(a){e(i,a);function i(){return a!==null&&a.apply(this,arguments)||this}return i.prototype.post=function(){a.prototype.post.call(this,void 0)},i})(h);C.VoidQueuedEvent=v;var u=(function(a){e(i,a);function i(){return a!==null&&a.apply(this,arguments)||this}return i.prototype.post=function(c){if(!this._listeners||this._listeners.length===0)throw new Error("error event posted while no listeners attached. Error: "+c.message);a.prototype.post.call(this,c)},i})(h);return C.ErrorQueuedEvent=u,C}var A={},pe;function Ce(){if(pe)return A;pe=1;var e=A&&A.__extends||(function(){var u=function(a,i){return u=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(c,s){c.__proto__=s}||function(c,s){for(var n in s)s.hasOwnProperty(n)&&(c[n]=s[n])},u(a,i)};return function(a,i){u(a,i);function c(){this.constructor=a}a.prototype=i===null?Object.create(i):(c.prototype=i.prototype,new c)}})();Object.defineProperty(A,"__esModule",{value:!0}),A.ErrorAsyncEvent=A.VoidAsyncEvent=A.AsyncEvent=void 0;var r=K(),t=N(),l=(function(u){e(a,u);function a(i){i===void 0&&(i={});var c=u.call(this)||this;return c._queued=!1,c.options=i,typeof i.condensed=="boolean"?c._condensed=i.condensed:c._condensed=!1,c}return Object.defineProperty(a.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new t.VoidSyncEvent),this._listenersChanged},enumerable:!1,configurable:!0}),a.defaultScheduler=function(i){typeof window<"u"?setTimeout(i,0):setImmediate(i)},a.setScheduler=function(i){a._scheduler=i},a.prototype.post=function(){for(var i=this,c=[],s=0;s<arguments.length;s++)c[s]=arguments[s];if(!(!this._listeners||this._listeners.length===0))if(this._condensed){if(this._queuedData=c,this._queuedListeners=this._listeners,this._queued)return;this._queued=!0,a._scheduler(function(){i._queued=!1;for(var o=i._queuedData,f=i._queuedListeners,d=0;d<f.length;++d){var p=f[d];i._call(p,o)}})}else{var n=this._listeners;a._scheduler(function(){for(var o=0;o<n.length;++o){var f=n[o];i._call(f,c)}})}},a.prototype._call=function(i,c){i.event&&i.event instanceof a?i.event._postDirect(c):u.prototype._call.call(this,i,c)},a.prototype._postDirect=function(i){if(!(!this._listeners||this._listeners.length===0))for(var c=this._listeners,s=0;s<c.length;++s){var n=c[s];this._call(n,i)}},a.prototype._attach=function(i,c,s){var n,o,f,d,p=(o=(n=this._listeners)===null||n===void 0?void 0:n.length)!==null&&o!==void 0?o:0,g=u.prototype._attach.call(this,i,c,s);return this.evtListenersChanged&&p!==((d=(f=this._listeners)===null||f===void 0?void 0:f.length)!==null&&d!==void 0?d:0)&&this.evtListenersChanged.post(),g},a.prototype._detach=function(){for(var i,c,s,n,o=[],f=0;f<arguments.length;f++)o[f]=arguments[f];var d=(c=(i=this._listeners)===null||i===void 0?void 0:i.length)!==null&&c!==void 0?c:0,p=u.prototype._detach.apply(this,o);return this.evtListenersChanged&&d!==((n=(s=this._listeners)===null||s===void 0?void 0:s.length)!==null&&n!==void 0?n:0)&&this.evtListenersChanged.post(),p},a._scheduler=a.defaultScheduler,a})(r.BaseEvent);A.AsyncEvent=l;var h=(function(u){e(a,u);function a(){return u!==null&&u.apply(this,arguments)||this}return a.prototype.post=function(){u.prototype.post.call(this,void 0)},a})(l);A.VoidAsyncEvent=h;var v=(function(u){e(a,u);function a(){return u!==null&&u.apply(this,arguments)||this}return a.prototype.post=function(i){if(this.listenerCount()===0)throw new Error("error event posted while no listeners attached. Error: "+i.message);u.prototype.post.call(this,i)},a})(l);return A.ErrorAsyncEvent=v,A}var Q={},R={},ye;function Fe(){if(ye)return R;ye=1,Object.defineProperty(R,"__esModule",{value:!0}),R.shallowEquals=void 0;function e(r,t){if(r===t)return!0;if(typeof r!=typeof t)return!1;switch(typeof r){case"boolean":case"number":case"string":case"function":case"symbol":case"undefined":return!1;case"object":if(r===null||t===null)return!1;if(Array.isArray(r)||Array.isArray(t)){if(!Array.isArray(r)||!Array.isArray(t)||r.length!==t.length)return!1;for(var l=0;l<r.length;++l)if(r[l]!==t[l])return!1;return!0}var h=[],v=[];for(var u in r)r.hasOwnProperty(u)&&h.push(u);for(var a in t)t.hasOwnProperty(a)&&v.push(a);if(h.sort(),v.sort(),h.join(",")!==v.join(","))return!1;for(var l=0;l<h.length;++l)if(r[h[l]]!==t[h[l]])return!1;return!0;default:return!1}}return R.shallowEquals=e,R}var ge;function $e(){return ge||(ge=1,(function(e){var r=Q&&Q.__extends||(function(){var s=function(n,o){return s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(f,d){f.__proto__=d}||function(f,d){for(var p in d)d.hasOwnProperty(p)&&(f[p]=d[p])},s(n,o)};return function(n,o){s(n,o);function f(){this.constructor=n}n.prototype=o===null?Object.create(o):(f.prototype=o.prototype,new f)}})();Object.defineProperty(e,"__esModule",{value:!0}),e.ErrorAnyEvent=e.VoidAnyEvent=e.AnyEvent=e.EventType=void 0;var t=Fe(),l=N(),h=Ce(),v=me(),u;(function(s){s[s.Sync=0]="Sync",s[s.Async=1]="Async",s[s.Queued=2]="Queued"})(u=e.EventType||(e.EventType={}));var a=(function(){function s(n){this._events=[],n&&n.monitorAttach&&(this.evtFirstAttached=new i,this.evtLastDetached=new i)}return Object.defineProperty(s.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new l.VoidSyncEvent),this._listenersChanged},enumerable:!1,configurable:!0}),s.prototype.attach=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var f=u.Sync;n.length>0&&typeof n[0]=="number"&&(f=n.shift());var d=this,p,g,_;return typeof n[0]=="function"||n[0]&&typeof n[0]=="object"&&typeof n[0].post=="function"?(typeof n[0]=="function"?p=n[0]:_=n[0],g=n[1]):(d=n[0],p=n[1],g=n[2]),this._attach(f,d,p,_,g,!1)},s.prototype.once=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var f=u.Sync;n.length>0&&typeof n[0]=="number"&&(f=n.shift());var d=this,p,g,_;return typeof n[0]=="function"||n[0]&&typeof n[0]=="object"&&typeof n[0].post=="function"?(typeof n[0]=="function"?p=n[0]:_=n[0],g=n[1]):(d=n[0],p=n[1],g=n[2]),this._attach(f,d,p,_,g,!0)},s.prototype._attach=function(n,o,f,d,p,g){var _=this,ie=this.evtFirstAttached?this.listenerCount():0,w;switch(n){case u.Sync:{for(var z=0,ae=this._events;z<ae.length;z++){var S=ae[z];S instanceof l.SyncEvent&&(w=S)}w||(w=new l.SyncEvent,this._events.push(w))}break;case u.Async:{for(var W=0,ue=this._events;W<ue.length;W++){var S=ue[W];S instanceof h.AsyncEvent&&t.shallowEquals(S.options,p)&&(w=S)}w||(w=new h.AsyncEvent(p),this._events.push(w))}break;case u.Queued:{for(var J=0,ce=this._events;J<ce.length;J++){var S=ce[J];S instanceof v.QueuedEvent&&t.shallowEquals(S.options,p)&&(w=S)}w||(w=new v.QueuedEvent(p),this._events.push(w))}break;default:throw new Error("unknown EventType")}var B;return g?d?B=w.once(d):B=w.once(o,f):d?B=w.attach(d):B=w.attach(o,f),this.evtFirstAttached&&ie===0&&this.evtFirstAttached.post(),this.evtListenersChanged&&ie!==this.listenerCount()&&this.evtListenersChanged.post(),function(){var le=_.evtLastDetached?_.listenerCount():0;B(),_.evtLastDetached&&le>0&&_.listenerCount()===0&&_.evtLastDetached.post(),_.evtListenersChanged&&le!==_.listenerCount()&&_.evtListenersChanged.post()}},s.prototype.attachSync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Sync),this.attach.apply(this,n)},s.prototype.onceSync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Sync),this.once.apply(this,n)},s.prototype.attachAsync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Async),this.attach.apply(this,n)},s.prototype.onceAsync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Async),this.once.apply(this,n)},s.prototype.attachQueued=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Queued),this.attach.apply(this,n)},s.prototype.onceQueued=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Queued),this.once.apply(this,n)},s.prototype.detach=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];for(var f=this.listenerCount(),d=0;d<this._events.length;++d)this._events[d].detach.apply(this._events[d],n);this.evtListenersChanged&&f!==this.listenerCount()&&this.evtListenersChanged.post(),this.evtLastDetached&&f>0&&this.listenerCount()===0&&this.evtLastDetached.post()},s.prototype.post=function(n){for(var o=[],f=0;f<this._events.length;++f)o.push(this._events[f]);for(var f=0;f<o.length;++f)o[f].post(n)},s.prototype.listenerCount=function(){for(var n=0,o=0;o<this._events.length;++o)n+=this._events[o].listenerCount();return n},s})();e.AnyEvent=a;var i=(function(s){r(n,s);function n(){return s!==null&&s.apply(this,arguments)||this}return n.prototype.post=function(){s.prototype.post.call(this,void 0)},n})(a);e.VoidAnyEvent=i;var c=(function(s){r(n,s);function n(){return s!==null&&s.apply(this,arguments)||this}return n.prototype.post=function(o){if(this.listenerCount()===0)throw new Error("error event posted while no listeners attached. Error: "+o.message);s.prototype.post.call(this,o)},n})(a);e.ErrorAnyEvent=c})(Q)),Q}var _e;function He(){return _e||(_e=1,(function(e){var r=q&&q.__createBinding||(Object.create?(function(i,c,s,n){n===void 0&&(n=s),Object.defineProperty(i,n,{enumerable:!0,get:function(){return c[s]}})}):(function(i,c,s,n){n===void 0&&(n=s),i[n]=c[s]})),t=q&&q.__exportStar||function(i,c){for(var s in i)s!=="default"&&!c.hasOwnProperty(s)&&r(c,i,s)};Object.defineProperty(e,"__esModule",{value:!0}),e.flush=e.flushOnce=e.queue=void 0,t(K(),e),t(N(),e),t(me(),e),t(Ce(),e),t($e(),e);var l=x(),h=x();Object.defineProperty(e,"EventQueue",{enumerable:!0,get:function(){return h.default}});function v(){return l.default.global()}e.queue=v;function u(){l.default.global().flushOnce()}e.flushOnce=u;function a(i){i===void 0&&(i=10),l.default.global().flush(i)}e.flush=a})(q)),q}var E=He();let Ae=null;function Xe(e){Ae=e}function V(){return Ae}let Z=!1,b,te;const X=new Map,ne=new E.SyncEvent,Se=new E.SyncEvent;async function Ze(e){const r=V();if(r)return r.connect(e);try{if(b=await navigator.bluetooth.requestDevice(e),b.gatt===void 0)throw new Error("Device does not support GATT");te=await b.gatt.connect(),b.addEventListener("gattserverdisconnected",ee),Z=!0,ne.post()}catch(t){throw t}}async function Ke(){const e=V();if(e)return e.reconnect();if(Z)return;const r=await navigator.bluetooth.getDevices();if(r.length===0)throw new Error("No devices found");if(b=r[0],b.gatt===void 0)throw new Error("Device does not support GATT");te=await b.gatt.connect(),b.removeEventListener("gattserverdisconnected",ee),b.addEventListener("gattserverdisconnected",ee),Z=!0,ne.post()}function ee(){X.clear(),Z=!1,Se.post()}async function re(e,r){if(X.has(r)){const h=X.get(r);if(h===void 0)throw new Error("Characteristic not found");return h}const l=await(await te.getPrimaryService(e)).getCharacteristic(r);return X.set(r,l),l}async function Ye(e,r){const t=V();return t?t.readCharacteristic(e,r):await(await re(e,r)).readValue()}async function Ge(e,r,t){const l=V();if(l)return l.writeCharacteristic(e,r,t);const h=new DataView(new ArrayBuffer(t.length));for(let u=0;u<t.length;u++)h.setUint8(u,t[u]);await(await re(e,r)).writeValue(h)}async function ze(e,r,t){const l=V();if(l)return l.subscribe(e,r,t);const h=await re(e,r);h.addEventListener("characteristicvaluechanged",v=>{let u=v.target.value;u===void 0&&(u=new DataView(new ArrayBuffer(0))),t(u)}),await h.startNotifications()}function Y(){return{onConnect:ne,onDisconnect:Se,reconnect:Ke,connect:Ze,readCharacteristic:Ye,writeCharacteristic:Ge,subscribe:ze}}const be="19b10000-e8f2-537e-4f6c-d104768a1214",We="6b061bdc-9bc1-4952-a96f-c6ed551b2c3e",Je="6357e3bb-874c-4f35-b31e-2bcdf59d5419",xe="998ff920-81af-42a9-a915-f88025f9647d",et={filters:[{namePrefix:"BLE-STANDEE"}],optionalServices:[be]},tt=10,nt=13,rt=20,st=5e3,ot=3,it=2e3,at=5;function F(){return{serviceUuid:be,messageCharUuid:We,handshakeCharUuid:xe,messageFloodCharUuid:Je,options:et,TTL:tt,PACKET_SIZE:nt,BATCH_SIZE:rt,ACK_TIMEOUT:st,MAX_RETRIES:ot,RECONNECT_DELAY:it,RECONNECT_MAX_RETRIES:at}}class De{id;data;constructor(r,t){this.id=r,this.data=t}}const{serviceUuid:ut,messageCharUuid:ct}=F(),Te=new E.SyncEvent,qe=new E.SyncEvent,Oe=new E.SyncEvent;function lt(){return new Promise((e,r)=>{Y().subscribe(ut,ct,ht).then(()=>e()).catch(t=>r(t))})}function ht(e){console.log(e),e!==void 0&&e.getUint8(y.Data.TYPE)===y.DataType.BUTTON_PRESS&&ft(e)}function ft(e){const r=e.getUint8(y.Data.DATA),t=new De(e.getUint8(y.Data.ID),vt(e));r===y.Button.MINUS&&qe.post(t),r===y.Button.PLUS&&Te.post(t),r===y.Button.EITHER&&Oe.post(t)}function vt(e){const r=e.buffer,t=y.Data.DATA+1,l=e.byteLength-t;return Array.from(new Uint8Array(r,t,l)).map(h=>String.fromCharCode(h)).join("").replace(/\0/g,"").trim()}function dt(){return{subscribe:lt,onPlusButton:Te,onMinButton:qe,onEitherButton:Oe}}const{serviceUuid:pt,messageFloodCharUuid:yt}=F(),Pe=new E.SyncEvent,Le=new E.SyncEvent,Me=new E.SyncEvent,je=new E.SyncEvent;function gt(){return new Promise((e,r)=>{Y().subscribe(pt,yt,_t).then(()=>e()).catch(t=>r(t))})}function _t(e){if(console.log(e),e!==void 0&&(e.getUint8(y.Data.TYPE)===y.FloodTypes.NODES&&Le.post(Et(e)),e.getUint8(y.Data.TYPE)===y.FloodTypes.LOST_NODE&&Me.post(e.getUint8(y.Data.ID)),e.getUint8(y.Data.TYPE)===y.FloodTypes.MESSAGE_DONE&&Pe.post(e.getUint8(y.Data.DATA)),e.getUint8(y.Data.TYPE)===y.FloodTypes.DATA_FEEDBACK)){const r=[];for(let t=y.Data.DATA;t<e.byteLength;t++)r.push(e.getUint8(t));console.log("received packets"),console.log(r),je.post(r)}}function Et(e){const r=e.getUint8(y.Data.DATA),t=e.buffer.byteLength,l=[];for(let h=y.Data.DATA+1;h<t;h++)l.push(e.getUint8(h));return new Map([[r,l]])}function Be(){return{subscribe:gt,onMessageDone:Pe,onReceivedPackets:je,onNode:Le,onNodeLost:Me}}const k=[];function wt(){let e=Math.floor(Math.random()*256);for(;k.includes(e);)e=Math.floor(Math.random()*256);return e}function mt(e){k.push(e),Ct()}function Ct(){k.length<=20||k.splice(0,k.length-20)}function At(){const e=wt();return mt(e),e}function St(){return{create:At}}const{TTL:Ee,PACKET_SIZE:we}=F();function bt(e){if(e.data==null){let h=[e.id,e.cache,Ee,0,0,2,0,e.type,0];return[new Uint8Array(h)]}const r=new Uint8Array(2+e.data.length),t=e.data.length+2,l=[];r.set([e.type,t],0),r.set(e.data,2);for(let h=0;h<t;h+=we){const v=Math.min(we,t-h),u=new Uint8Array(v),a=[e.id,e.cache,Ee,h&255,h>>8&255,t&255,t>>8&255];u.set(r.subarray(h,h+v),0),l.push(new Uint8Array([...a,...u]))}return l}class Dt{cache=St().create();type;id;data;packets=[];resolve;constructor(r,t,l){this.type=r,this.id=t,this.data=l,this.packets=bt(this)}send(){return Ve().write(this)}then(r){return this.resolve=r,this}}const{serviceUuid:Tt,messageCharUuid:qt,BATCH_SIZE:Ot,ACK_TIMEOUT:Ue,MAX_RETRIES:H}=F(),{onMessageDone:Pt,onReceivedPackets:Lt}=Be(),Mt=Y(),D=[],G=new Map,I=new Map,P=new Map,T=new Map;let M=!1,O=0,L=!1;const Qe=new E.SyncEvent,Re=new E.SyncEvent,se=new E.SyncEvent,ke=new E.SyncEvent,Ie=new E.SyncEvent;Pt.attach(Bt);Lt.attach(Ut);function jt(e){return P.set(e.cache,e),e.packets.forEach(r=>D.push(r)),Re.post({cacheId:e.cache,type:e.type,targetId:e.id,packetCount:e.packets.length,totalBytes:e.data?.length??0,timestamp:Date.now()}),new Promise((r,t)=>{G.set(e.cache,()=>{e.resolve?.(),r()}),I.set(e.cache,t),T.set(e.cache,{timer:0,retries:0}),!M&&!L&&j()})}function j(){if(M||L)return;if(O>=Ot){L=!0;return}const e=D.shift();if(e==null){M=!1;return}const r=e[y.Data.CACHE];if(!P.has(r)){j();return}M=!0,oe(r),Qe.post({cacheId:r,position:e[y.Data.POSITION]|e[y.Data.POSITION+1]<<8,dataSize:e[y.Data.DATA_SIZE]|e[y.Data.DATA_SIZE+1]<<8,packetBytes:e.length,batchIndex:O,timestamp:Date.now()}),Mt.writeCharacteristic(Tt,qt,e).catch(t=>console.log(t)).finally(()=>{M=!1,O++,j()})}function Bt(e){Ie.post({cacheId:e,timestamp:Date.now()}),G.get(e)?.(),Ne(e),L=!1,O=0,j()}function Ut(e){O=0,L=!1;const r=new Set;for(const t of e){if(t===0||r.has(t))continue;r.add(t);const l=P.get(t);if(l==null)continue;const h=T.get(t);h!=null&&(h.retries=0,oe(t)),se.post({cacheId:t,packetCount:l.packets.length,reason:"feedback",timestamp:Date.now()}),l.packets.forEach(v=>D.unshift(v))}j()}function oe(e){const r=T.get(e);r!=null&&(clearTimeout(r.timer),r.timer=setTimeout(()=>{Rt(e)},Ue))}function Qt(e){const r=T.get(e);r!=null&&(clearTimeout(r.timer),T.delete(e))}function Rt(e){if(!P.has(e))return;const r=T.get(e);if(r==null)return;if(r.retries++,ke.post({cacheId:e,retryCount:r.retries,maxRetries:H,willRetry:r.retries<=H,timestamp:Date.now()}),r.retries>H){I.get(e)?.(new Error(`Buffer ${e} timed out after ${H} retries (${Ue}ms timeout)`)),Ne(e);return}const t=P.get(e);t!=null&&(se.post({cacheId:e,packetCount:t.packets.length,reason:"timeout",timestamp:Date.now()}),t.packets.forEach(l=>D.unshift(l))),oe(e),L=!1,O=0,j()}function Ne(e){G.delete(e),I.delete(e),P.delete(e),Qt(e);for(let r=D.length-1;r>=0;r--)D[r][y.Data.CACHE]===e&&D.splice(r,1)}function kt(e){for(const[,r]of I)r(e);G.clear(),I.clear();for(const r of T.values())clearTimeout(r.timer);T.clear(),P.clear(),D.length=0,M=!1,O=0,L=!1}function Ve(){return{write:jt,abortAll:kt,onPacketSent:Qe,onMessageQueued:Re,onRetransmit:se,onTimeout:ke,onMessageComplete:Ie}}class It{id;elements=[];onPlus=new E.SyncEvent;onMin=new E.SyncEvent;neighbors=[];constructor(r){this.id=r}name(){return this.id.toString(16).padStart(2,"0")}}exports.Buffer=Dt;exports.ButtonResult=De;exports.Standee=It;exports.libExports=E;exports.setBluetoothProvider=Xe;exports.useBluetooth=Y;exports.useBluetoothSettings=F;exports.useFloodCharacteristic=Be;exports.useMeshWriter=Ve;exports.useMessageCharacteristic=dt;
1
+ "use strict";const y=require("./enums.cjs");var q={},U={},he;function K(){if(he)return U;he=1,Object.defineProperty(U,"__esModule",{value:!0}),U.BaseEvent=void 0;var e=(function(){function r(){}return r.prototype.attach=function(t,l){return this._attach(t,l,!1)},r.prototype.once=function(t,l){return this._attach(t,l,!0)},r.prototype._attach=function(t,l,h){var v=this,u,a,i,c;if(typeof t=="function")a=t,c=function(){return v.detach(a)};else if(!l&&typeof t.post=="function")i=t,c=function(){return v.detach(i)};else{if(typeof t!="object"||t===void 0)throw new Error("Expect a function or object as first argument");if(typeof l!="function")throw new Error("Expect a function as second argument");u=t,a=l,c=function(){return v.detach(u,a)}}return this._listeners?this._listeners=this._listeners.slice():this._listeners=[],this._listeners.push({deleted:!1,boundTo:u,handler:a,event:i,once:h}),c},r.prototype.detach=function(){for(var t=[],l=0;l<arguments.length;l++)t[l]=arguments[l];this._detach.apply(this,t)},r.prototype._detach=function(){for(var t=[],l=0;l<arguments.length;l++)t[l]=arguments[l];if(!(!this._listeners||this._listeners.length===0)){var h,v,u;t.length>=1&&(typeof t[0]=="function"?v=t[0]:t.length===1&&typeof t[0].post=="function"?u=t[0]:h=t[0]),t.length>=2&&(v=t[1]),this._listeners=this._listeners.filter(function(a){return(typeof v>"u"||a.handler===v)&&(typeof u>"u"||a.event===u)&&(typeof h>"u"||a.boundTo===h)?(a.deleted=!0,!1):!0}),this._listeners.length===0&&delete this._listeners}},r.prototype.post=function(t){throw new Error("abstract")},r.prototype.listenerCount=function(){return this._listeners?this._listeners.length:0},r.prototype._call=function(t,l){this._listeners&&(t.deleted||(t.once&&(t.deleted=!0,this._listeners=this._listeners.filter(function(h){return h!==t}),this._listeners.length===0&&delete this._listeners),t.event?t.event.post.apply(t.event,l):t.handler&&t.handler.apply(typeof t.boundTo=="object"?t.boundTo:this,l)))},r})();return U.BaseEvent=e,U}var m={},fe;function V(){if(fe)return m;fe=1;var e=m&&m.__extends||(function(){var v=function(u,a){return v=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(i,c){i.__proto__=c}||function(i,c){for(var s in c)c.hasOwnProperty(s)&&(i[s]=c[s])},v(u,a)};return function(u,a){v(u,a);function i(){this.constructor=u}u.prototype=a===null?Object.create(a):(i.prototype=a.prototype,new i)}})();Object.defineProperty(m,"__esModule",{value:!0}),m.ErrorSyncEvent=m.VoidSyncEvent=m.SyncEvent=void 0;var r=K(),t=(function(v){e(u,v);function u(){var a=v!==null&&v.apply(this,arguments)||this;return a._recursion=0,a}return Object.defineProperty(u.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new l),this._listenersChanged},enumerable:!1,configurable:!0}),u.prototype.post=function(){for(var a=[],i=0;i<arguments.length;i++)a[i]=arguments[i];if(!(!this._listeners||this._listeners.length===0)){if(this._recursion++,typeof u.MAX_RECURSION_DEPTH=="number"&&Number.isInteger(u.MAX_RECURSION_DEPTH)&&u.MAX_RECURSION_DEPTH>0&&this._recursion>u.MAX_RECURSION_DEPTH)throw new Error("event fired recursively");for(var c=this._listeners,s=0;s<c.length;++s){var n=c[s];this._call(n,a)}this._recursion--}},u.prototype._attach=function(a,i,c){var s,n,o,f,d=(n=(s=this._listeners)===null||s===void 0?void 0:s.length)!==null&&n!==void 0?n:0,p=v.prototype._attach.call(this,a,i,c);return this.evtListenersChanged&&d!==((f=(o=this._listeners)===null||o===void 0?void 0:o.length)!==null&&f!==void 0?f:0)&&this.evtListenersChanged.post(),p},u.prototype._detach=function(){for(var a,i,c,s,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var f=(i=(a=this._listeners)===null||a===void 0?void 0:a.length)!==null&&i!==void 0?i:0,d=v.prototype._detach.apply(this,n);return this.evtListenersChanged&&f!==((s=(c=this._listeners)===null||c===void 0?void 0:c.length)!==null&&s!==void 0?s:0)&&this.evtListenersChanged.post(),d},u.MAX_RECURSION_DEPTH=10,u})(r.BaseEvent);m.SyncEvent=t;var l=(function(v){e(u,v);function u(){return v!==null&&v.apply(this,arguments)||this}return u.prototype.post=function(){v.prototype.post.call(this,void 0)},u})(t);m.VoidSyncEvent=l;var h=(function(v){e(u,v);function u(){return v!==null&&v.apply(this,arguments)||this}return u.prototype.post=function(a){if(this.listenerCount()===0)throw new Error("error event posted while no listeners attached. Error: "+a.message);v.prototype.post.call(this,a)},u})(t);return m.ErrorSyncEvent=h,m}var C={},$={},ve;function x(){if(ve)return $;ve=1,Object.defineProperty($,"__esModule",{value:!0});var e=V(),r=(function(){function t(){this.evtFilled=new e.SyncEvent,this.evtDrained=new e.SyncEvent,this._queue=[],this._flushing=!1}return t.global=function(){return t._instance||t.resetGlobal(),t._instance},t.resetGlobal=function(){t._instance=new t},t.prototype.empty=function(){return this._queue.length===0},t.prototype.add=function(l){this._queue.push(l),this._queue.length===1&&!this._flushing&&this.evtFilled.post(this)},t.prototype.flushOnce=function(){var l=this._queue.length===0,h=this._flushing;this._flushing=!0;try{var v=this._queue;this._queue=[];for(var u=0;u<v.length;++u)v[u]()}finally{this._flushing=h,!l&&!h&&this._queue.length===0&&this.evtDrained.post(this)}},t.prototype.flush=function(l){l===void 0&&(l=10);var h=this._queue.length===0,v=this._flushing;this._flushing=!0;try{for(var u=0;this._queue.length>0;){if(typeof l=="number"&&u>=l)throw this._queue=[],new Error("unable to flush the queue due to recursively added event. Clearing queue now");this.flushOnce(),++u}}finally{this._flushing=v,!h&&!v&&this._queue.length===0&&this.evtDrained.post(this)}},t})();return $.default=r,$}var de;function me(){if(de)return C;de=1;var e=C&&C.__extends||(function(){var a=function(i,c){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(s,n){s.__proto__=n}||function(s,n){for(var o in n)n.hasOwnProperty(o)&&(s[o]=n[o])},a(i,c)};return function(i,c){a(i,c);function s(){this.constructor=i}i.prototype=c===null?Object.create(c):(s.prototype=c.prototype,new s)}})();Object.defineProperty(C,"__esModule",{value:!0}),C.ErrorQueuedEvent=C.VoidQueuedEvent=C.QueuedEvent=void 0;var r=K(),t=x(),l=V(),h=(function(a){e(i,a);function i(c){c===void 0&&(c={});var s=a.call(this)||this;return s._queued=!1,s.options=c,typeof c.condensed=="boolean"?s._condensed=c.condensed:s._condensed=!1,typeof c.queue=="object"&&c.queue!==null&&(s._queue=c.queue),s}return Object.defineProperty(i.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new l.VoidSyncEvent),this._listenersChanged},enumerable:!1,configurable:!0}),i.prototype.post=function(){for(var c=this,s=[],n=0;n<arguments.length;n++)s[n]=arguments[n];if(!(!this._listeners||this._listeners.length===0)){var o=this._queue?this._queue:t.default.global();if(this._condensed){if(this._queuedData=s,this._queuedListeners=this._listeners,this._queued)return;this._queued=!0,o.add(function(){c._queued=!1;for(var d=c._queuedData,p=c._queuedListeners,g=0;g<p.length;++g){var _=p[g];c._call(_,d)}})}else{var f=this._listeners;o.add(function(){for(var d=0;d<f.length;++d){var p=f[d];c._call(p,s)}})}}},i.prototype._attach=function(c,s,n){var o,f,d,p,g=(f=(o=this._listeners)===null||o===void 0?void 0:o.length)!==null&&f!==void 0?f:0,_=a.prototype._attach.call(this,c,s,n);return this.evtListenersChanged&&g!==((p=(d=this._listeners)===null||d===void 0?void 0:d.length)!==null&&p!==void 0?p:0)&&this.evtListenersChanged.post(),_},i.prototype._detach=function(){for(var c,s,n,o,f=[],d=0;d<arguments.length;d++)f[d]=arguments[d];var p=(s=(c=this._listeners)===null||c===void 0?void 0:c.length)!==null&&s!==void 0?s:0,g=a.prototype._detach.apply(this,f);return this.evtListenersChanged&&p!==((o=(n=this._listeners)===null||n===void 0?void 0:n.length)!==null&&o!==void 0?o:0)&&this.evtListenersChanged.post(),g},i})(r.BaseEvent);C.QueuedEvent=h;var v=(function(a){e(i,a);function i(){return a!==null&&a.apply(this,arguments)||this}return i.prototype.post=function(){a.prototype.post.call(this,void 0)},i})(h);C.VoidQueuedEvent=v;var u=(function(a){e(i,a);function i(){return a!==null&&a.apply(this,arguments)||this}return i.prototype.post=function(c){if(!this._listeners||this._listeners.length===0)throw new Error("error event posted while no listeners attached. Error: "+c.message);a.prototype.post.call(this,c)},i})(h);return C.ErrorQueuedEvent=u,C}var A={},pe;function Ce(){if(pe)return A;pe=1;var e=A&&A.__extends||(function(){var u=function(a,i){return u=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(c,s){c.__proto__=s}||function(c,s){for(var n in s)s.hasOwnProperty(n)&&(c[n]=s[n])},u(a,i)};return function(a,i){u(a,i);function c(){this.constructor=a}a.prototype=i===null?Object.create(i):(c.prototype=i.prototype,new c)}})();Object.defineProperty(A,"__esModule",{value:!0}),A.ErrorAsyncEvent=A.VoidAsyncEvent=A.AsyncEvent=void 0;var r=K(),t=V(),l=(function(u){e(a,u);function a(i){i===void 0&&(i={});var c=u.call(this)||this;return c._queued=!1,c.options=i,typeof i.condensed=="boolean"?c._condensed=i.condensed:c._condensed=!1,c}return Object.defineProperty(a.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new t.VoidSyncEvent),this._listenersChanged},enumerable:!1,configurable:!0}),a.defaultScheduler=function(i){typeof window<"u"?setTimeout(i,0):setImmediate(i)},a.setScheduler=function(i){a._scheduler=i},a.prototype.post=function(){for(var i=this,c=[],s=0;s<arguments.length;s++)c[s]=arguments[s];if(!(!this._listeners||this._listeners.length===0))if(this._condensed){if(this._queuedData=c,this._queuedListeners=this._listeners,this._queued)return;this._queued=!0,a._scheduler(function(){i._queued=!1;for(var o=i._queuedData,f=i._queuedListeners,d=0;d<f.length;++d){var p=f[d];i._call(p,o)}})}else{var n=this._listeners;a._scheduler(function(){for(var o=0;o<n.length;++o){var f=n[o];i._call(f,c)}})}},a.prototype._call=function(i,c){i.event&&i.event instanceof a?i.event._postDirect(c):u.prototype._call.call(this,i,c)},a.prototype._postDirect=function(i){if(!(!this._listeners||this._listeners.length===0))for(var c=this._listeners,s=0;s<c.length;++s){var n=c[s];this._call(n,i)}},a.prototype._attach=function(i,c,s){var n,o,f,d,p=(o=(n=this._listeners)===null||n===void 0?void 0:n.length)!==null&&o!==void 0?o:0,g=u.prototype._attach.call(this,i,c,s);return this.evtListenersChanged&&p!==((d=(f=this._listeners)===null||f===void 0?void 0:f.length)!==null&&d!==void 0?d:0)&&this.evtListenersChanged.post(),g},a.prototype._detach=function(){for(var i,c,s,n,o=[],f=0;f<arguments.length;f++)o[f]=arguments[f];var d=(c=(i=this._listeners)===null||i===void 0?void 0:i.length)!==null&&c!==void 0?c:0,p=u.prototype._detach.apply(this,o);return this.evtListenersChanged&&d!==((n=(s=this._listeners)===null||s===void 0?void 0:s.length)!==null&&n!==void 0?n:0)&&this.evtListenersChanged.post(),p},a._scheduler=a.defaultScheduler,a})(r.BaseEvent);A.AsyncEvent=l;var h=(function(u){e(a,u);function a(){return u!==null&&u.apply(this,arguments)||this}return a.prototype.post=function(){u.prototype.post.call(this,void 0)},a})(l);A.VoidAsyncEvent=h;var v=(function(u){e(a,u);function a(){return u!==null&&u.apply(this,arguments)||this}return a.prototype.post=function(i){if(this.listenerCount()===0)throw new Error("error event posted while no listeners attached. Error: "+i.message);u.prototype.post.call(this,i)},a})(l);return A.ErrorAsyncEvent=v,A}var Q={},R={},ye;function Fe(){if(ye)return R;ye=1,Object.defineProperty(R,"__esModule",{value:!0}),R.shallowEquals=void 0;function e(r,t){if(r===t)return!0;if(typeof r!=typeof t)return!1;switch(typeof r){case"boolean":case"number":case"string":case"function":case"symbol":case"undefined":return!1;case"object":if(r===null||t===null)return!1;if(Array.isArray(r)||Array.isArray(t)){if(!Array.isArray(r)||!Array.isArray(t)||r.length!==t.length)return!1;for(var l=0;l<r.length;++l)if(r[l]!==t[l])return!1;return!0}var h=[],v=[];for(var u in r)r.hasOwnProperty(u)&&h.push(u);for(var a in t)t.hasOwnProperty(a)&&v.push(a);if(h.sort(),v.sort(),h.join(",")!==v.join(","))return!1;for(var l=0;l<h.length;++l)if(r[h[l]]!==t[h[l]])return!1;return!0;default:return!1}}return R.shallowEquals=e,R}var ge;function $e(){return ge||(ge=1,(function(e){var r=Q&&Q.__extends||(function(){var s=function(n,o){return s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(f,d){f.__proto__=d}||function(f,d){for(var p in d)d.hasOwnProperty(p)&&(f[p]=d[p])},s(n,o)};return function(n,o){s(n,o);function f(){this.constructor=n}n.prototype=o===null?Object.create(o):(f.prototype=o.prototype,new f)}})();Object.defineProperty(e,"__esModule",{value:!0}),e.ErrorAnyEvent=e.VoidAnyEvent=e.AnyEvent=e.EventType=void 0;var t=Fe(),l=V(),h=Ce(),v=me(),u;(function(s){s[s.Sync=0]="Sync",s[s.Async=1]="Async",s[s.Queued=2]="Queued"})(u=e.EventType||(e.EventType={}));var a=(function(){function s(n){this._events=[],n&&n.monitorAttach&&(this.evtFirstAttached=new i,this.evtLastDetached=new i)}return Object.defineProperty(s.prototype,"evtListenersChanged",{get:function(){return this._listenersChanged||(this._listenersChanged=new l.VoidSyncEvent),this._listenersChanged},enumerable:!1,configurable:!0}),s.prototype.attach=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var f=u.Sync;n.length>0&&typeof n[0]=="number"&&(f=n.shift());var d=this,p,g,_;return typeof n[0]=="function"||n[0]&&typeof n[0]=="object"&&typeof n[0].post=="function"?(typeof n[0]=="function"?p=n[0]:_=n[0],g=n[1]):(d=n[0],p=n[1],g=n[2]),this._attach(f,d,p,_,g,!1)},s.prototype.once=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var f=u.Sync;n.length>0&&typeof n[0]=="number"&&(f=n.shift());var d=this,p,g,_;return typeof n[0]=="function"||n[0]&&typeof n[0]=="object"&&typeof n[0].post=="function"?(typeof n[0]=="function"?p=n[0]:_=n[0],g=n[1]):(d=n[0],p=n[1],g=n[2]),this._attach(f,d,p,_,g,!0)},s.prototype._attach=function(n,o,f,d,p,g){var _=this,ie=this.evtFirstAttached?this.listenerCount():0,w;switch(n){case u.Sync:{for(var z=0,ae=this._events;z<ae.length;z++){var S=ae[z];S instanceof l.SyncEvent&&(w=S)}w||(w=new l.SyncEvent,this._events.push(w))}break;case u.Async:{for(var W=0,ue=this._events;W<ue.length;W++){var S=ue[W];S instanceof h.AsyncEvent&&t.shallowEquals(S.options,p)&&(w=S)}w||(w=new h.AsyncEvent(p),this._events.push(w))}break;case u.Queued:{for(var J=0,ce=this._events;J<ce.length;J++){var S=ce[J];S instanceof v.QueuedEvent&&t.shallowEquals(S.options,p)&&(w=S)}w||(w=new v.QueuedEvent(p),this._events.push(w))}break;default:throw new Error("unknown EventType")}var B;return g?d?B=w.once(d):B=w.once(o,f):d?B=w.attach(d):B=w.attach(o,f),this.evtFirstAttached&&ie===0&&this.evtFirstAttached.post(),this.evtListenersChanged&&ie!==this.listenerCount()&&this.evtListenersChanged.post(),function(){var le=_.evtLastDetached?_.listenerCount():0;B(),_.evtLastDetached&&le>0&&_.listenerCount()===0&&_.evtLastDetached.post(),_.evtListenersChanged&&le!==_.listenerCount()&&_.evtListenersChanged.post()}},s.prototype.attachSync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Sync),this.attach.apply(this,n)},s.prototype.onceSync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Sync),this.once.apply(this,n)},s.prototype.attachAsync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Async),this.attach.apply(this,n)},s.prototype.onceAsync=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Async),this.once.apply(this,n)},s.prototype.attachQueued=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Queued),this.attach.apply(this,n)},s.prototype.onceQueued=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return n.unshift(u.Queued),this.once.apply(this,n)},s.prototype.detach=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];for(var f=this.listenerCount(),d=0;d<this._events.length;++d)this._events[d].detach.apply(this._events[d],n);this.evtListenersChanged&&f!==this.listenerCount()&&this.evtListenersChanged.post(),this.evtLastDetached&&f>0&&this.listenerCount()===0&&this.evtLastDetached.post()},s.prototype.post=function(n){for(var o=[],f=0;f<this._events.length;++f)o.push(this._events[f]);for(var f=0;f<o.length;++f)o[f].post(n)},s.prototype.listenerCount=function(){for(var n=0,o=0;o<this._events.length;++o)n+=this._events[o].listenerCount();return n},s})();e.AnyEvent=a;var i=(function(s){r(n,s);function n(){return s!==null&&s.apply(this,arguments)||this}return n.prototype.post=function(){s.prototype.post.call(this,void 0)},n})(a);e.VoidAnyEvent=i;var c=(function(s){r(n,s);function n(){return s!==null&&s.apply(this,arguments)||this}return n.prototype.post=function(o){if(this.listenerCount()===0)throw new Error("error event posted while no listeners attached. Error: "+o.message);s.prototype.post.call(this,o)},n})(a);e.ErrorAnyEvent=c})(Q)),Q}var _e;function He(){return _e||(_e=1,(function(e){var r=q&&q.__createBinding||(Object.create?(function(i,c,s,n){n===void 0&&(n=s),Object.defineProperty(i,n,{enumerable:!0,get:function(){return c[s]}})}):(function(i,c,s,n){n===void 0&&(n=s),i[n]=c[s]})),t=q&&q.__exportStar||function(i,c){for(var s in i)s!=="default"&&!c.hasOwnProperty(s)&&r(c,i,s)};Object.defineProperty(e,"__esModule",{value:!0}),e.flush=e.flushOnce=e.queue=void 0,t(K(),e),t(V(),e),t(me(),e),t(Ce(),e),t($e(),e);var l=x(),h=x();Object.defineProperty(e,"EventQueue",{enumerable:!0,get:function(){return h.default}});function v(){return l.default.global()}e.queue=v;function u(){l.default.global().flushOnce()}e.flushOnce=u;function a(i){i===void 0&&(i=10),l.default.global().flush(i)}e.flush=a})(q)),q}var E=He();let Ae=null;function Xe(e){Ae=e}function k(){return Ae}let Z=!1,b,te;const X=new Map,ne=new E.SyncEvent,Se=new E.SyncEvent;async function Ze(e){const r=k();if(r)return r.connect(e);try{if(b=await navigator.bluetooth.requestDevice(e),b.gatt===void 0)throw new Error("Device does not support GATT");te=await b.gatt.connect(),b.addEventListener("gattserverdisconnected",ee),Z=!0,ne.post()}catch(t){throw t}}async function Ke(){const e=k();if(e)return e.reconnect();if(Z)return;const r=await navigator.bluetooth.getDevices();if(r.length===0)throw new Error("No devices found");if(b=r[0],b.gatt===void 0)throw new Error("Device does not support GATT");te=await b.gatt.connect(),b.removeEventListener("gattserverdisconnected",ee),b.addEventListener("gattserverdisconnected",ee),Z=!0,ne.post()}function ee(){X.clear(),Z=!1,Se.post()}async function re(e,r){if(X.has(r)){const h=X.get(r);if(h===void 0)throw new Error("Characteristic not found");return h}const l=await(await te.getPrimaryService(e)).getCharacteristic(r);return X.set(r,l),l}async function Ye(e,r){const t=k();return t?t.readCharacteristic(e,r):await(await re(e,r)).readValue()}async function Ge(e,r,t){const l=k();if(l)return l.writeCharacteristic(e,r,t);const h=new DataView(new ArrayBuffer(t.length));for(let u=0;u<t.length;u++)h.setUint8(u,t[u]);await(await re(e,r)).writeValue(h)}async function ze(e,r,t){const l=k();if(l)return l.subscribe(e,r,t);const h=await re(e,r);h.addEventListener("characteristicvaluechanged",v=>{let u=v.target.value;u===void 0&&(u=new DataView(new ArrayBuffer(0))),t(u)}),await h.startNotifications()}function Y(){return{onConnect:ne,onDisconnect:Se,reconnect:Ke,connect:Ze,readCharacteristic:Ye,writeCharacteristic:Ge,subscribe:ze}}const be="19b10000-e8f2-537e-4f6c-d104768a1214",We="6b061bdc-9bc1-4952-a96f-c6ed551b2c3e",Je="6357e3bb-874c-4f35-b31e-2bcdf59d5419",xe="998ff920-81af-42a9-a915-f88025f9647d",et={filters:[{namePrefix:"BLE-STANDEE"}],optionalServices:[be]},tt=10,nt=13,rt=20,st=5e3,ot=3,it=2e3,at=5;function F(){return{serviceUuid:be,messageCharUuid:We,handshakeCharUuid:xe,messageFloodCharUuid:Je,options:et,TTL:tt,PACKET_SIZE:nt,BATCH_SIZE:rt,ACK_TIMEOUT:st,MAX_RETRIES:ot,RECONNECT_DELAY:it,RECONNECT_MAX_RETRIES:at}}class De{id;data;constructor(r,t){this.id=r,this.data=t}}const{serviceUuid:ut,messageCharUuid:ct}=F(),Te=new E.SyncEvent,qe=new E.SyncEvent,Oe=new E.SyncEvent;function lt(){return new Promise((e,r)=>{Y().subscribe(ut,ct,ht).then(()=>e()).catch(t=>r(t))})}function ht(e){console.log(e),e!==void 0&&e.getUint8(y.Data.TYPE)===y.DataType.BUTTON_PRESS&&Pe(e)}function Pe(e){const r=e.getUint8(y.Data.DATA),t=new De(e.getUint8(y.Data.ID),ft(e));r===y.Button.MINUS&&qe.post(t),r===y.Button.PLUS&&Te.post(t),r===y.Button.EITHER&&Oe.post(t)}function ft(e){const r=e.buffer,t=y.Data.DATA+1,l=e.byteLength-t;return Array.from(new Uint8Array(r,t,l)).map(h=>String.fromCharCode(h)).join("").replace(/\0/g,"").trim()}function vt(){return{subscribe:lt,onPlusButton:Te,onMinButton:qe,onEitherButton:Oe}}const{serviceUuid:dt,messageFloodCharUuid:pt}=F(),Le=new E.SyncEvent,Me=new E.SyncEvent,je=new E.SyncEvent,yt=new E.SyncEvent;function gt(){return new Promise((e,r)=>{Y().subscribe(dt,pt,_t).then(()=>e()).catch(t=>r(t))})}function _t(e){console.log(e),e!==void 0&&(e.getUint8(y.Data.TYPE)===y.FloodTypes.NODES&&Me.post(Et(e)),e.getUint8(y.Data.TYPE)===y.FloodTypes.LOST_NODE&&je.post(e.getUint8(y.Data.ID)),e.getUint8(y.Data.TYPE)===y.FloodTypes.MESSAGE_DONE&&Le.post(e.getUint8(y.Data.DATA)),e.getUint8(y.Data.TYPE)===y.FloodTypes.DATA_FEEDBACK&&Pe(e))}function Et(e){const r=e.getUint8(y.Data.DATA),t=e.buffer.byteLength,l=[];for(let h=y.Data.DATA+1;h<t;h++)l.push(e.getUint8(h));return new Map([[r,l]])}function Be(){return{subscribe:gt,onMessageDone:Le,onReceivedPackets:yt,onNode:Me,onNodeLost:je}}const I=[];function wt(){let e=Math.floor(Math.random()*256);for(;I.includes(e);)e=Math.floor(Math.random()*256);return e}function mt(e){I.push(e),Ct()}function Ct(){I.length<=20||I.splice(0,I.length-20)}function At(){const e=wt();return mt(e),e}function St(){return{create:At}}const{TTL:Ee,PACKET_SIZE:we}=F();function bt(e){if(e.data==null){let h=[e.id,e.cache,Ee,0,0,2,0,e.type,0];return[new Uint8Array(h)]}const r=new Uint8Array(2+e.data.length),t=e.data.length+2,l=[];r.set([e.type,t],0),r.set(e.data,2);for(let h=0;h<t;h+=we){const v=Math.min(we,t-h),u=new Uint8Array(v),a=[e.id,e.cache,Ee,h&255,h>>8&255,t&255,t>>8&255];u.set(r.subarray(h,h+v),0),l.push(new Uint8Array([...a,...u]))}return l}class Dt{cache=St().create();type;id;data;packets=[];resolve;constructor(r,t,l){this.type=r,this.id=t,this.data=l,this.packets=bt(this)}send(){return ke().write(this)}then(r){return this.resolve=r,this}}const{serviceUuid:Tt,messageCharUuid:qt,BATCH_SIZE:Ot,ACK_TIMEOUT:Ue,MAX_RETRIES:H}=F(),{onMessageDone:Pt,onReceivedPackets:Lt}=Be(),Mt=Y(),D=[],G=new Map,N=new Map,P=new Map,T=new Map;let M=!1,O=0,L=!1;const Qe=new E.SyncEvent,Re=new E.SyncEvent,se=new E.SyncEvent,Ie=new E.SyncEvent,Ne=new E.SyncEvent;Pt.attach(Bt);Lt.attach(Ut);function jt(e){return P.set(e.cache,e),e.packets.forEach(r=>D.push(r)),Re.post({cacheId:e.cache,type:e.type,targetId:e.id,packetCount:e.packets.length,totalBytes:e.data?.length??0,timestamp:Date.now()}),new Promise((r,t)=>{G.set(e.cache,()=>{e.resolve?.(),r()}),N.set(e.cache,t),T.set(e.cache,{timer:0,retries:0}),!M&&!L&&j()})}function j(){if(M||L)return;if(O>=Ot){L=!0;return}const e=D.shift();if(e==null){M=!1;return}const r=e[y.Data.CACHE];if(!P.has(r)){j();return}M=!0,oe(r),Qe.post({cacheId:r,position:e[y.Data.POSITION]|e[y.Data.POSITION+1]<<8,dataSize:e[y.Data.DATA_SIZE]|e[y.Data.DATA_SIZE+1]<<8,packetBytes:e.length,batchIndex:O,timestamp:Date.now()}),Mt.writeCharacteristic(Tt,qt,e).catch(t=>console.log(t)).finally(()=>{M=!1,O++,j()})}function Bt(e){Ne.post({cacheId:e,timestamp:Date.now()}),G.get(e)?.(),Ve(e),L=!1,O=0,j()}function Ut(e){O=0,L=!1;const r=new Set;for(const t of e){if(t===0||r.has(t))continue;r.add(t);const l=P.get(t);if(l==null)continue;const h=T.get(t);h!=null&&(h.retries=0,oe(t)),se.post({cacheId:t,packetCount:l.packets.length,reason:"feedback",timestamp:Date.now()}),l.packets.forEach(v=>D.unshift(v))}j()}function oe(e){const r=T.get(e);r!=null&&(clearTimeout(r.timer),r.timer=setTimeout(()=>{Rt(e)},Ue))}function Qt(e){const r=T.get(e);r!=null&&(clearTimeout(r.timer),T.delete(e))}function Rt(e){if(!P.has(e))return;const r=T.get(e);if(r==null)return;if(r.retries++,Ie.post({cacheId:e,retryCount:r.retries,maxRetries:H,willRetry:r.retries<=H,timestamp:Date.now()}),r.retries>H){N.get(e)?.(new Error(`Buffer ${e} timed out after ${H} retries (${Ue}ms timeout)`)),Ve(e);return}const t=P.get(e);t!=null&&(se.post({cacheId:e,packetCount:t.packets.length,reason:"timeout",timestamp:Date.now()}),t.packets.forEach(l=>D.unshift(l))),oe(e),L=!1,O=0,j()}function Ve(e){G.delete(e),N.delete(e),P.delete(e),Qt(e);for(let r=D.length-1;r>=0;r--)D[r][y.Data.CACHE]===e&&D.splice(r,1)}function It(e){for(const[,r]of N)r(e);G.clear(),N.clear();for(const r of T.values())clearTimeout(r.timer);T.clear(),P.clear(),D.length=0,M=!1,O=0,L=!1}function ke(){return{write:jt,abortAll:It,onPacketSent:Qe,onMessageQueued:Re,onRetransmit:se,onTimeout:Ie,onMessageComplete:Ne}}class Nt{id;elements=[];onPlus=new E.SyncEvent;onMin=new E.SyncEvent;neighbors=[];constructor(r){this.id=r}name(){return this.id.toString(16).padStart(2,"0")}}exports.Buffer=Dt;exports.ButtonResult=De;exports.Standee=Nt;exports.libExports=E;exports.setBluetoothProvider=Xe;exports.useBluetooth=Y;exports.useBluetoothSettings=F;exports.useFloodCharacteristic=Be;exports.useMeshWriter=ke;exports.useMessageCharacteristic=vt;
@@ -1,4 +1,4 @@
1
- import { Data as g, DataType as Ve, Button as x, FloodTypes as $ } from "./enums.js";
1
+ import { Data as _, Button as x, DataType as ke, FloodTypes as $ } from "./enums.js";
2
2
  var O = {}, B = {}, ve;
3
3
  function K() {
4
4
  if (ve) return B;
@@ -65,7 +65,7 @@ function K() {
65
65
  return B.BaseEvent = e, B;
66
66
  }
67
67
  var m = {}, de;
68
- function N() {
68
+ function V() {
69
69
  if (de) return m;
70
70
  de = 1;
71
71
  var e = m && m.__extends || /* @__PURE__ */ (function() {
@@ -159,7 +159,7 @@ var C = {}, F = {}, pe;
159
159
  function ee() {
160
160
  if (pe) return F;
161
161
  pe = 1, Object.defineProperty(F, "__esModule", { value: !0 });
162
- var e = N(), r = (
162
+ var e = V(), r = (
163
163
  /** @class */
164
164
  (function() {
165
165
  function t() {
@@ -223,7 +223,7 @@ function Ae() {
223
223
  };
224
224
  })();
225
225
  Object.defineProperty(C, "__esModule", { value: !0 }), C.ErrorQueuedEvent = C.VoidQueuedEvent = C.QueuedEvent = void 0;
226
- var r = K(), t = ee(), l = N(), h = (
226
+ var r = K(), t = ee(), l = V(), h = (
227
227
  /** @class */
228
228
  (function(a) {
229
229
  e(i, a);
@@ -252,8 +252,8 @@ function Ae() {
252
252
  this._queued = !0, o.add(function() {
253
253
  c._queued = !1;
254
254
  for (var d = c._queuedData, p = c._queuedListeners, y = 0; y < p.length; ++y) {
255
- var _ = p[y];
256
- c._call(_, d);
255
+ var g = p[y];
256
+ c._call(g, d);
257
257
  }
258
258
  });
259
259
  } else {
@@ -267,8 +267,8 @@ function Ae() {
267
267
  }
268
268
  }
269
269
  }, i.prototype._attach = function(c, s, n) {
270
- var o, f, d, p, y = (f = (o = this._listeners) === null || o === void 0 ? void 0 : o.length) !== null && f !== void 0 ? f : 0, _ = a.prototype._attach.call(this, c, s, n);
271
- return this.evtListenersChanged && y !== ((p = (d = this._listeners) === null || d === void 0 ? void 0 : d.length) !== null && p !== void 0 ? p : 0) && this.evtListenersChanged.post(), _;
270
+ var o, f, d, p, y = (f = (o = this._listeners) === null || o === void 0 ? void 0 : o.length) !== null && f !== void 0 ? f : 0, g = a.prototype._attach.call(this, c, s, n);
271
+ return this.evtListenersChanged && y !== ((p = (d = this._listeners) === null || d === void 0 ? void 0 : d.length) !== null && p !== void 0 ? p : 0) && this.evtListenersChanged.post(), g;
272
272
  }, i.prototype._detach = function() {
273
273
  for (var c, s, n, o, f = [], d = 0; d < arguments.length; d++)
274
274
  f[d] = arguments[d];
@@ -328,7 +328,7 @@ function Se() {
328
328
  };
329
329
  })();
330
330
  Object.defineProperty(A, "__esModule", { value: !0 }), A.ErrorAsyncEvent = A.VoidAsyncEvent = A.AsyncEvent = void 0;
331
- var r = K(), t = N(), l = (
331
+ var r = K(), t = V(), l = (
332
332
  /** @class */
333
333
  (function(u) {
334
334
  e(a, u);
@@ -487,7 +487,7 @@ function Fe() {
487
487
  };
488
488
  })();
489
489
  Object.defineProperty(e, "__esModule", { value: !0 }), e.ErrorAnyEvent = e.VoidAnyEvent = e.AnyEvent = e.EventType = void 0;
490
- var t = $e(), l = N(), h = Se(), v = Ae(), u;
490
+ var t = $e(), l = V(), h = Se(), v = Ae(), u;
491
491
  (function(s) {
492
492
  s[s.Sync = 0] = "Sync", s[s.Async = 1] = "Async", s[s.Queued = 2] = "Queued";
493
493
  })(u = e.EventType || (e.EventType = {}));
@@ -511,17 +511,17 @@ function Fe() {
511
511
  n[o] = arguments[o];
512
512
  var f = u.Sync;
513
513
  n.length > 0 && typeof n[0] == "number" && (f = n.shift());
514
- var d = this, p, y, _;
515
- return typeof n[0] == "function" || n[0] && typeof n[0] == "object" && typeof n[0].post == "function" ? (typeof n[0] == "function" ? p = n[0] : _ = n[0], y = n[1]) : (d = n[0], p = n[1], y = n[2]), this._attach(f, d, p, _, y, !1);
514
+ var d = this, p, y, g;
515
+ return typeof n[0] == "function" || n[0] && typeof n[0] == "object" && typeof n[0].post == "function" ? (typeof n[0] == "function" ? p = n[0] : g = n[0], y = n[1]) : (d = n[0], p = n[1], y = n[2]), this._attach(f, d, p, g, y, !1);
516
516
  }, s.prototype.once = function() {
517
517
  for (var n = [], o = 0; o < arguments.length; o++)
518
518
  n[o] = arguments[o];
519
519
  var f = u.Sync;
520
520
  n.length > 0 && typeof n[0] == "number" && (f = n.shift());
521
- var d = this, p, y, _;
522
- return typeof n[0] == "function" || n[0] && typeof n[0] == "object" && typeof n[0].post == "function" ? (typeof n[0] == "function" ? p = n[0] : _ = n[0], y = n[1]) : (d = n[0], p = n[1], y = n[2]), this._attach(f, d, p, _, y, !0);
521
+ var d = this, p, y, g;
522
+ return typeof n[0] == "function" || n[0] && typeof n[0] == "object" && typeof n[0].post == "function" ? (typeof n[0] == "function" ? p = n[0] : g = n[0], y = n[1]) : (d = n[0], p = n[1], y = n[2]), this._attach(f, d, p, g, y, !0);
523
523
  }, s.prototype._attach = function(n, o, f, d, p, y) {
524
- var _ = this, ue = this.evtFirstAttached ? this.listenerCount() : 0, E;
524
+ var g = this, ue = this.evtFirstAttached ? this.listenerCount() : 0, E;
525
525
  switch (n) {
526
526
  case u.Sync:
527
527
  {
@@ -555,8 +555,8 @@ function Fe() {
555
555
  }
556
556
  var U;
557
557
  return y ? d ? U = E.once(d) : U = E.once(o, f) : d ? U = E.attach(d) : U = E.attach(o, f), this.evtFirstAttached && ue === 0 && this.evtFirstAttached.post(), this.evtListenersChanged && ue !== this.listenerCount() && this.evtListenersChanged.post(), function() {
558
- var fe = _.evtLastDetached ? _.listenerCount() : 0;
559
- U(), _.evtLastDetached && fe > 0 && _.listenerCount() === 0 && _.evtLastDetached.post(), _.evtListenersChanged && fe !== _.listenerCount() && _.evtListenersChanged.post();
558
+ var fe = g.evtLastDetached ? g.listenerCount() : 0;
559
+ U(), g.evtLastDetached && fe > 0 && g.listenerCount() === 0 && g.evtLastDetached.post(), g.evtListenersChanged && fe !== g.listenerCount() && g.evtListenersChanged.post();
560
560
  };
561
561
  }, s.prototype.attachSync = function() {
562
562
  for (var n = [], o = 0; o < arguments.length; o++)
@@ -643,7 +643,7 @@ function He() {
643
643
  })), t = O && O.__exportStar || function(i, c) {
644
644
  for (var s in i) s !== "default" && !c.hasOwnProperty(s) && r(c, i, s);
645
645
  };
646
- Object.defineProperty(e, "__esModule", { value: !0 }), e.flush = e.flushOnce = e.queue = void 0, t(K(), e), t(N(), e), t(Ae(), e), t(Se(), e), t(Fe(), e);
646
+ Object.defineProperty(e, "__esModule", { value: !0 }), e.flush = e.flushOnce = e.queue = void 0, t(K(), e), t(V(), e), t(Ae(), e), t(Se(), e), t(Fe(), e);
647
647
  var l = ee(), h = ee();
648
648
  Object.defineProperty(e, "EventQueue", { enumerable: !0, get: function() {
649
649
  return h.default;
@@ -664,16 +664,16 @@ function He() {
664
664
  }
665
665
  var w = He();
666
666
  let be = null;
667
- function Nt(e) {
667
+ function Vt(e) {
668
668
  be = e;
669
669
  }
670
- function V() {
670
+ function k() {
671
671
  return be;
672
672
  }
673
673
  let Z = !1, b, ne;
674
674
  const X = /* @__PURE__ */ new Map(), re = new w.SyncEvent(), Te = new w.SyncEvent();
675
675
  async function Xe(e) {
676
- const r = V();
676
+ const r = k();
677
677
  if (r) return r.connect(e);
678
678
  try {
679
679
  if (b = await navigator.bluetooth.requestDevice(e), b.gatt === void 0)
@@ -684,7 +684,7 @@ async function Xe(e) {
684
684
  }
685
685
  }
686
686
  async function Ze() {
687
- const e = V();
687
+ const e = k();
688
688
  if (e) return e.reconnect();
689
689
  if (Z)
690
690
  return;
@@ -709,11 +709,11 @@ async function se(e, r) {
709
709
  return X.set(r, l), l;
710
710
  }
711
711
  async function Ke(e, r) {
712
- const t = V();
712
+ const t = k();
713
713
  return t ? t.readCharacteristic(e, r) : await (await se(e, r)).readValue();
714
714
  }
715
715
  async function Ye(e, r, t) {
716
- const l = V();
716
+ const l = k();
717
717
  if (l) return l.writeCharacteristic(e, r, t);
718
718
  const h = new DataView(new ArrayBuffer(t.length));
719
719
  for (let u = 0; u < t.length; u++)
@@ -721,7 +721,7 @@ async function Ye(e, r, t) {
721
721
  await (await se(e, r)).writeValue(h);
722
722
  }
723
723
  async function Ge(e, r, t) {
724
- const l = V();
724
+ const l = k();
725
725
  if (l) return l.subscribe(e, r, t);
726
726
  const h = await se(e, r);
727
727
  h.addEventListener(
@@ -777,20 +777,20 @@ function lt() {
777
777
  });
778
778
  }
779
779
  function ht(e) {
780
- console.log(e), e !== void 0 && e.getUint8(g.TYPE) === Ve.BUTTON_PRESS && ft(e);
780
+ console.log(e), e !== void 0 && e.getUint8(_.TYPE) === ke.BUTTON_PRESS && Le(e);
781
781
  }
782
- function ft(e) {
783
- const r = e.getUint8(g.DATA), t = new at(
784
- e.getUint8(g.ID),
785
- vt(e)
782
+ function Le(e) {
783
+ const r = e.getUint8(_.DATA), t = new at(
784
+ e.getUint8(_.ID),
785
+ ft(e)
786
786
  );
787
787
  r === x.MINUS && De.post(t), r === x.PLUS && Oe.post(t), r === x.EITHER && Pe.post(t);
788
788
  }
789
- function vt(e) {
790
- const r = e.buffer, t = g.DATA + 1, l = e.byteLength - t;
789
+ function ft(e) {
790
+ const r = e.buffer, t = _.DATA + 1, l = e.byteLength - t;
791
791
  return Array.from(new Uint8Array(r, t, l)).map((h) => String.fromCharCode(h)).join("").replace(/\0/g, "").trim();
792
792
  }
793
- function Vt() {
793
+ function kt() {
794
794
  return {
795
795
  subscribe: lt,
796
796
  onPlusButton: Oe,
@@ -798,46 +798,41 @@ function Vt() {
798
798
  onEitherButton: Pe
799
799
  };
800
800
  }
801
- const { serviceUuid: dt, messageFloodCharUuid: pt } = Y(), Le = new w.SyncEvent(), Me = new w.SyncEvent(), je = new w.SyncEvent(), Ue = new w.SyncEvent();
801
+ const { serviceUuid: vt, messageFloodCharUuid: dt } = Y(), Me = new w.SyncEvent(), je = new w.SyncEvent(), Ue = new w.SyncEvent(), pt = new w.SyncEvent();
802
802
  function yt() {
803
803
  return new Promise((e, r) => {
804
- oe().subscribe(dt, pt, gt).then(() => e()).catch((t) => r(t));
804
+ oe().subscribe(vt, dt, gt).then(() => e()).catch((t) => r(t));
805
805
  });
806
806
  }
807
807
  function gt(e) {
808
- if (console.log(e), e !== void 0 && (e.getUint8(g.TYPE) === $.NODES && Me.post(_t(e)), e.getUint8(g.TYPE) === $.LOST_NODE && je.post(e.getUint8(g.ID)), e.getUint8(g.TYPE) === $.MESSAGE_DONE && Le.post(e.getUint8(g.DATA)), e.getUint8(g.TYPE) === $.DATA_FEEDBACK)) {
809
- const r = [];
810
- for (let t = g.DATA; t < e.byteLength; t++)
811
- r.push(e.getUint8(t));
812
- console.log("received packets"), console.log(r), Ue.post(r);
813
- }
808
+ console.log(e), e !== void 0 && (e.getUint8(_.TYPE) === $.NODES && je.post(_t(e)), e.getUint8(_.TYPE) === $.LOST_NODE && Ue.post(e.getUint8(_.ID)), e.getUint8(_.TYPE) === $.MESSAGE_DONE && Me.post(e.getUint8(_.DATA)), e.getUint8(_.TYPE) === $.DATA_FEEDBACK && Le(e));
814
809
  }
815
810
  function _t(e) {
816
- const r = e.getUint8(g.DATA), t = e.buffer.byteLength, l = [];
817
- for (let h = g.DATA + 1; h < t; h++)
811
+ const r = e.getUint8(_.DATA), t = e.buffer.byteLength, l = [];
812
+ for (let h = _.DATA + 1; h < t; h++)
818
813
  l.push(e.getUint8(h));
819
814
  return /* @__PURE__ */ new Map([[r, l]]);
820
815
  }
821
816
  function Et() {
822
817
  return {
823
818
  subscribe: yt,
824
- onMessageDone: Le,
825
- onReceivedPackets: Ue,
826
- onNode: Me,
827
- onNodeLost: je
819
+ onMessageDone: Me,
820
+ onReceivedPackets: pt,
821
+ onNode: je,
822
+ onNodeLost: Ue
828
823
  };
829
824
  }
830
- const k = [];
825
+ const I = [];
831
826
  function wt() {
832
827
  let e = Math.floor(Math.random() * 256);
833
- for (; k.includes(e); ) e = Math.floor(Math.random() * 256);
828
+ for (; I.includes(e); ) e = Math.floor(Math.random() * 256);
834
829
  return e;
835
830
  }
836
831
  function mt(e) {
837
- k.push(e), Ct();
832
+ I.push(e), Ct();
838
833
  }
839
834
  function Ct() {
840
- k.length <= 20 || k.splice(0, k.length - 20);
835
+ I.length <= 20 || I.splice(0, I.length - 20);
841
836
  }
842
837
  function At() {
843
838
  const e = wt();
@@ -881,15 +876,15 @@ class $t {
881
876
  this.type = r, this.id = t, this.data = l, this.packets = bt(this);
882
877
  }
883
878
  send() {
884
- return kt().write(this);
879
+ return It().write(this);
885
880
  }
886
881
  then(r) {
887
882
  return this.resolve = r, this;
888
883
  }
889
884
  }
890
- const { serviceUuid: Tt, messageCharUuid: qt, BATCH_SIZE: Ot, ACK_TIMEOUT: Be, MAX_RETRIES: H } = Y(), { onMessageDone: Dt, onReceivedPackets: Pt } = Et(), Lt = oe(), T = [], G = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(), P = /* @__PURE__ */ new Map(), q = /* @__PURE__ */ new Map();
885
+ const { serviceUuid: Tt, messageCharUuid: qt, BATCH_SIZE: Ot, ACK_TIMEOUT: Be, MAX_RETRIES: H } = Y(), { onMessageDone: Dt, onReceivedPackets: Pt } = Et(), Lt = oe(), T = [], G = /* @__PURE__ */ new Map(), N = /* @__PURE__ */ new Map(), P = /* @__PURE__ */ new Map(), q = /* @__PURE__ */ new Map();
891
886
  let M = !1, D = 0, L = !1;
892
- const Qe = new w.SyncEvent(), Re = new w.SyncEvent(), ie = new w.SyncEvent(), ke = new w.SyncEvent(), Ie = new w.SyncEvent();
887
+ const Qe = new w.SyncEvent(), Re = new w.SyncEvent(), ie = new w.SyncEvent(), Ie = new w.SyncEvent(), Ne = new w.SyncEvent();
893
888
  Dt.attach(jt);
894
889
  Pt.attach(Ut);
895
890
  function Mt(e) {
@@ -903,7 +898,7 @@ function Mt(e) {
903
898
  }), new Promise((r, t) => {
904
899
  G.set(e.cache, () => {
905
900
  e.resolve?.(), r();
906
- }), I.set(e.cache, t), q.set(e.cache, { timer: 0, retries: 0 }), !M && !L && j();
901
+ }), N.set(e.cache, t), q.set(e.cache, { timer: 0, retries: 0 }), !M && !L && j();
907
902
  });
908
903
  }
909
904
  function j() {
@@ -918,15 +913,15 @@ function j() {
918
913
  M = !1;
919
914
  return;
920
915
  }
921
- const r = e[g.CACHE];
916
+ const r = e[_.CACHE];
922
917
  if (!P.has(r)) {
923
918
  j();
924
919
  return;
925
920
  }
926
921
  M = !0, ae(r), Qe.post({
927
922
  cacheId: r,
928
- position: e[g.POSITION] | e[g.POSITION + 1] << 8,
929
- dataSize: e[g.DATA_SIZE] | e[g.DATA_SIZE + 1] << 8,
923
+ position: e[_.POSITION] | e[_.POSITION + 1] << 8,
924
+ dataSize: e[_.DATA_SIZE] | e[_.DATA_SIZE + 1] << 8,
930
925
  packetBytes: e.length,
931
926
  batchIndex: D,
932
927
  timestamp: Date.now()
@@ -935,10 +930,10 @@ function j() {
935
930
  });
936
931
  }
937
932
  function jt(e) {
938
- Ie.post({
933
+ Ne.post({
939
934
  cacheId: e,
940
935
  timestamp: Date.now()
941
- }), G.get(e)?.(), Ne(e), L = !1, D = 0, j();
936
+ }), G.get(e)?.(), Ve(e), L = !1, D = 0, j();
942
937
  }
943
938
  function Ut(e) {
944
939
  D = 0, L = !1;
@@ -972,16 +967,16 @@ function Qt(e) {
972
967
  if (!P.has(e)) return;
973
968
  const r = q.get(e);
974
969
  if (r == null) return;
975
- if (r.retries++, ke.post({
970
+ if (r.retries++, Ie.post({
976
971
  cacheId: e,
977
972
  retryCount: r.retries,
978
973
  maxRetries: H,
979
974
  willRetry: r.retries <= H,
980
975
  timestamp: Date.now()
981
976
  }), r.retries > H) {
982
- I.get(e)?.(new Error(
977
+ N.get(e)?.(new Error(
983
978
  `Buffer ${e} timed out after ${H} retries (${Be}ms timeout)`
984
- )), Ne(e);
979
+ )), Ve(e);
985
980
  return;
986
981
  }
987
982
  const t = P.get(e);
@@ -992,28 +987,28 @@ function Qt(e) {
992
987
  timestamp: Date.now()
993
988
  }), t.packets.forEach((l) => T.unshift(l))), ae(e), L = !1, D = 0, j();
994
989
  }
995
- function Ne(e) {
996
- G.delete(e), I.delete(e), P.delete(e), Bt(e);
990
+ function Ve(e) {
991
+ G.delete(e), N.delete(e), P.delete(e), Bt(e);
997
992
  for (let r = T.length - 1; r >= 0; r--)
998
- T[r][g.CACHE] === e && T.splice(r, 1);
993
+ T[r][_.CACHE] === e && T.splice(r, 1);
999
994
  }
1000
995
  function Rt(e) {
1001
- for (const [, r] of I)
996
+ for (const [, r] of N)
1002
997
  r(e);
1003
- G.clear(), I.clear();
998
+ G.clear(), N.clear();
1004
999
  for (const r of q.values())
1005
1000
  clearTimeout(r.timer);
1006
1001
  q.clear(), P.clear(), T.length = 0, M = !1, D = 0, L = !1;
1007
1002
  }
1008
- function kt() {
1003
+ function It() {
1009
1004
  return {
1010
1005
  write: Mt,
1011
1006
  abortAll: Rt,
1012
1007
  onPacketSent: Qe,
1013
1008
  onMessageQueued: Re,
1014
1009
  onRetransmit: ie,
1015
- onTimeout: ke,
1016
- onMessageComplete: Ie
1010
+ onTimeout: Ie,
1011
+ onMessageComplete: Ne
1017
1012
  };
1018
1013
  }
1019
1014
  class Ft {
@@ -1034,10 +1029,10 @@ export {
1034
1029
  Ft as S,
1035
1030
  Y as a,
1036
1031
  Et as b,
1037
- Vt as c,
1038
- kt as d,
1032
+ kt as c,
1033
+ It as d,
1039
1034
  at as e,
1040
1035
  w as l,
1041
- Nt as s,
1036
+ Vt as s,
1042
1037
  oe as u
1043
1038
  };