UNPKG

sabayon

Version:
2 lines (1 loc) 3.99 kB
let{parse:e,stringify:t,rawJSON:s,isRawJSON:n}=JSON;if(!s){const{freeze:r,setPrototypeOf:a}=Object;class o{static isRawJSON(e){return"object"==typeof e&&!!e&&#e in e}#e=!0;constructor(e){this.rawJSON=e,r(a(this,null))}}({isRawJSON:n}=o),s=t=>{const s=typeof t;switch(s){case"object":if(t)break;case"bigint":case"boolean":case"number":t=String(t);case"string":return e(t),new o(t)}throw new TypeError("Unexpected "+s)};const c=e;e=(e,t)=>{if("function"==typeof t){const s=t,n=[];e=String(e).replace(/(?:"(?:(?=(\\?))\1.)*?"\s*:?|[0-9eE+.-]+|true|false|null)/g,(e=>":"!==e.at(-1)?'"'===e[0]?n.push(e)-1:`"${e}"`:e)),t=function(e,t){switch(typeof t){case"number":t=n[t];case"string":return s.call(this,e,c(t),{source:t})}return s.call(this,e,t,{})}}return c(e,t)};const{isArray:i}=Array,l=(e,t)=>t,u=(e,t)=>{let s=0;const r=`${Math.random()}00`.slice(2),a="function"==typeof e?e:e&&i(e)?(t,s)=>!t||e.includes(t)?s:void 0:l;return function(e,o){if(o=a.call(this,e,o),n(o)){const{rawJSON:e}=o;t.set(o=`'${++s}'${r}`,e)}return o}},p=t;t=(e,t,s)=>{const n=new Map,r=p(e,u(t,n),s);return n.size?r.replace(new RegExp(`"(${[...n.keys()].join("|")})"`,"g"),((e,t)=>n.get(t))):r}}const r=(e,t,s)=>"number"==typeof t&&String(t)!==s?.source?BigInt(s.source):t,{ArrayBuffer:a,Atomics:o,Promise:c}=globalThis,{isArray:i}=Array,{create:l,getPrototypeOf:u,values:p}=Object,f=u(Int32Array),g=l(o),w=({currentTarget:e,type:t,origin:s,lastEventId:n,source:r,ports:a},o)=>e.dispatchEvent(new MessageEvent(t,{data:o,origin:s,lastEventId:n,source:r,ports:a})),y=()=>c.withResolvers();let d=0;const h=new Map,b=(e,t)=>class extends e{constructor(e,...s){super(e,...s),e instanceof t&&h.set(this,[d++,0,y()])}},v=new WeakSet,m=e=>(v.add(e),e),A=e=>null!==e&&"object"==typeof e&&!v.has(e),S=new WeakMap,k=(e,t,s)=>{if(h.has(e))t.set(e,h.get(e)[0]);else if(!(e instanceof f||e instanceof a))for(const n of p(e))A(n)&&!s.has(n)&&(s.add(n),k(n,t,s))};let O=0;const E=(...e)=>({value:new c((async t=>{const s="/__sabayon_wait_async.js";if(!O){const e=fetch(s,{method:"HEAD"}).then((e=>e.ok),(()=>!1));O=await e?1:-1}const n=new Worker(O<0?"data:application/javascript,onmessage%3De%3D%3EpostMessage(!Atomics.wait(...e.data))":s);n.onmessage=()=>{n.terminate(),t("ok")},n.postMessage(e)}))}),T=(e,t)=>{const s=h.get(e),[n,r,{promise:a}]=s;return s[1]=t,[n,a]};let{BigInt64Array:I,Int32Array:M,SharedArrayBuffer:j,addEventListener:J,postMessage:N}=globalThis,$=!0,x=e=>e,R=!1;const P=y();try{new j(4),g.waitAsync||(g.waitAsync=E),P.resolve()}catch(t){const s=N,n=J,o=[];let c="",l="";j=class extends a{},I=b(I,j),M=b(M,j),x=m,R=!0,g.notify=(e,t)=>{const[n]=(e=>S.get(e))(e);return s([c,1,e,n,t]),0},g.waitAsync=(...e)=>{const[t,s]=T(...e);return{value:s}},g.wait=(t,s,...n)=>{const[a]=T(t,s,...n),o=new XMLHttpRequest;o.open("POST",`${l}?sabayon`,!1),o.setRequestHeader("Content-Type","application/json"),o.send(`["${c}",${a},${s}]`),h.delete(t);const i=t instanceof I?e(o.responseText,r):e(o.responseText);return t.set(i),"ok"},n("message",(e=>{if(((e,t)=>{const{data:s}=e,n=i(s)&&(s.at(0)===t||0===s.at(1)&&!t);return n&&(e.stopImmediatePropagation(),e.preventDefault()),n})(e,c)){const[t,s,...n]=e.data;switch(s){case 0:c=t,l=n.at(0)?.serviceWorker||"",l||(g.wait=null,P.resolve());break;case 1:((e,t,s)=>{for(const[n,[r,a,{resolve:o}]]of h)if(t===r&&s===a){n.set(e),h.delete(n),o("ok");break}})(...n);break;case 2:((e,t,s)=>{for(const[s,n]of t)S.set(s,[n,e.currentTarget]);w(e,s)})(e,...n);break;case 3:P.resolve()}}else if($){const{currentTarget:t,type:s,origin:n,lastEventId:r,source:a,ports:c}=e;o.push([{currentTarget:t,type:s,origin:n,lastEventId:r,source:a,ports:c},e.data])}})),J=(e,...t)=>{if(n(e,...t),o.length)for(const e of o.splice(0))w(...e)},N=(e,...t)=>s(((e,t)=>{const s=new Map;return A(t)&&k(t,s,new Set),s.size?[e,2,s,t]:t})(c,e),...t)}await P.promise,$=!1;export{g as Atomics,I as BigInt64Array,M as Int32Array,j as SharedArrayBuffer,J as addEventListener,x as ignore,R as polyfill,N as postMessage};