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/dist/main.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./Objects-Zntmn07s.cjs"),s=require("./enums.cjs"),O=require("./elements.cjs");class ut{data=[];size=0;total=0}const G=7,S=new Map;function lt(t){return S.get(t)}function ft(t){S.delete(t)}function ht(t){const e=t.getUint8(s.Data.CACHE),o=t.getUint16(s.Data.DATA_SIZE,!0),n=t.getUint16(s.Data.POSITION,!0),r=t.byteLength-G;if(!S.has(e)){const i=new ut;i.size=o,S.set(e,i)}const a=S.get(e);for(let i=0;i<r;i++)a.data[i+n]=t.getUint8(G+i);a.total+=r}function dt(t){const e=S.get(t);return e.total==e.size||e.size==0}function gt(){return{add:ht,get:lt,remove:ft,isDone:dt}}const{serviceUuid:pt,handshakeCharUuid:At}=c.useBluetoothSettings(),V=new c.libExports.SyncEvent,{add:yt,get:Dt,remove:wt,isDone:Tt}=gt();function mt(){return new Promise((t,e)=>{c.useBluetooth().subscribe(pt,At,Et).then(()=>t()).catch(o=>e(o))})}function Et(t){console.log(t,t.buffer.byteLength);const e=t.getUint8(s.Data.CACHE);yt(t),Tt(e)&&(V.post(It(e)),wt(e))}function It(t){const e=Dt(t),o=new Map;let n=1;for(;n<e.size;){const r=e.data[n++],a=e.data[n++],i=[];for(let u=0;u<a;u++)i.push(e.data[n++]);o.set(r,i)}return o}function Z(){return{subscribe:mt,onNodes:V}}const{onNodes:bt}=Z(),{onMessageDone:St,onNode:Ct,onNodeLost:Ut}=c.useFloodCharacteristic(),{onPlusButton:_t,onMinButton:Nt,onEitherButton:Rt}=c.useMessageCharacteristic(),{connect:Bt,reconnect:Y,onConnect:xt,onDisconnect:q}=c.useBluetooth(),K=new c.libExports.SyncEvent,X=new c.libExports.SyncEvent;let B=!1,b=!1,C=!1;q.attach(()=>{B&&!b&&(c.useMeshWriter().abortAll(new Error("BLE disconnected")),Lt())});async function Pt(){C=!0,await Bt(c.useBluetoothSettings().options),await z(),B=!0}async function Mt(){C=!0,await Y(),await z(),B=!0}function z(){return Promise.all([Z().subscribe(),c.useMessageCharacteristic().subscribe(),c.useFloodCharacteristic().subscribe()]).then(()=>{})}async function Lt(){const{RECONNECT_DELAY:t,RECONNECT_MAX_RETRIES:e}=c.useBluetoothSettings();b=!0,C=!1;for(let o=1;o<=e;o++){const n=Math.min(t*Math.pow(2,o-1),3e4);if(K.post({attempt:o,maxRetries:e,delay:n}),await vt(n),C){b=!1,C=!1;return}try{await Y(),await z(),b=!1;return}catch(r){console.warn(`Auto-reconnect attempt ${o}/${e} failed:`,r)}}b=!1,X.post()}function vt(t){return new Promise(e=>setTimeout(e,t))}function Ot(){B=!1,b=!1,C=!1}function j(){return{connect:Pt,onConnect:xt,onDisconnect:q,onReconnecting:K,onReconnectFailed:X,reconnect:Mt,onNodes:bt,onNode:Ct,onNodeLost:Ut,onPlusButton:_t,onMinButton:Nt,onEitherButton:Rt,onMessageDone:St,_resetForTesting:Ot}}function kt(t){return{upload:(e,o)=>{const n=new Uint8Array(o.length+1);return n.set([e],0),n.set(o,1),new c.Buffer(s.DataType.UPLOAD,t.id,n)},clear:()=>new c.Buffer(s.DataType.CLEAR_GRAPHICS,t.id)}}class $t{id;buffers;totalPackets=0;onProgress=new c.libExports.SyncEvent;onError=new c.libExports.SyncEvent;constructor(e,o){this.id=o,this.buffers=e,this.buffers.forEach(n=>{this.totalPackets+=n.packets.length})}async send(){const e=this.totalPackets&255,o=this.totalPackets>>8&255,n=new Uint8Array([e,o]),r=new c.Buffer(s.DataType.LOADER,this.id,n),a=this.buffers.length;let i=0;try{await r.send();for(const u of this.buffers)await u.send(),i++,this.onProgress.post({current:i,total:a,percentage:i/a})}catch(u){throw this.onError.post({buffer:i,error:u}),u}}}function zt(t){return{load:e=>new $t(e,t.id)}}const{onNodes:Ht,onNode:Ft,onNodeLost:Gt}=j();Ht.attach(Wt);Ft.attach(Vt);Gt.attach(Zt);const p=[],x=new c.libExports.SyncEvent;function Wt(t){console.log("standees",t);for(const[e,o]of t)J(e),p.find(n=>n.id===e).neighbors=o;x.post(p)}function Vt(t){console.log("nodes",t);const e=t.keys().next().value,o=t.get(e);J(e),p.find(n=>n.id===e).neighbors=o,x.post(p)}function Zt(t){Yt(t),x.post(p)}function J(t){p.some(e=>e.id===t)||p.push(new c.Standee(t))}function Yt(t){const e=p.findIndex(o=>o.id===t);e!==-1&&p.splice(e,1)}function qt(){return{standees:p,onChange:x}}function Kt(t){const e=()=>{let n=Math.floor(Math.random()*256);return o(n)?e():n},o=n=>t.some(r=>r.id===n);return{generate:e}}function Xt(t){return{add:n=>(n.id=Kt(t.elements).generate(),t.elements.push(n),n),remove:n=>{t.elements=t.elements.filter(r=>r.id!==n)}}}function jt(t){return new c.Buffer(s.DataType.CLEAR_SCREEN,t.id)}function Jt(t){let e=new Uint8Array;for(const o of t.elements)o.dirty!==!1&&(o instanceof O.Image&&(e=k(e,Qt(o))),o instanceof O.Rectangle&&(e=k(e,te(o))),o instanceof O.Text&&(e=k(e,ee(o))),o.dirty=!1);return new c.Buffer(s.DataType.ALL,t.id,e)}function k(t,e){const o=new Uint8Array(t.length+e.length);return o.set(t,0),o.set(e,t.length),o}function Qt(t){return new Uint8Array([s.DataType.DRAW_IMAGE,7,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.data])}function te(t){return new Uint8Array([s.DataType.DRAW_RECTANGLE,9,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.radius,t.fill?1:0,t.colorBlack?0:1])}function ee(t){const e=new TextEncoder().encode(t.text),o=new Uint8Array(9+e.length);return o.set([s.DataType.DRAW_TEXT,7+e.length,t.id,t.show?1:0,t.x,t.y,t.font,t.colorBlack?0:1,t.center?1:0],0),o.set(e,9),o}function ne(){return{clear:jt,draw:Jt}}function oe(t,e){const o=Math.ceil(t.length/e),n=Math.ceil(e/8),r=new Uint8Array(n*o);for(let a=0;a<o;a++)for(let i=0;i<n;i++){let u=0;for(let l=0;l<8;l++){const f=i*8+l,w=a*e+f;f<e&&w<t.length&&t[w]&&(u|=1<<7-l)}r[a*n+i]=Q(u)}return r}function Q(t){return t=(t&240)>>4|(t&15)<<4,t=(t&204)>>2|(t&51)<<2,t=(t&170)>>1|(t&85)<<1,t}function se(t){const e=t.reduce((r,a)=>r+a.length,0),o=new Uint8Array(e);let n=0;for(const r of t)o.set(r,n),n+=r.length;return o}function P(){return{pixelsToBytes:oe,bitswap:Q,flattenUint8Arrays:se}}const{flattenUint8Arrays:tt}=P();class T{element_id;property;constructor(e,o){this.element_id=e,this.property=o}}class A{target;constructor(e){this.target=e}format(){return new Uint8Array}}class re extends A{format(){return new Uint8Array([s.ActionType.INCREMENT,2,this.target.element_id,this.target.property])}}class ae extends A{format(){return new Uint8Array([s.ActionType.DECREMENT,2,this.target.element_id,this.target.property])}}class ie extends A{format(){return new Uint8Array([s.ActionType.SHOW,1,this.target.element_id])}}class ce extends A{format(){return new Uint8Array([s.ActionType.HIDE,1,this.target.element_id])}}let ue=class extends A{value;constructor(e,o){super(e),this.value=o}format(){const e=this.dataType(),o=this.convertValue(e),n=new Uint8Array(5+o.length),r=[s.ActionType.SET,o.length+3,e,this.target.element_id,this.target.property];return n.set(r,0),n.set(o,5),n}dataType(){return typeof this.value==="string"?s.ActionDataType.STRING:s.ActionDataType.NUMBER}convertValue(e){return e===s.ActionDataType.STRING?new TextEncoder().encode(this.value):e===s.ActionDataType.NUMBER?new Uint8Array([this.value]):new Uint8Array}};class le extends A{format(){return this.target.element_id!==0&&this.target.property!==0?new Uint8Array([s.ActionType.BROADCAST,2,this.target.element_id,this.target.property]):new Uint8Array([s.ActionType.BROADCAST,0])}}class fe extends A{format(){return new Uint8Array([s.ActionType.BREAK,0])}}let he=class extends A{fromLow;fromHigh;toLow;toHigh;value;constructor(e,o,n,r,a,i){super(i),this.value=e,this.fromLow=o,this.fromHigh=n,this.toLow=r,this.toHigh=a}format(){return new Uint8Array([s.ActionType.MAP,8,this.value.element_id,this.value.property,this.fromLow,this.fromHigh,this.toLow,this.toHigh,this.target.element_id,this.target.property])}};class de extends A{deciseconds;resetable;onComplete;constructor(e,o,n){super(new T(0,0)),this.deciseconds=e,this.resetable=o,this.onComplete=n}format(){const e=tt(this.onComplete.map(r=>r.format())),o=new Uint8Array(4+e.length),n=[s.ActionType.TIMER,e.length+2,this.resetable?1:0,this.deciseconds];return o.set(n,0),e.forEach((r,a)=>o.set([r],a+4)),o}}class ge extends A{left;operator;right;actions;constructor(e,o,n,r){super(new T(0,0)),this.actions=r,this.left=e,this.operator=o,this.right=n}convertValue(e){if(typeof e=="number")return new Uint8Array([s.ActionDataType.NUMBER,1,e]);if(typeof e=="string"){const o=new TextEncoder().encode(e);return new Uint8Array([s.ActionDataType.STRING,o.length,...o])}return e instanceof T?new Uint8Array([s.ActionDataType.ELEMENT,2,e.element_id,e.property]):new Uint8Array}format(){const e=tt(this.actions.map(u=>u.format())),o=this.convertValue(this.left),n=this.convertValue(this.right),r=4+o.length+n.length+e.length,a=new Uint8Array(r),i=[s.ActionType.CONDITION,r-2,o.length+n.length+1];return a.set(i,0),a.set([this.operator],3),a.set(o,4),a.set(n,4+o.length),a.set(e,4+o.length+n.length),a}}function pe(){return{target(t,e){return new T(t,e)},increment(t){return new re(t)},decrement(t){return new ae(t)},show(t){return new ie(new T(t,0))},hide(t){return new ce(new T(t,0))},set(t,e){return new ue(t,e)},broadcast(t){return new le(t||new T(0,0))},timer(t,e,o){return new de(t,e,o)},stop(){return new fe(new T(0,0))},condition(t,e,o,n){return new ge(t,e,o,n)},map(t,e,o,n,r,a){return new he(t,e,o,n,r,a)}}}const{flattenUint8Arrays:Ae}=P();function ye(t){return{setButton(e,o){const n=Ae(o.map(a=>a.format())),r=new Uint8Array(1+n.length);return r.set([e],0),n.forEach((a,i)=>r.set([a],i+1)),new c.Buffer(s.DataType.ACTIONS,t.id,r)}}}function De(t){return new Promise((e,o)=>{const n=new Image;n.onload=()=>e(n),n.onerror=o,n.src=t})}function we(t){const e=document.createElement("canvas"),o=e.getContext("2d");if(e.width=t.width,e.height=t.height,!o)throw new Error("Failed to get 2D context for image data.");return o.drawImage(t,0,0),o.getImageData(0,0,t.width,t.height)}function Te(t){const e=[],o=t.data,n=t.width,r=t.height;for(let a=0;a<r;a++)for(let i=0;i<n;i++){const u=(a*n+i)*4,l=o[u],f=o[u+1],w=o[u+2],v=.2126*l+.7152*f+.0722*w;e.push(v<128?0:1)}return{width:n,height:r,pixels:e}}function et(){return{createImage:De,getImageData:we,imageDataToPixels:Te}}const{createImage:me,getImageData:Ee,imageDataToPixels:Ie}=et(),{pixelsToBytes:be}=P();function Se(t){return new Promise((e,o)=>{me(t).then(n=>Ee(n)).then(n=>Ie(n)).then(n=>be(n.pixels,n.width)).then(n=>e(n)).catch(n=>o(n))})}function Ce(){return{imageToBytes:Se}}function Ue(t,e,o,n,r){return(t-e)/(o-e)*(r-n)+n}function _e(){return{map:Ue}}let h=!1;const E=[],D=new Map;let _=500,$=!1;const nt=new c.libExports.SyncEvent,N=new c.libExports.SyncEvent,ot=new Map([[s.DataType.UPLOAD,"UPLOAD"],[s.DataType.DRAW_IMAGE,"DRAW_IMAGE"],[s.DataType.DRAW_RECTANGLE,"DRAW_RECTANGLE"],[s.DataType.BUTTON_PRESS,"BUTTON_PRESS"],[s.DataType.DRAW_TEXT,"DRAW_TEXT"],[s.DataType.ALL,"ALL"],[s.DataType.CLEAR_GRAPHICS,"CLEAR_GRAPHICS"],[s.DataType.ACTIONS,"ACTIONS"],[s.DataType.CLEAR_SCREEN,"CLEAR_SCREEN"],[s.DataType.LOADER,"LOADER"]]);function Ne(){h=!0,$||($e(),$=!0)}function Re(){h=!1}function Be(){return h}function xe(t){_=t,st()}function Pe(){return E}function Me(){return Array.from(D.values())}function Le(t){return D.get(t)}function ve(){E.length=0,D.clear()}function Oe(t){const e=t.length>s.Data.TYPE?t[s.Data.TYPE]:0;return{cacheId:t[s.Data.CACHE],targetId:t[s.Data.ID],ttl:t[s.Data.TTL],position:t.length>4?t[s.Data.POSITION]|t[s.Data.POSITION+1]<<8:0,dataSize:t.length>6?t[s.Data.DATA_SIZE]|t[s.Data.DATA_SIZE+1]<<8:0,typeName:ot.get(e)??`UNKNOWN(${e})`,typeValue:e,payloadBytes:Math.max(0,t.length-s.Data.DATA),raw:Array.from(t)}}function g(t,e){if(!h)return;const o={type:t,timestamp:Date.now(),data:e};E.push(o),st(),nt.post(o)}function st(){E.length>_&&E.splice(0,E.length-_)}function ke(t,e){if(!h)return;const o=D.get(t);o&&(Object.assign(o,e),o.completedAt&&o.queuedAt&&(o.latencyMs=o.completedAt-o.queuedAt),N.post(o))}function $e(){const t=c.useMeshWriter(),e=c.useFloodCharacteristic(),o=c.useBluetooth();t.onMessageQueued.attach(n=>{h&&(D.set(n.cacheId,{cacheId:n.cacheId,queuedAt:n.timestamp,firstPacketAt:null,completedAt:null,latencyMs:null,retransmitCount:0,timeoutCount:0,packetsSent:0,totalPackets:n.packetCount}),g("message_queued",{cacheId:n.cacheId,typeName:ot.get(n.type)??`UNKNOWN(${n.type})`,targetId:n.targetId,packetCount:n.packetCount,totalBytes:n.totalBytes}))}),t.onPacketSent.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.packetsSent++,r.firstPacketAt===null&&(r.firstPacketAt=n.timestamp)),g("packet_sent",{cacheId:n.cacheId,position:n.position,dataSize:n.dataSize,batchIndex:n.batchIndex})}),t.onRetransmit.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.retransmitCount++,N.post(r)),g("retransmit",{cacheId:n.cacheId,packetCount:n.packetCount,reason:n.reason})}),t.onTimeout.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.timeoutCount++,N.post(r)),g("timeout",{cacheId:n.cacheId,retryCount:n.retryCount,maxRetries:n.maxRetries,willRetry:n.willRetry})}),t.onMessageComplete.attach(n=>{h&&(ke(n.cacheId,{completedAt:n.timestamp}),g("message_complete",{cacheId:n.cacheId}))}),e.onMessageDone.attach(n=>{h&&g("message_done",{cacheId:n})}),e.onReceivedPackets.attach(n=>{h&&g("received_packets",{incompleteCaches:n.filter(r=>r!==0)})}),e.onNode.attach(n=>{if(h)for(const[r,a]of n)g("node_discovered",{nodeId:r,neighbors:a})}),e.onNodeLost.attach(n=>{h&&g("node_lost",{nodeId:n})}),o.onConnect.attach(()=>{h&&g("connected",{})}),o.onDisconnect.attach(()=>{h&&g("disconnected",{})})}function ze(){h=!1,E.length=0,D.clear(),$=!1,_=500}function He(){return{enable:Ne,disable:Re,isEnabled:Be,clear:ve,getLog:Pe,getTimings:Me,getTimingForCache:Le,setMaxLogSize:xe,decodePacket:Oe,onDebugEvent:nt,onTimingUpdate:N,_resetForTesting:ze}}const{messageCharUuid:rt,messageFloodCharUuid:M,handshakeCharUuid:at,BATCH_SIZE:Fe,PACKET_SIZE:R}=c.useBluetoothSettings();let H=!1,U=!1,L=!0,d={standees:[]};const y=new Map,m=new Map;let I=0;const W=7;function Ge(t){d={responseDelay:10,dropRate:0,partialDelivery:!1,...t},H=!0,U=!1,L=!0,y.clear(),m.clear(),I=0,c.setBluetoothProvider(Qe)}function it(){H=!1,U=!1,L=!0,y.clear(),m.clear(),I=0,c.setBluetoothProvider(null)}function We(){return H}function Ve(){return d}function Ze(t){d={...d,...t}}function Ye(t,e,o){const n=y.get(rt);if(!n)return;const r=e==="plus"?s.Button.PLUS:e==="minus"?s.Button.MINUS:s.Button.EITHER,a=o?new TextEncoder().encode(o):new Uint8Array(0),i=s.Data.DATA+1+a.length,u=new ArrayBuffer(i),l=new DataView(u);l.setUint8(s.Data.ID,t),l.setUint8(s.Data.TYPE,s.DataType.BUTTON_PRESS),l.setUint8(s.Data.DATA,r);for(let f=0;f<a.length;f++)l.setUint8(s.Data.DATA+1+f,a[f]);n(l)}function qe(t){const e=y.get(M);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+1),n=new DataView(o);n.setUint8(s.Data.ID,t),n.setUint8(s.Data.TYPE,s.FloodTypes.LOST_NODE),n.setUint8(s.Data.DATA,t),e(n)}function Ke(t){const e=y.get(M);e&&e(nn(t))}function Xe(){U=!1,y.clear(),m.clear(),I=0,c.useBluetooth().onDisconnect.post()}function je(t){L=t}function Je(){it()}const Qe={async connect(t){U=!0,c.useBluetooth().onConnect.post()},async reconnect(){if(!L)throw new Error("No devices found");U=!0,c.useBluetooth().onConnect.post()},async readCharacteristic(t,e){},async writeCharacteristic(t,e,o){if(!U||e!==rt||d.dropRate!==void 0&&d.dropRate>0&&Math.random()<d.dropRate)return;const n=o[s.Data.CACHE],r=o[s.Data.POSITION]|o[s.Data.POSITION+1]<<8,a=o[s.Data.DATA_SIZE]|o[s.Data.DATA_SIZE+1]<<8;m.has(n)||m.set(n,{received:new Set,dataSize:a});const i=m.get(n);i.received.add(r),I++;const u=Math.ceil(a/R);if(i.received.size>=u){const f=d.responseDelay??10;setTimeout(()=>{tn(n)},f);return}if(d.partialDelivery&&I>=Fe){I=0;const f=d.responseDelay??10;setTimeout(()=>{const w=[];for(const[v,F]of m){const ct=Math.ceil(F.dataSize/R);F.received.size<ct&&w.push(v)}w.length>0&&en(w)},f)}},async subscribe(t,e,o){y.set(e,o),e===at&&on()}};function tn(t){const e=y.get(M);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+1),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.MESSAGE_DONE),n.setUint8(s.Data.DATA,t),m.delete(t),I=0,e(n)}function en(t){const e=y.get(M);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+t.length),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.DATA_FEEDBACK);for(let r=0;r<t.length;r++)n.setUint8(s.Data.DATA+r,t[r]);e(n)}function nn(t){const e=s.Data.DATA+1+t.neighbors.length,o=new ArrayBuffer(e),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.NODES),n.setUint8(s.Data.DATA,t.id);for(let r=0;r<t.neighbors.length;r++)n.setUint8(s.Data.DATA+1+r,t.neighbors[r]);return n}function on(){const t=y.get(at);if(!t||d.standees.length===0)return;const e=[d.standees.length];for(const r of d.standees)e.push(r.id,r.neighbors.length,...r.neighbors);const o=200,n=e.length;for(let r=0;r<n;r+=R){const a=Math.min(R,n-r),i=W+a,u=new ArrayBuffer(i),l=new DataView(u);l.setUint8(s.Data.ID,0),l.setUint8(s.Data.CACHE,o),l.setUint8(s.Data.TTL,10),l.setUint16(s.Data.POSITION,r,!0),l.setUint16(s.Data.DATA_SIZE,n,!0);for(let f=0;f<a;f++)l.setUint8(W+f,e[r+f]);t(l)}}function sn(){return{activate:Ge,deactivate:it,isActive:We,getConfig:Ve,updateConfig:Ze,pressButton:Ye,simulateNodeLost:qe,simulateNodeJoin:Ke,disconnect:Xe,reconnectable:je,_resetForTesting:Je}}exports.useActions=pe;exports.useBinary=P;exports.useButtons=ye;exports.useConverter=Ce;exports.useDebug=He;exports.useDisplay=ne;exports.useGraphics=kt;exports.useImage=et;exports.useLoader=zt;exports.useMesh=j;exports.useMockBluetooth=sn;exports.useStandeeElements=Xt;exports.useStandees=qt;exports.useUtils=_e;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./Objects-CQULDdkF.cjs"),s=require("./enums.cjs"),O=require("./elements.cjs");class ct{data=[];size=0;total=0}const G=7,C=new Map;function ut(t){return C.get(t)}function lt(t){C.delete(t)}function ft(t){const e=t.getUint8(s.Data.CACHE),o=t.getUint16(s.Data.DATA_SIZE,!0),n=t.getUint16(s.Data.POSITION,!0),r=t.byteLength-G;if(!C.has(e)){const i=new ct;i.size=o,C.set(e,i)}const a=C.get(e);for(let i=0;i<r;i++)a.data[i+n]=t.getUint8(G+i);a.total+=r}function ht(t){const e=C.get(t);return e.total==e.size||e.size==0}function dt(){return{add:ft,get:ut,remove:lt,isDone:ht}}const{serviceUuid:gt,handshakeCharUuid:pt}=c.useBluetoothSettings(),V=new c.libExports.SyncEvent,{add:At,get:yt,remove:Dt,isDone:wt}=dt();function Tt(){return new Promise((t,e)=>{c.useBluetooth().subscribe(gt,pt,mt).then(()=>t()).catch(o=>e(o))})}function mt(t){console.log(t,t.buffer.byteLength);const e=t.getUint8(s.Data.CACHE);At(t),wt(e)&&(V.post(Et(e)),Dt(e))}function Et(t){const e=yt(t),o=new Map;let n=1;for(;n<e.size;){const r=e.data[n++],a=e.data[n++],i=[];for(let u=0;u<a;u++)i.push(e.data[n++]);o.set(r,i)}return o}function Z(){return{subscribe:Tt,onNodes:V}}const{onNodes:It}=Z(),{onMessageDone:bt,onNode:Ct,onNodeLost:St}=c.useFloodCharacteristic(),{onPlusButton:Ut,onMinButton:_t,onEitherButton:Bt}=c.useMessageCharacteristic(),{connect:Nt,reconnect:Y,onConnect:Rt,onDisconnect:q}=c.useBluetooth(),K=new c.libExports.SyncEvent,X=new c.libExports.SyncEvent;let x=!1,b=!1,S=!1;q.attach(()=>{x&&!b&&(c.useMeshWriter().abortAll(new Error("BLE disconnected")),Pt())});async function xt(){S=!0,await Nt(c.useBluetoothSettings().options),await z(),x=!0}async function Mt(){S=!0,await Y(),await z(),x=!0}function z(){return Promise.all([Z().subscribe(),c.useMessageCharacteristic().subscribe(),c.useFloodCharacteristic().subscribe()]).then(()=>{})}async function Pt(){const{RECONNECT_DELAY:t,RECONNECT_MAX_RETRIES:e}=c.useBluetoothSettings();b=!0,S=!1;for(let o=1;o<=e;o++){const n=Math.min(t*Math.pow(2,o-1),3e4);if(K.post({attempt:o,maxRetries:e,delay:n}),await Lt(n),S){b=!1,S=!1;return}try{await Y(),await z(),b=!1;return}catch(r){console.warn(`Auto-reconnect attempt ${o}/${e} failed:`,r)}}b=!1,X.post()}function Lt(t){return new Promise(e=>setTimeout(e,t))}function vt(){x=!1,b=!1,S=!1}function j(){return{connect:xt,onConnect:Rt,onDisconnect:q,onReconnecting:K,onReconnectFailed:X,reconnect:Mt,onNodes:It,onNode:Ct,onNodeLost:St,onPlusButton:Ut,onMinButton:_t,onEitherButton:Bt,onMessageDone:bt,_resetForTesting:vt}}function Ot(t){return{upload:(e,o)=>{const n=new Uint8Array(o.length+1);return n.set([e],0),n.set(o,1),new c.Buffer(s.DataType.UPLOAD,t.id,n)},clear:()=>new c.Buffer(s.DataType.CLEAR_GRAPHICS,t.id)}}class kt{id;buffers;totalPackets=0;onProgress=new c.libExports.SyncEvent;onError=new c.libExports.SyncEvent;constructor(e,o){this.id=o,this.buffers=e,this.buffers.forEach(n=>{this.totalPackets+=n.packets.length})}async send(){const e=this.totalPackets&255,o=this.totalPackets>>8&255,n=new Uint8Array([e,o]),r=new c.Buffer(s.DataType.LOADER,this.id,n),a=this.buffers.length;let i=0;try{await r.send();for(const u of this.buffers)await u.send(),i++,this.onProgress.post({current:i,total:a,percentage:i/a})}catch(u){throw this.onError.post({buffer:i,error:u}),u}}}function $t(t){return{load:e=>new kt(e,t.id)}}const{onNodes:zt,onNode:Ft,onNodeLost:Ht}=j();zt.attach(Gt);Ft.attach(Wt);Ht.attach(Vt);const p=[],M=new c.libExports.SyncEvent;function Gt(t){console.log("standees",t);for(const[e,o]of t)J(e),p.find(n=>n.id===e).neighbors=o;M.post(p)}function Wt(t){console.log("nodes",t);const e=t.keys().next().value,o=t.get(e);J(e),p.find(n=>n.id===e).neighbors=o,M.post(p)}function Vt(t){Zt(t),M.post(p)}function J(t){p.some(e=>e.id===t)||p.push(new c.Standee(t))}function Zt(t){const e=p.findIndex(o=>o.id===t);e!==-1&&p.splice(e,1)}function Yt(){return{standees:p,onChange:M}}function qt(t){const e=()=>{let n=Math.floor(Math.random()*256);return o(n)?e():n},o=n=>t.some(r=>r.id===n);return{generate:e}}function Kt(t){return{add:n=>(n.id=qt(t.elements).generate(),t.elements.push(n),n),remove:n=>{t.elements=t.elements.filter(r=>r.id!==n)}}}function Xt(t){return new c.Buffer(s.DataType.CLEAR_SCREEN,t.id)}function jt(t){let e=new Uint8Array;for(const o of t.elements)o.dirty!==!1&&(o instanceof O.Image&&(e=k(e,Jt(o))),o instanceof O.Rectangle&&(e=k(e,Qt(o))),o instanceof O.Text&&(e=k(e,te(o))),o.dirty=!1);return new c.Buffer(s.DataType.ALL,t.id,e)}function k(t,e){const o=new Uint8Array(t.length+e.length);return o.set(t,0),o.set(e,t.length),o}function Jt(t){return new Uint8Array([s.DataType.DRAW_IMAGE,7,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.data])}function Qt(t){return new Uint8Array([s.DataType.DRAW_RECTANGLE,9,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.radius,t.fill?1:0,t.colorBlack?0:1])}function te(t){const e=new TextEncoder().encode(t.text),o=new Uint8Array(9+e.length);return o.set([s.DataType.DRAW_TEXT,7+e.length,t.id,t.show?1:0,t.x,t.y,t.font,t.colorBlack?0:1,t.center?1:0],0),o.set(e,9),o}function ee(){return{clear:Xt,draw:jt}}function ne(t,e){const o=Math.ceil(t.length/e),n=Math.ceil(e/8),r=new Uint8Array(n*o);for(let a=0;a<o;a++)for(let i=0;i<n;i++){let u=0;for(let l=0;l<8;l++){const f=i*8+l,w=a*e+f;f<e&&w<t.length&&t[w]&&(u|=1<<7-l)}r[a*n+i]=Q(u)}return r}function Q(t){return t=(t&240)>>4|(t&15)<<4,t=(t&204)>>2|(t&51)<<2,t=(t&170)>>1|(t&85)<<1,t}function oe(t){const e=t.reduce((r,a)=>r+a.length,0),o=new Uint8Array(e);let n=0;for(const r of t)o.set(r,n),n+=r.length;return o}function P(){return{pixelsToBytes:ne,bitswap:Q,flattenUint8Arrays:oe}}const{flattenUint8Arrays:tt}=P();class T{element_id;property;constructor(e,o){this.element_id=e,this.property=o}}class A{target;constructor(e){this.target=e}format(){return new Uint8Array}}class se extends A{format(){return new Uint8Array([s.ActionType.INCREMENT,2,this.target.element_id,this.target.property])}}class re extends A{format(){return new Uint8Array([s.ActionType.DECREMENT,2,this.target.element_id,this.target.property])}}class ae extends A{format(){return new Uint8Array([s.ActionType.SHOW,1,this.target.element_id])}}class ie extends A{format(){return new Uint8Array([s.ActionType.HIDE,1,this.target.element_id])}}let ce=class extends A{value;constructor(e,o){super(e),this.value=o}format(){const e=this.dataType(),o=this.convertValue(e),n=new Uint8Array(5+o.length),r=[s.ActionType.SET,o.length+3,e,this.target.element_id,this.target.property];return n.set(r,0),n.set(o,5),n}dataType(){return typeof this.value==="string"?s.ActionDataType.STRING:s.ActionDataType.NUMBER}convertValue(e){return e===s.ActionDataType.STRING?new TextEncoder().encode(this.value):e===s.ActionDataType.NUMBER?new Uint8Array([this.value]):new Uint8Array}};class ue extends A{format(){return this.target.element_id!==0&&this.target.property!==0?new Uint8Array([s.ActionType.BROADCAST,2,this.target.element_id,this.target.property]):new Uint8Array([s.ActionType.BROADCAST,0])}}class le extends A{format(){return new Uint8Array([s.ActionType.BREAK,0])}}let fe=class extends A{fromLow;fromHigh;toLow;toHigh;value;constructor(e,o,n,r,a,i){super(i),this.value=e,this.fromLow=o,this.fromHigh=n,this.toLow=r,this.toHigh=a}format(){return new Uint8Array([s.ActionType.MAP,8,this.value.element_id,this.value.property,this.fromLow,this.fromHigh,this.toLow,this.toHigh,this.target.element_id,this.target.property])}};class he extends A{deciseconds;resetable;onComplete;constructor(e,o,n){super(new T(0,0)),this.deciseconds=e,this.resetable=o,this.onComplete=n}format(){const e=tt(this.onComplete.map(r=>r.format())),o=new Uint8Array(4+e.length),n=[s.ActionType.TIMER,e.length+2,this.resetable?1:0,this.deciseconds];return o.set(n,0),e.forEach((r,a)=>o.set([r],a+4)),o}}class de extends A{left;operator;right;actions;constructor(e,o,n,r){super(new T(0,0)),this.actions=r,this.left=e,this.operator=o,this.right=n}convertValue(e){if(typeof e=="number")return new Uint8Array([s.ActionDataType.NUMBER,1,e]);if(typeof e=="string"){const o=new TextEncoder().encode(e);return new Uint8Array([s.ActionDataType.STRING,o.length,...o])}return e instanceof T?new Uint8Array([s.ActionDataType.ELEMENT,2,e.element_id,e.property]):new Uint8Array}format(){const e=tt(this.actions.map(u=>u.format())),o=this.convertValue(this.left),n=this.convertValue(this.right),r=4+o.length+n.length+e.length,a=new Uint8Array(r),i=[s.ActionType.CONDITION,r-2,o.length+n.length+1];return a.set(i,0),a.set([this.operator],3),a.set(o,4),a.set(n,4+o.length),a.set(e,4+o.length+n.length),a}}function ge(){return{target(t,e){return new T(t,e)},increment(t){return new se(t)},decrement(t){return new re(t)},show(t){return new ae(new T(t,0))},hide(t){return new ie(new T(t,0))},set(t,e){return new ce(t,e)},broadcast(t){return new ue(t||new T(0,0))},timer(t,e,o){return new he(t,e,o)},stop(){return new le(new T(0,0))},condition(t,e,o,n){return new de(t,e,o,n)},map(t,e,o,n,r,a){return new fe(t,e,o,n,r,a)}}}const{flattenUint8Arrays:pe}=P();function Ae(t){return{setButton(e,o){const n=pe(o.map(a=>a.format())),r=new Uint8Array(1+n.length);return r.set([e],0),n.forEach((a,i)=>r.set([a],i+1)),new c.Buffer(s.DataType.ACTIONS,t.id,r)}}}function ye(t){return new Promise((e,o)=>{const n=new Image;n.onload=()=>e(n),n.onerror=o,n.src=t})}function De(t){const e=document.createElement("canvas"),o=e.getContext("2d");if(e.width=t.width,e.height=t.height,!o)throw new Error("Failed to get 2D context for image data.");return o.drawImage(t,0,0),o.getImageData(0,0,t.width,t.height)}function we(t){const e=[],o=t.data,n=t.width,r=t.height;for(let a=0;a<r;a++)for(let i=0;i<n;i++){const u=(a*n+i)*4,l=o[u],f=o[u+1],w=o[u+2],v=.2126*l+.7152*f+.0722*w;e.push(v<128?0:1)}return{width:n,height:r,pixels:e}}function et(){return{createImage:ye,getImageData:De,imageDataToPixels:we}}const{createImage:Te,getImageData:me,imageDataToPixels:Ee}=et(),{pixelsToBytes:Ie}=P();function be(t){return new Promise((e,o)=>{Te(t).then(n=>me(n)).then(n=>Ee(n)).then(n=>Ie(n.pixels,n.width)).then(n=>e(n)).catch(n=>o(n))})}function Ce(){return{imageToBytes:be}}function Se(t,e,o,n,r){return(t-e)/(o-e)*(r-n)+n}function Ue(){return{map:Se}}let h=!1;const E=[],D=new Map;let B=500,$=!1;const nt=new c.libExports.SyncEvent,N=new c.libExports.SyncEvent,ot=new Map([[s.DataType.UPLOAD,"UPLOAD"],[s.DataType.DRAW_IMAGE,"DRAW_IMAGE"],[s.DataType.DRAW_RECTANGLE,"DRAW_RECTANGLE"],[s.DataType.BUTTON_PRESS,"BUTTON_PRESS"],[s.DataType.DRAW_TEXT,"DRAW_TEXT"],[s.DataType.ALL,"ALL"],[s.DataType.CLEAR_GRAPHICS,"CLEAR_GRAPHICS"],[s.DataType.ACTIONS,"ACTIONS"],[s.DataType.CLEAR_SCREEN,"CLEAR_SCREEN"],[s.DataType.LOADER,"LOADER"]]);function _e(){h=!0,$||(ke(),$=!0)}function Be(){h=!1}function Ne(){return h}function Re(t){B=t,st()}function xe(){return E}function Me(){return Array.from(D.values())}function Pe(t){return D.get(t)}function Le(){E.length=0,D.clear()}function ve(t){const e=t.length>s.Data.TYPE?t[s.Data.TYPE]:0;return{cacheId:t[s.Data.CACHE],targetId:t[s.Data.ID],ttl:t[s.Data.TTL],position:t.length>4?t[s.Data.POSITION]|t[s.Data.POSITION+1]<<8:0,dataSize:t.length>6?t[s.Data.DATA_SIZE]|t[s.Data.DATA_SIZE+1]<<8:0,typeName:ot.get(e)??`UNKNOWN(${e})`,typeValue:e,payloadBytes:Math.max(0,t.length-s.Data.DATA),raw:Array.from(t)}}function g(t,e){if(!h)return;const o={type:t,timestamp:Date.now(),data:e};E.push(o),st(),nt.post(o)}function st(){E.length>B&&E.splice(0,E.length-B)}function Oe(t,e){if(!h)return;const o=D.get(t);o&&(Object.assign(o,e),o.completedAt&&o.queuedAt&&(o.latencyMs=o.completedAt-o.queuedAt),N.post(o))}function ke(){const t=c.useMeshWriter(),e=c.useFloodCharacteristic(),o=c.useBluetooth();t.onMessageQueued.attach(n=>{h&&(D.set(n.cacheId,{cacheId:n.cacheId,queuedAt:n.timestamp,firstPacketAt:null,completedAt:null,latencyMs:null,retransmitCount:0,timeoutCount:0,packetsSent:0,totalPackets:n.packetCount}),g("message_queued",{cacheId:n.cacheId,typeName:ot.get(n.type)??`UNKNOWN(${n.type})`,targetId:n.targetId,packetCount:n.packetCount,totalBytes:n.totalBytes}))}),t.onPacketSent.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.packetsSent++,r.firstPacketAt===null&&(r.firstPacketAt=n.timestamp)),g("packet_sent",{cacheId:n.cacheId,position:n.position,dataSize:n.dataSize,batchIndex:n.batchIndex})}),t.onRetransmit.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.retransmitCount++,N.post(r)),g("retransmit",{cacheId:n.cacheId,packetCount:n.packetCount,reason:n.reason})}),t.onTimeout.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.timeoutCount++,N.post(r)),g("timeout",{cacheId:n.cacheId,retryCount:n.retryCount,maxRetries:n.maxRetries,willRetry:n.willRetry})}),t.onMessageComplete.attach(n=>{h&&(Oe(n.cacheId,{completedAt:n.timestamp}),g("message_complete",{cacheId:n.cacheId}))}),e.onMessageDone.attach(n=>{h&&g("message_done",{cacheId:n})}),e.onReceivedPackets.attach(n=>{h&&g("received_packets",{incompleteCaches:n.filter(r=>r!==0)})}),e.onNode.attach(n=>{if(h)for(const[r,a]of n)g("node_discovered",{nodeId:r,neighbors:a})}),e.onNodeLost.attach(n=>{h&&g("node_lost",{nodeId:n})}),o.onConnect.attach(()=>{h&&g("connected",{})}),o.onDisconnect.attach(()=>{h&&g("disconnected",{})})}function $e(){h=!1,E.length=0,D.clear(),$=!1,B=500}function ze(){return{enable:_e,disable:Be,isEnabled:Ne,clear:Le,getLog:xe,getTimings:Me,getTimingForCache:Pe,setMaxLogSize:Re,decodePacket:ve,onDebugEvent:nt,onTimingUpdate:N,_resetForTesting:$e}}const{messageCharUuid:Fe,messageFloodCharUuid:_,handshakeCharUuid:rt,BATCH_SIZE:He,PACKET_SIZE:R}=c.useBluetoothSettings();let F=!1,U=!1,L=!0,d={standees:[]};const y=new Map,m=new Map;let I=0;const W=7;function Ge(t){d={responseDelay:10,dropRate:0,partialDelivery:!1,...t},F=!0,U=!1,L=!0,y.clear(),m.clear(),I=0,c.setBluetoothProvider(Qe)}function at(){F=!1,U=!1,L=!0,y.clear(),m.clear(),I=0,c.setBluetoothProvider(null)}function We(){return F}function Ve(){return d}function Ze(t){d={...d,...t}}function Ye(t,e,o){const n=y.get(_);if(!n)return;const r=e==="plus"?s.Button.PLUS:e==="minus"?s.Button.MINUS:s.Button.EITHER,a=o?new TextEncoder().encode(o):new Uint8Array(0),i=s.Data.DATA+1+a.length,u=new ArrayBuffer(i),l=new DataView(u);l.setUint8(s.Data.ID,t),l.setUint8(s.Data.TYPE,s.FloodTypes.DATA_FEEDBACK),l.setUint8(s.Data.DATA,r);for(let f=0;f<a.length;f++)l.setUint8(s.Data.DATA+1+f,a[f]);n(l)}function qe(t){const e=y.get(_);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+1),n=new DataView(o);n.setUint8(s.Data.ID,t),n.setUint8(s.Data.TYPE,s.FloodTypes.LOST_NODE),n.setUint8(s.Data.DATA,t),e(n)}function Ke(t){const e=y.get(_);e&&e(nn(t))}function Xe(){U=!1,y.clear(),m.clear(),I=0,c.useBluetooth().onDisconnect.post()}function je(t){L=t}function Je(){at()}const Qe={async connect(t){U=!0,c.useBluetooth().onConnect.post()},async reconnect(){if(!L)throw new Error("No devices found");U=!0,c.useBluetooth().onConnect.post()},async readCharacteristic(t,e){},async writeCharacteristic(t,e,o){if(!U||e!==Fe||d.dropRate!==void 0&&d.dropRate>0&&Math.random()<d.dropRate)return;const n=o[s.Data.CACHE],r=o[s.Data.POSITION]|o[s.Data.POSITION+1]<<8,a=o[s.Data.DATA_SIZE]|o[s.Data.DATA_SIZE+1]<<8;m.has(n)||m.set(n,{received:new Set,dataSize:a});const i=m.get(n);i.received.add(r),I++;const u=Math.ceil(a/R);if(i.received.size>=u){const f=d.responseDelay??10;setTimeout(()=>{tn(n)},f);return}if(d.partialDelivery&&I>=He){I=0;const f=d.responseDelay??10;setTimeout(()=>{const w=[];for(const[v,H]of m){const it=Math.ceil(H.dataSize/R);H.received.size<it&&w.push(v)}w.length>0&&en(w)},f)}},async subscribe(t,e,o){y.set(e,o),e===rt&&on()}};function tn(t){const e=y.get(_);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+1),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.MESSAGE_DONE),n.setUint8(s.Data.DATA,t),m.delete(t),I=0,e(n)}function en(t){const e=y.get(_);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+t.length),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.DATA_FEEDBACK);for(let r=0;r<t.length;r++)n.setUint8(s.Data.DATA+r,t[r]);e(n)}function nn(t){const e=s.Data.DATA+1+t.neighbors.length,o=new ArrayBuffer(e),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.NODES),n.setUint8(s.Data.DATA,t.id);for(let r=0;r<t.neighbors.length;r++)n.setUint8(s.Data.DATA+1+r,t.neighbors[r]);return n}function on(){const t=y.get(rt);if(!t||d.standees.length===0)return;const e=[d.standees.length];for(const r of d.standees)e.push(r.id,r.neighbors.length,...r.neighbors);const o=200,n=e.length;for(let r=0;r<n;r+=R){const a=Math.min(R,n-r),i=W+a,u=new ArrayBuffer(i),l=new DataView(u);l.setUint8(s.Data.ID,0),l.setUint8(s.Data.CACHE,o),l.setUint8(s.Data.TTL,10),l.setUint16(s.Data.POSITION,r,!0),l.setUint16(s.Data.DATA_SIZE,n,!0);for(let f=0;f<a;f++)l.setUint8(W+f,e[r+f]);t(l)}}function sn(){return{activate:Ge,deactivate:at,isActive:We,getConfig:Ve,updateConfig:Ze,pressButton:Ye,simulateNodeLost:qe,simulateNodeJoin:Ke,disconnect:Xe,reconnectable:je,_resetForTesting:Je}}exports.useActions=ge;exports.useBinary=P;exports.useButtons=Ae;exports.useConverter=Ce;exports.useDebug=ze;exports.useDisplay=ee;exports.useGraphics=Ot;exports.useImage=et;exports.useLoader=$t;exports.useMesh=j;exports.useMockBluetooth=sn;exports.useStandeeElements=Kt;exports.useStandees=Yt;exports.useUtils=Ue;