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/README.md +887 -0
- package/dist/Objects-Zntmn07s.cjs +1 -0
- package/dist/Objects-ulh5Kfet.js +1043 -0
- package/dist/main.cjs +1 -1
- package/dist/main.js +641 -287
- package/dist/objects.cjs +1 -1
- package/dist/objects.js +1 -1
- package/dist/types/Types.d.ts +99 -0
- package/dist/types/ble/BluetoothProvider.d.ts +9 -0
- package/dist/types/ble/Mesh.d.ts +14 -9
- package/dist/types/ble/MeshWriter.d.ts +8 -0
- package/dist/types/ble/MockBluetooth.d.ts +25 -0
- package/dist/types/ble/Settings.d.ts +4 -0
- package/dist/types/composable/Debug.d.ts +26 -0
- package/dist/types/main.d.ts +3 -1
- package/dist/types/standee/Loader.d.ts +5 -0
- package/package.json +1 -1
- package/dist/Objects-C2BH61j-.js +0 -941
- package/dist/Objects-WdcC281c.cjs +0 -1
- package/readme.md +0 -30
|
@@ -0,0 +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;
|