2 lines
8.4 KiB
JavaScript
2 lines
8.4 KiB
JavaScript
import{a as j}from"../chunk-AI6QGAXS.js";import{t as U,w as z}from"../chunk-QDPEV33M.js";import"../chunk-F4GETNPB.js";import{e as t,f as p,g as u,h,j as Q}from"../chunk-QY3QWFKW.js";Q();var W,T,C,M,B,w,x,L,P,A,_,O,R,v,G,k,E,D,S,i,K,I,g,J,q=class q extends j{constructor(e,r){super();p(this,i);p(this,W);p(this,T,0);p(this,C,!1);p(this,M,!1);p(this,B,!1);p(this,w,new EventTarget);p(this,x);p(this,L,!1);p(this,P);p(this,A);p(this,_);p(this,O);p(this,R);p(this,v);p(this,G);p(this,k,new Map);p(this,E,new Set);p(this,D);p(this,S,[]);u(this,P,e),u(this,x,U()),u(this,D,r?.extensions??{}),u(this,_,new Promise(o=>{t(this,P).addEventListener("message",s=>{if(s.data.type==="here")o();else throw new Error("Invalid message")},{once:!0})})),u(this,O,new Promise(o=>{let s=a=>{a.data.type==="ready"&&(u(this,A,a.data.id),t(this,P).removeEventListener("message",s),o())};t(this,P).addEventListener("message",s)})),u(this,W,h(this,i,K).call(this,r))}static async create(e,r){let o=new q(e,r);return await t(o,W),o}get waitReady(){return new Promise(e=>{t(this,W).then(()=>{t(this,L)?e():e(new Promise(r=>{t(this,w).addEventListener("connected",()=>{r()})}))})})}get debug(){return t(this,T)}get ready(){return t(this,C)}get closed(){return t(this,M)}get isLeader(){return t(this,B)}async close(){var e;t(this,M)||(u(this,M,!0),t(this,R)?.close(),t(this,v)?.close(),(e=t(this,G))==null||e.call(this),t(this,P).terminate())}async[Symbol.asyncDispose](){await this.close()}async execProtocolRaw(e){return await h(this,i,g).call(this,"execProtocolRaw",e)}async execProtocol(e){return await h(this,i,g).call(this,"execProtocol",e)}async execProtocolStream(e){return await h(this,i,g).call(this,"execProtocolStream",e)}async execProtocolRawStream(e,r){await h(this,i,g).call(this,"execProtocolRawStream",e,r)}async syncToFs(){await h(this,i,g).call(this,"syncToFs")}async listen(e,r,o){let s=z(e),a=o??this;return t(this,k).has(s)||t(this,k).set(s,new Set),t(this,k).get(s).add(r),await a.exec(`LISTEN ${e}`),async l=>{await this.unlisten(s,r,l)}}async unlisten(e,r,o){await this.waitReady;let s=o??this;r?t(this,k).get(e)?.delete(r):t(this,k).delete(e),t(this,k).get(e)?.size===0&&await s.exec(`UNLISTEN ${e}`)}onNotification(e){return t(this,E).add(e),()=>{t(this,E).delete(e)}}offNotification(e){t(this,E).delete(e)}async dumpDataDir(e){return await h(this,i,g).call(this,"dumpDataDir",e)}onLeaderChange(e){return t(this,w).addEventListener("leader-change",e),()=>{t(this,w).removeEventListener("leader-change",e)}}offLeaderChange(e){t(this,w).removeEventListener("leader-change",e)}async _handleBlob(e){await h(this,i,g).call(this,"_handleBlob",e)}async _getWrittenBlob(){return await h(this,i,g).call(this,"_getWrittenBlob")}async _cleanupBlob(){await h(this,i,g).call(this,"_cleanupBlob")}async _checkReady(){await this.waitReady}async _runExclusiveQuery(e){await h(this,i,g).call(this,"_acquireQueryLock");try{return await e()}finally{await h(this,i,g).call(this,"_releaseQueryLock")}}async _runExclusiveTransaction(e){await h(this,i,g).call(this,"_acquireTransactionLock");try{return await e()}finally{await h(this,i,g).call(this,"_releaseTransactionLock")}}};W=new WeakMap,T=new WeakMap,C=new WeakMap,M=new WeakMap,B=new WeakMap,w=new WeakMap,x=new WeakMap,L=new WeakMap,P=new WeakMap,A=new WeakMap,_=new WeakMap,O=new WeakMap,R=new WeakMap,v=new WeakMap,G=new WeakMap,k=new WeakMap,E=new WeakMap,D=new WeakMap,S=new WeakMap,i=new WeakSet,K=async function(e={}){for(let[c,y]of Object.entries(t(this,D))){if(y instanceof URL)throw new Error("URL extensions are not supported on the client side of a worker");{let d=await y.setup(this,{},!0);if(d.emscriptenOpts&&console.warn(`PGlite extension ${c} returned emscriptenOpts, these are not supported on the client side of a worker`),d.namespaceObj){let b=this;b[c]=d.namespaceObj}d.bundlePath&&console.warn(`PGlite extension ${c} returned bundlePath, this is not supported on the client side of a worker`),d.init&&await d.init(),d.close&&t(this,S).push(d.close)}}await t(this,_);let{extensions:r,...o}=e;t(this,P).postMessage({type:"init",options:o}),await t(this,O);let s=`pglite-tab-close:${t(this,x)}`;u(this,G,await N(s));let a=`pglite-broadcast:${t(this,A)}`;u(this,R,new BroadcastChannel(a));let l=`pglite-tab:${t(this,x)}`;u(this,v,new BroadcastChannel(l)),t(this,R).addEventListener("message",async c=>{c.data.type==="leader-here"?(u(this,L,!1),t(this,w).dispatchEvent(new Event("leader-change")),h(this,i,I).call(this)):c.data.type==="notify"&&h(this,i,J).call(this,c.data.channel,c.data.payload)}),t(this,v).addEventListener("message",async c=>{c.data.type==="connected"&&(u(this,L,!0),t(this,w).dispatchEvent(new Event("connected")),u(this,T,await h(this,i,g).call(this,"getDebugLevel")),u(this,C,!0))}),t(this,P).addEventListener("message",async c=>{c.data.type==="leader-now"&&(u(this,B,!0),t(this,w).dispatchEvent(new Event("leader-change")))}),h(this,i,I).call(this),this._initArrayTypes()},I=async function(){t(this,L)||(t(this,R).postMessage({type:"tab-here",id:t(this,x)}),setTimeout(()=>h(this,i,I).call(this),16))},g=async function(e,...r){let o=U(),s={type:"rpc-call",callId:o,method:e,args:r};return t(this,v).postMessage(s),await new Promise((a,l)=>{let c=b=>{if(b.data.callId!==o)return;d();let f=b.data;if(f.type==="rpc-return")a(f.result);else if(f.type==="rpc-error"){let F=new Error(f.error.message);Object.assign(F,f.error),l(F)}else l(new Error("Invalid message"))},y=()=>{d(),l(new $)},d=()=>{t(this,v).removeEventListener("message",c),t(this,w).removeEventListener("leader-change",y)};t(this,w).addEventListener("leader-change",y),t(this,v).addEventListener("message",c)})},J=function(e,r){let o=t(this,k).get(e);if(o)for(let s of o)queueMicrotask(()=>s(r));for(let s of t(this,E))queueMicrotask(()=>s(e,r))};var H=q;async function se({init:m}){postMessage({type:"here"});let n=await new Promise(y=>{addEventListener("message",d=>{d.data.type==="init"&&y(d.data.options)},{once:!0})}),e=n.id??`${import.meta.url}:${n.dataDir??""}`;postMessage({type:"ready",id:e});let r=`pglite-election-lock:${e}`,o=`pglite-broadcast:${e}`,s=new BroadcastChannel(o),a=new Set;await N(r);let l=m(n);s.onmessage=async y=>{let d=y.data;switch(d.type){case"tab-here":V(d.id,await l,a);break}},s.postMessage({type:"leader-here",id:e}),postMessage({type:"leader-now"}),(await l).onNotification((y,d)=>{s.postMessage({type:"notify",channel:y,payload:d})})}function V(m,n,e){if(e.has(m))return;e.add(m);let r=`pglite-tab:${m}`,o=`pglite-tab-close:${m}`,s=new BroadcastChannel(r);navigator.locks.request(o,()=>new Promise(l=>{s.close(),e.delete(m),l()}));let a=X(m,n);s.addEventListener("message",async l=>{let c=l.data;switch(c.type){case"rpc-call":{await n.waitReady;let{callId:y,method:d,args:b}=c;try{let f=await a[d](...b);s.postMessage({type:"rpc-return",callId:y,result:f})}catch(f){console.error(f),s.postMessage({type:"rpc-error",callId:y,error:{message:f.message}})}break}}}),s.postMessage({type:"connected"})}function X(m,n){let e=null,r=null,o=`pglite-tab-close:${m}`;return N(o).then(()=>{r&&n.exec("ROLLBACK"),e?.(),r?.()}),{async getDebugLevel(){return n.debug},async close(){await n.close()},async execProtocol(s){let{messages:a,data:l}=await n.execProtocol(s);if(l.byteLength!==l.buffer.byteLength){let c=new ArrayBuffer(l.byteLength),y=new Uint8Array(c);return y.set(l),{messages:a,data:y}}else return{messages:a,data:l}},async execProtocolStream(s){return await n.execProtocolStream(s)},async execProtocolRawStream(s,a){return await n.execProtocolRawStream(s,a)},async execProtocolRaw(s){let a=await n.execProtocolRaw(s);if(a.byteLength!==a.buffer.byteLength){let l=new ArrayBuffer(a.byteLength),c=new Uint8Array(l);return c.set(a),c}else return a},async dumpDataDir(s){return await n.dumpDataDir(s)},async syncToFs(){return await n.syncToFs()},async _handleBlob(s){return await n._handleBlob(s)},async _getWrittenBlob(){return await n._getWrittenBlob()},async _cleanupBlob(){return await n._cleanupBlob()},async _checkReady(){return await n._checkReady()},async _acquireQueryLock(){return new Promise(s=>{n._runExclusiveQuery(()=>new Promise(a=>{e=a,s()}))})},async _releaseQueryLock(){e?.(),e=null},async _acquireTransactionLock(){return new Promise(s=>{n._runExclusiveTransaction(()=>new Promise(a=>{r=a,s()}))})},async _releaseTransactionLock(){r?.(),r=null}}}var $=class extends Error{constructor(){super("Leader changed, pending operation in indeterminate state")}};async function N(m){let n;return await new Promise(e=>{navigator.locks.request(m,()=>new Promise(r=>{n=r,e()}))}),n}export{$ as LeaderChangedError,H as PGliteWorker,se as worker};
|
|
//# sourceMappingURL=index.js.map
|