@liascript/exporter
Version:
A generic exporter for LiaScript
1 lines • 22.4 kB
JavaScript
!function(e,t,s,n,r){var i="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},a="function"==typeof i[n]&&i[n],o=a.cache||{},c="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function d(t,s){if(!o[t]){if(!e[t]){var r="function"==typeof i[n]&&i[n];if(!s&&r)return r(t,!0);if(a)return a(t,!0);if(c&&"string"==typeof t)return c(t);var h=new Error("Cannot find module '"+t+"'");throw h.code="MODULE_NOT_FOUND",h}u.resolve=function(s){var n=e[t][1][s];return null!=n?n:s},u.cache={};var l=o[t]=new d.Module(t);e[t][0].call(l.exports,u,l,l.exports,this)}return o[t].exports;function u(e){var t=u.resolve(e);return!1===t?{}:d(t)}}d.isParcelRequire=!0,d.Module=function(e){this.id=e,this.bundle=d,this.exports={}},d.modules=e,d.cache=o,d.parent=a,d.register=function(t,s){e[t]=[function(e,t){t.exports=s},{}]},Object.defineProperty(d,"root",{get:function(){return i[n]}}),i[n]=d;for(var h=0;h<t.length;h++)d(t[h])}({"3M9RT":[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"GenericProvider",(()=>r.GenericProvider)),n.export(s,"PubSubChannel",(()=>r.PubSubChannel)),n.export(s,"SyncHealthMonitor",(()=>i.SyncHealthMonitor)),n.export(s,"computeDocumentHash",(()=>i.computeDocumentHash));var r=e("./index"),i=e("./sync-monitor")},{"./index":"9b0Z3","./sync-monitor":"jgKsD","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],"9b0Z3":[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"PubSubChannel",(()=>g)),n.export(s,"GenericProvider",(()=>m));var r=e("yjs"),i=e("y-protocols/awareness"),a=e("y-protocols/sync"),o=e("lib0/encoding"),c=e("lib0/decoding"),d=e("lib0/observable"),h=e("lib0/broadcastchannel");const l=(()=>{const e=new Uint32Array(256);for(let t=0;t<256;t++){let s=t;for(let e=0;e<8;e++)s=1&s?s>>>1^3988292384:s>>>1;e[t]=s}return e})();function u(e){let t=4294967295;for(let s=0;s<e.length;s++)t=t>>>8^l[255&(t^e[s])];return(4294967295^t)>>>0}function p(e){const t=u(e),s=new Uint8Array(4+e.length);return s[0]=t>>>24&255,s[1]=t>>>16&255,s[2]=t>>>8&255,s[3]=255&t,s.set(e,4),s}function _(e){const t=r.encodeStateAsUpdate(e);let s=0;for(let e=0;e<t.length;e++)s=(s<<5)-s+t[e]|0;return s}class g extends d.Observable{constructor(e){super(),this.provider=e}publish(e,t){this.provider._sendPubSub(e,t)}subscribe(e,t){const s=(s,n)=>{"*"!==e&&e!==n||t(s,n)};return this.on("message",s),()=>this.off("message",s)}_handleMessage(e,t){this.emit("message",[t,e])}}class m extends d.Observable{constructor(e,t,s={}){super(),this._status={state:"disconnected"},this._synced=!1,this._destroying=!1,this._bcChannel="",this._bcConnected=!1,this._hashMismatchCount=0,this._lastHashMismatchTime=0,this._syncRequestTimes=[],this._maxSyncRequestsPerWindow=20,this._syncRequestWindowMs=1e4,this._localSeqNum=0,this._remoteSeqNums=new Map,this._corruptedMessageCount=0,this._lastCorruptedMessageTime=0,this._batchUpdates=0,this._pendingUpdate=null,this._awarenessInterval=100,this._pendingAwarenessClients=new Set,this._lastAwarenessTime=0,this.doc=e,this.transport=t,this.pubsub=new g(this),this.awareness=s.awareness||new i.Awareness(e),this._syncInterval=s.syncInterval??5e3,this._verifyUpdates=s.verifyUpdates??!0,this._batchUpdates=s.batchUpdates??0,this._disableBc=s.disableBc??!1,this._awarenessInterval=s.awarenessInterval??100,this._setupDocumentSync(),this._setupAwarenessSync()}async connect(e){if(this._destroying)throw new Error("Provider is being destroyed");if("connected"!==this._status.state)if("connecting"!==this._status.state){this._setStatus({state:"connecting"});try{if(this._setupBroadcastChannel(e),await this.transport.connect(e),this._unsubscribeTransport=this.transport.onMessage((e=>{this._handleIncomingMessage(e)})),this.transport.onPeerConnect){const e=this.transport.onPeerConnect((e=>{this._destroying||this.syncNow()})),t=this._unsubscribeTransport;this._unsubscribeTransport=()=>{t?.(),e()}}this._setStatus({state:"connected"}),this.syncNow(),this._broadcastAwareness([this.doc.clientID]),this._syncInterval>0&&(this._syncIntervalId=setInterval((()=>{if(this.transport.isConnected&&!this._destroying){const e=Date.now();this._syncRequestTimes=this._syncRequestTimes.filter((t=>e-t<this._syncRequestWindowMs)),this._syncRequestTimes.length<this._maxSyncRequestsPerWindow&&this._sendSyncStep1()}}),this._syncInterval))}catch(e){throw this._setStatus({state:"error",error:e instanceof Error?e:new Error(String(e))}),e}}else console.warn("[GenericProvider] Connection already in progress, ignoring connect() call");else console.warn("[GenericProvider] Already connected, ignoring connect() call")}disconnect(){if(void 0!==this._syncIntervalId&&(clearInterval(this._syncIntervalId),this._syncIntervalId=void 0),this._corruptedMessageCount=0,this._lastCorruptedMessageTime=0,void 0!==this._batchTimeoutId&&(clearTimeout(this._batchTimeoutId),this._batchTimeoutId=void 0,this._pendingUpdate&&this.transport.isConnected&&this._sendUpdate(this._pendingUpdate),this._pendingUpdate=null),void 0!==this._awarenessTimeoutId&&(clearTimeout(this._awarenessTimeoutId),this._awarenessTimeoutId=void 0,this._pendingAwarenessClients.size>0&&this.transport.isConnected)){const e=Array.from(this._pendingAwarenessClients);this._sendAwarenessNow(e)}this._pendingAwarenessClients.clear(),this._disconnectBroadcastChannel(),this._unsubscribeTransport&&(this._unsubscribeTransport(),this._unsubscribeTransport=void 0),i.removeAwarenessStates(this.awareness,[this.doc.clientID],"disconnect"),this.transport.disconnect(),this._synced=!1,this._setStatus({state:"disconnected"})}destroy(){this._destroying=!0,void 0!==this._syncIntervalId&&(clearInterval(this._syncIntervalId),this._syncIntervalId=void 0),void 0!==this._batchTimeoutId&&(clearTimeout(this._batchTimeoutId),this._batchTimeoutId=void 0,this._pendingUpdate&&this.transport.isConnected&&this._sendUpdate(this._pendingUpdate),this._pendingUpdate=null),this.disconnect(),this._updateHandler&&(this.doc.off("update",this._updateHandler),this._updateHandler=void 0),this._awarenessUpdateHandler&&(this.awareness.off("update",this._awarenessUpdateHandler),this._awarenessUpdateHandler=void 0),this._beforeUnloadHandler&&"undefined"!=typeof window&&(window.removeEventListener("beforeunload",this._beforeUnloadHandler),this._beforeUnloadHandler=void 0),this.awareness.destroy(),super.destroy()}get status(){return this._status}get connected(){return this.transport.isConnected}get bcConnected(){return this._bcConnected}get synced(){return this._synced}syncNow(){if(!this.transport.isConnected)return void console.warn("Cannot sync: transport not connected");const e=r.encodeStateAsUpdate(this.doc);e.length>0&&this._sendUpdate(e),this._sendSyncStep1(),this._broadcastAwareness([this.doc.clientID])}_setupDocumentSync(){this._updateHandler=(e,t)=>{t!==this&&(this._batchUpdates>0?this._batchUpdate(e):this._sendUpdate(e))},this.doc.on("update",this._updateHandler)}_batchUpdate(e){if(this._pendingUpdate)try{this._pendingUpdate=r.mergeUpdates([this._pendingUpdate,e])}catch(t){console.error("[GenericProvider] Failed to merge updates:",t),this._sendUpdate(this._pendingUpdate),this._pendingUpdate=e}else this._pendingUpdate=e;void 0!==this._batchTimeoutId&&clearTimeout(this._batchTimeoutId),this._batchTimeoutId=setTimeout((()=>{this._pendingUpdate&&(this._sendUpdate(this._pendingUpdate),this._pendingUpdate=null),this._batchTimeoutId=void 0}),this._batchUpdates)}_setupAwarenessSync(){this._awarenessUpdateHandler=({added:e,updated:t,removed:s},n)=>{const r=e.concat(t).concat(s);this._broadcastAwareness(r)},this.awareness.on("update",this._awarenessUpdateHandler),"undefined"!=typeof window&&(this._beforeUnloadHandler=()=>{i.removeAwarenessStates(this.awareness,[this.doc.clientID],"window unload"),this._disconnectBroadcastChannel()},window.addEventListener("beforeunload",this._beforeUnloadHandler))}_handleIncomingMessage(e){const t=function(e){if(e.length<4)return null;const t=(e[0]<<24|e[1]<<16|e[2]<<8|e[3])>>>0,s=e.subarray(4);return u(s)!==t?null:s}(e);if(null!==t)try{const e=c.createDecoder(t),s=c.readVarUint(e);switch(s){case 0:{const t=o.createEncoder();o.writeVarUint(t,0);a.readSyncMessage(e,t,this.doc,this)!==a.messageYjsSyncStep2||this._synced||(this._synced=!0,this.emit("synced",[!0])),o.length(t)>1&&this._send(o.toUint8Array(t));break}case 1:i.applyAwarenessUpdate(this.awareness,c.readVarUint8Array(e),this);break;case 2:{const t=c.readVarString(e),s=c.readVarUint8Array(e);try{const e=(new TextDecoder).decode(s),n=JSON.parse(e);this.pubsub._handleMessage(t,n)}catch(e){console.error("Error decoding pub/sub message:",e)}break}case 3:{const t=c.readVarUint(e),s=c.readVarUint(e),n=this._remoteSeqNums.get(s)??-1;if(t<=n){console.warn(`[GenericProvider] Duplicate or out-of-order update detected from client ${s}: seqNum ${t} <= lastSeen ${n}`);break}if(n>=0&&t>n+1){const e=t-n-1;console.warn(`[GenericProvider] Sequence gap detected from client ${s}: expected ${n+1}, got ${t} (gap of ${e} messages)`),this._sendSyncStep1()}this._remoteSeqNums.set(s,t);const r=o.createEncoder();o.writeVarUint(r,0);const i=a.readSyncMessage(e,r,this.doc,this),d=c.readVarInt(e),h=_(this.doc);if(h!==d){this._hashMismatchCount++;const e=Date.now();e-this._lastHashMismatchTime>1e4&&(this._hashMismatchCount=1),this._lastHashMismatchTime=e;const t=Math.min(1e4,10*Math.pow(5,this._hashMismatchCount-1));console.warn(`[GenericProvider] Hash mismatch #${this._hashMismatchCount} detected! Local: ${h}, Expected: ${d}`),console.warn(`[GenericProvider] Re-sync scheduled in ${t}ms...`),setTimeout((()=>{this.transport.isConnected&&!this._destroying&&this.syncNow()}),t)}else this._hashMismatchCount=0;i!==a.messageYjsSyncStep2||this._synced||h!==d||(this._synced=!0,this.emit("synced",[!0])),o.length(r)>1&&this._send(o.toUint8Array(r));break}default:console.warn("Unknown message type:",s)}}catch(e){console.error("[GenericProvider] Error handling message:",e)}else{this._corruptedMessageCount++;const e=Date.now();e-this._lastCorruptedMessageTime>1e4&&(this._corruptedMessageCount=1),this._lastCorruptedMessageTime=e,console.warn(`[GenericProvider] 💥 Corrupted message rejected (#${this._corruptedMessageCount}): CRC32 checksum mismatch. This is expected if data corruption simulation is enabled.`);const t=Math.min(5e3,100*Math.pow(5,Math.min(this._corruptedMessageCount-1,3)));setTimeout((()=>{this.transport.isConnected&&!this._destroying&&this._sendSyncStep1()}),t)}}_sendSyncStep1(){const e=Date.now();if(this._syncRequestTimes=this._syncRequestTimes.filter((t=>e-t<this._syncRequestWindowMs)),this._syncRequestTimes.length>=this._maxSyncRequestsPerWindow)return void console.warn(`[GenericProvider] Sync rate limit exceeded (${this._maxSyncRequestsPerWindow} requests per ${this._syncRequestWindowMs/1e3}s), throttling...`);this._syncRequestTimes.push(e);const t=o.createEncoder();o.writeVarUint(t,0),a.writeSyncStep1(t,this.doc),this._send(o.toUint8Array(t))}_sendUpdate(e){const t=o.createEncoder();if(this._verifyUpdates){o.writeVarUint(t,3),o.writeVarUint(t,this._localSeqNum++),o.writeVarUint(t,this.doc.clientID),a.writeUpdate(t,e);const s=_(this.doc);o.writeVarInt(t,s)}else o.writeVarUint(t,0),a.writeUpdate(t,e);this._send(o.toUint8Array(t))}_sendAwarenessUpdate(e){const t=o.createEncoder();o.writeVarUint(t,1),o.writeVarUint8Array(t,i.encodeAwarenessUpdate(this.awareness,e)),this._send(o.toUint8Array(t))}_sendPubSub(e,t){if(this.transport.isConnected)try{const s=o.createEncoder();o.writeVarUint(s,2),o.writeVarString(s,e);const n=JSON.stringify(t),r=(new TextEncoder).encode(n);o.writeVarUint8Array(s,r),this._send(o.toUint8Array(s))}catch(e){console.error("Error sending pub/sub message:",e)}else console.warn("Cannot send pub/sub message: not connected")}_broadcastAwareness(e){if(0===e.length)return;if(this._awarenessInterval<=0)return void this._sendAwarenessNow(e);for(const t of e)this._pendingAwarenessClients.add(t);if(void 0!==this._awarenessTimeoutId)return;const t=Date.now()-this._lastAwarenessTime,s=Math.max(0,this._awarenessInterval-t);this._awarenessTimeoutId=setTimeout((()=>{this._awarenessTimeoutId=void 0,this._lastAwarenessTime=Date.now();const e=Array.from(this._pendingAwarenessClients);this._pendingAwarenessClients.clear(),e.length>0&&this._sendAwarenessNow(e)}),s)}_sendAwarenessNow(e){const t=o.createEncoder();o.writeVarUint(t,1),o.writeVarUint8Array(t,i.encodeAwarenessUpdate(this.awareness,e)),this._send(o.toUint8Array(t))}_setupBroadcastChannel(e){if(this._disableBc||"undefined"==typeof BroadcastChannel||"undefined"==typeof window)return;this._bcChannel=`yjs-${e.room}`,this._bcSubscriber=(e,t)=>{if(t===this)return;const s=new Uint8Array(e);this._handleIncomingMessage(s)},h.subscribe(this._bcChannel,this._bcSubscriber),this._bcConnected=!0;const t=o.createEncoder();o.writeVarUint(t,0),a.writeSyncStep1(t,this.doc),h.publish(this._bcChannel,p(o.toUint8Array(t)),this);const s=o.createEncoder();if(o.writeVarUint(s,0),a.writeSyncStep2(s,this.doc),h.publish(this._bcChannel,p(o.toUint8Array(s)),this),null!==this.awareness.getLocalState()){const e=o.createEncoder();o.writeVarUint(e,1),o.writeVarUint8Array(e,i.encodeAwarenessUpdate(this.awareness,[this.doc.clientID])),h.publish(this._bcChannel,p(o.toUint8Array(e)),this)}}_disconnectBroadcastChannel(){if(!this._bcConnected||!this._bcSubscriber)return;const e=o.createEncoder();o.writeVarUint(e,1),o.writeVarUint8Array(e,i.encodeAwarenessUpdate(this.awareness,[this.doc.clientID],new Map)),h.publish(this._bcChannel,p(o.toUint8Array(e)),this),h.unsubscribe(this._bcChannel,this._bcSubscriber),this._bcConnected=!1,this._bcSubscriber=void 0}_send(e){const t=p(e);if(this._bcConnected&&h.publish(this._bcChannel,t,this),this.transport.isConnected)try{const e=this.transport.send(t);e instanceof Promise&&e.catch((e=>{console.error("Error sending data:",e)}))}catch(e){console.error("Error sending data:",e)}}_setStatus(e){this._status=e,this.emit("status",[e])}_testSetSequenceNumber(e){this._localSeqNum=e,console.warn(`[GenericProvider TEST] Sequence number set to ${e} (MAX_SAFE_INTEGER: ${Number.MAX_SAFE_INTEGER})`)}_testGetSequenceNumber(){return this._localSeqNum}}},{yjs:"dugeO","y-protocols/awareness":"dkAja","y-protocols/sync":"2fRsv","lib0/encoding":"iMu7f","lib0/decoding":"gVpVW","lib0/observable":"ewQE3","lib0/broadcastchannel":"42CYj","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],dkAja:[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"outdatedTimeout",(()=>h)),n.export(s,"Awareness",(()=>l)),n.export(s,"removeAwarenessStates",(()=>u)),n.export(s,"encodeAwarenessUpdate",(()=>p)),n.export(s,"modifyAwarenessUpdate",(()=>_)),n.export(s,"applyAwarenessUpdate",(()=>g));var r=e("lib0/encoding"),i=e("lib0/decoding"),a=e("lib0/time"),o=e("lib0/math"),c=e("lib0/observable"),d=e("lib0/function");e("yjs");const h=3e4;class l extends c.Observable{constructor(e){super(),this.doc=e,this.clientID=e.clientID,this.states=new Map,this.meta=new Map,this._checkInterval=setInterval((()=>{const e=a.getUnixTime();null!==this.getLocalState()&&h/2<=e-this.meta.get(this.clientID).lastUpdated&&this.setLocalState(this.getLocalState());const t=[];this.meta.forEach(((s,n)=>{n!==this.clientID&&h<=e-s.lastUpdated&&this.states.has(n)&&t.push(n)})),t.length>0&&u(this,t,"timeout")}),o.floor(h/10)),e.on("destroy",(()=>{this.destroy()})),this.setLocalState({})}destroy(){this.emit("destroy",[this]),this.setLocalState(null),super.destroy(),clearInterval(this._checkInterval)}getLocalState(){return this.states.get(this.clientID)||null}setLocalState(e){const t=this.clientID,s=this.meta.get(t),n=void 0===s?0:s.clock+1,r=this.states.get(t);null===e?this.states.delete(t):this.states.set(t,e),this.meta.set(t,{clock:n,lastUpdated:a.getUnixTime()});const i=[],o=[],c=[],h=[];null===e?h.push(t):null==r?null!=e&&i.push(t):(o.push(t),d.equalityDeep(r,e)||c.push(t)),(i.length>0||c.length>0||h.length>0)&&this.emit("change",[{added:i,updated:c,removed:h},"local"]),this.emit("update",[{added:i,updated:o,removed:h},"local"])}setLocalStateField(e,t){const s=this.getLocalState();null!==s&&this.setLocalState({...s,[e]:t})}getStates(){return this.states}}const u=(e,t,s)=>{const n=[];for(let s=0;s<t.length;s++){const r=t[s];if(e.states.has(r)){if(e.states.delete(r),r===e.clientID){const t=e.meta.get(r);e.meta.set(r,{clock:t.clock+1,lastUpdated:a.getUnixTime()})}n.push(r)}}n.length>0&&(e.emit("change",[{added:[],updated:[],removed:n},s]),e.emit("update",[{added:[],updated:[],removed:n},s]))},p=(e,t,s=e.states)=>{const n=t.length,i=r.createEncoder();r.writeVarUint(i,n);for(let a=0;a<n;a++){const n=t[a],o=s.get(n)||null,c=e.meta.get(n).clock;r.writeVarUint(i,n),r.writeVarUint(i,c),r.writeVarString(i,JSON.stringify(o))}return r.toUint8Array(i)},_=(e,t)=>{const s=i.createDecoder(e),n=r.createEncoder(),a=i.readVarUint(s);r.writeVarUint(n,a);for(let e=0;e<a;e++){const e=i.readVarUint(s),a=i.readVarUint(s),o=t(JSON.parse(i.readVarString(s)));r.writeVarUint(n,e),r.writeVarUint(n,a),r.writeVarString(n,JSON.stringify(o))}return r.toUint8Array(n)},g=(e,t,s)=>{const n=i.createDecoder(t),r=a.getUnixTime(),o=[],c=[],h=[],l=[],u=i.readVarUint(n);for(let t=0;t<u;t++){const t=i.readVarUint(n);let s=i.readVarUint(n);const a=JSON.parse(i.readVarString(n)),u=e.meta.get(t),p=e.states.get(t),_=void 0===u?0:u.clock;(_<s||_===s&&null===a&&e.states.has(t))&&(null===a?t===e.clientID&&null!=e.getLocalState()?s++:e.states.delete(t):e.states.set(t,a),e.meta.set(t,{clock:s,lastUpdated:r}),void 0===u&&null!==a?o.push(t):void 0!==u&&null===a?l.push(t):null!==a&&(d.equalityDeep(a,p)||h.push(t),c.push(t)))}(o.length>0||h.length>0||l.length>0)&&e.emit("change",[{added:o,updated:h,removed:l},s]),(o.length>0||c.length>0||l.length>0)&&e.emit("update",[{added:o,updated:c,removed:l},s])}},{"lib0/encoding":"iMu7f","lib0/decoding":"gVpVW","lib0/time":"bwglv","lib0/math":"8ydGZ","lib0/observable":"ewQE3","lib0/function":"bmDBb",yjs:"dugeO","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],"2fRsv":[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"messageYjsSyncStep1",(()=>o)),n.export(s,"messageYjsSyncStep2",(()=>c)),n.export(s,"messageYjsUpdate",(()=>d)),n.export(s,"writeSyncStep1",(()=>h)),n.export(s,"writeSyncStep2",(()=>l)),n.export(s,"readSyncStep1",(()=>u)),n.export(s,"readSyncStep2",(()=>p)),n.export(s,"writeUpdate",(()=>_)),n.export(s,"readUpdate",(()=>g)),n.export(s,"readSyncMessage",(()=>m));var r=e("lib0/encoding"),i=e("lib0/decoding"),a=e("yjs");const o=0,c=1,d=2,h=(e,t)=>{r.writeVarUint(e,o);const s=a.encodeStateVector(t);r.writeVarUint8Array(e,s)},l=(e,t,s)=>{r.writeVarUint(e,c),r.writeVarUint8Array(e,a.encodeStateAsUpdate(t,s))},u=(e,t,s)=>l(t,s,i.readVarUint8Array(e)),p=(e,t,s,n)=>{try{a.applyUpdate(t,i.readVarUint8Array(e),s)}catch(e){null!=n&&n(e),console.error("Caught error while handling a Yjs update",e)}},_=(e,t)=>{r.writeVarUint(e,d),r.writeVarUint8Array(e,t)},g=p,m=(e,t,s,n,r)=>{const a=i.readVarUint(e);switch(a){case o:u(e,t,s);break;case c:p(e,s,n,r);break;case d:g(e,s,n,r);break;default:throw new Error("Unknown message type")}return a}},{"lib0/encoding":"iMu7f","lib0/decoding":"gVpVW",yjs:"dugeO","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],"42CYj":[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"subscribe",(()=>l)),n.export(s,"unsubscribe",(()=>u)),n.export(s,"publish",(()=>p));var r=e("./map.js"),i=e("./set.js"),a=e("./buffer.js"),o=e("./storage.js");const c=new Map;const d="undefined"==typeof BroadcastChannel?class{constructor(e){this.room=e,this.onmessage=null,this._onChange=t=>t.key===e&&null!==this.onmessage&&this.onmessage({data:a.fromBase64(t.newValue||"")}),o.onChange(this._onChange)}postMessage(e){o.varStorage.setItem(this.room,a.toBase64(a.createUint8ArrayFromArrayBuffer(e)))}close(){o.offChange(this._onChange)}}:BroadcastChannel,h=e=>r.setIfUndefined(c,e,(()=>{const t=i.create(),s=new d(e);return s.onmessage=e=>t.forEach((t=>t(e.data,"broadcastchannel"))),{bc:s,subs:t}})),l=(e,t)=>(h(e).subs.add(t),t),u=(e,t)=>{const s=h(e),n=s.subs.delete(t);return n&&0===s.subs.size&&(s.bc.close(),c.delete(e)),n},p=(e,t,s=null)=>{const n=h(e);n.bc.postMessage(t),n.subs.forEach((e=>e(t,s)))}},{"./map.js":"erHG1","./set.js":"3aqr7","./buffer.js":"9uhqR","./storage.js":"3Gk2E","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],jgKsD:[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"computeDocumentHash",(()=>i)),n.export(s,"SyncHealthMonitor",(()=>a));var r=e("yjs");function i(e){return function(e){let t=0;for(let s=0;s<e.length;s++)t=(t<<5)-t+e[s]|0;return t.toString(36)}(r.encodeStateAsUpdate(e))}class a{constructor(e,t={}){this.options=t,this.localHash="",this.peerHashes=new Map,this.provider=e,this.options.checkInterval=t.checkInterval??1e4}start(){this.intervalId||(console.log("[SyncMonitor] Starting health checks..."),this.intervalId=setInterval((()=>{this.checkHealth()}),this.options.checkInterval),this.provider.pubsub.subscribe("sync-health",(e=>{"hash-broadcast"===e.type&&this.handlePeerHash(e.clientId,e.hash)})))}stop(){this.intervalId&&(clearInterval(this.intervalId),this.intervalId=void 0)}checkHealth(){this.provider.connected&&(this.localHash=i(this.provider.doc),this.provider.pubsub.publish("sync-health",{type:"hash-broadcast",clientId:this.provider.doc.clientID,hash:this.localHash}),this.detectDivergence())}handlePeerHash(e,t){this.peerHashes.set(e,t)}detectDivergence(){const e=[];this.peerHashes.forEach(((t,s)=>{t!==this.localHash&&e.push(s)})),e.length>0&&(console.warn("[SyncMonitor] ⚠️ Desync detected with clients:",e),console.warn("[SyncMonitor] Local hash:",this.localHash,"Peer hashes:",Array.from(this.peerHashes.entries())),console.log("[SyncMonitor] 🔄 Triggering automatic re-sync..."),this.provider.syncNow(),this.options.onDesync&&this.options.onDesync({localHash:this.localHash,peerHashes:new Map(this.peerHashes),divergentClients:e,timestamp:Date.now()}))}}},{yjs:"dugeO","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}]},[],0,"parcelRequire55a5");